HttpCurl.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /**
  3. * HttpCurl Curl模拟Http工具类
  4. *
  5. * @author gaoming13 <gaoming13@yeah.net>
  6. * @link https://github.com/gaoming13/wechat-php-sdk
  7. * @link http://me.diary8.com/
  8. */
  9. namespace echowx\utils;
  10. class HttpCurl {
  11. /**
  12. * 模拟GET请求
  13. *
  14. * @param string $url
  15. * @param string $data_type
  16. *
  17. * @return mixed
  18. *
  19. * Examples:
  20. * ```
  21. * HttpCurl::get('http://api.example.com/?a=123&b=456', 'json');
  22. * ```
  23. */
  24. static public function get($url, $data_type='text')
  25. {
  26. $cl = curl_init();
  27. if(stripos($url, 'https://') !== FALSE) {
  28. curl_setopt($cl, CURLOPT_SSL_VERIFYPEER, FALSE);
  29. curl_setopt($cl, CURLOPT_SSL_VERIFYHOST, FALSE);
  30. curl_setopt($cl, CURLOPT_SSLVERSION, 1);
  31. }
  32. curl_setopt($cl, CURLOPT_URL, $url);
  33. curl_setopt($cl, CURLOPT_RETURNTRANSFER, 1 );
  34. $content = curl_exec($cl);
  35. $status = curl_getinfo($cl);
  36. curl_close($cl);
  37. if (isset($status['http_code']) && $status['http_code'] == 200) {
  38. if ($data_type == 'json') {
  39. $content = json_decode($content, true);
  40. }
  41. return $content;
  42. } else {
  43. return FALSE;
  44. }
  45. }
  46. /**
  47. * 模拟POST请求
  48. *
  49. * @param string $url
  50. * @param array $fields
  51. * @param string $data_type
  52. *
  53. * @return mixed
  54. *
  55. * Examples:
  56. * ```
  57. * HttpCurl::post('http://api.example.com/?a=123', array('abc'=>'123', 'efg'=>'567'), 'json');
  58. * HttpCurl::post('http://api.example.com/', '这是post原始内容', 'json');
  59. * 文件post上传
  60. * HttpCurl::post('http://api.example.com/', array('abc'=>'123', 'file1'=>'@/data/1.jpg'), 'json');
  61. * ```
  62. */
  63. static public function post($url, $fields, $data_type='text')
  64. {
  65. $cl = curl_init();
  66. if(stripos($url, 'https://') !== FALSE) {
  67. curl_setopt($cl, CURLOPT_SSL_VERIFYPEER, FALSE);
  68. curl_setopt($cl, CURLOPT_SSL_VERIFYHOST, FALSE);
  69. curl_setopt($cl, CURLOPT_SSLVERSION, 1);
  70. }
  71. if (class_exists('\CURLFile')) {
  72. if (isset($fields['media'])) {
  73. $fields = array('media' => new \CURLFile(realpath(ltrim($fields['media'], '@'))));
  74. }
  75. } else {
  76. if (defined('CURLOPT_SAFE_UPLOAD')) {
  77. curl_setopt($cl, CURLOPT_SAFE_UPLOAD, false);
  78. }
  79. }
  80. curl_setopt($cl, CURLOPT_URL, $url);
  81. curl_setopt($cl, CURLOPT_RETURNTRANSFER, 1 );
  82. curl_setopt($cl, CURLOPT_POST, true);
  83. curl_setopt($cl, CURLOPT_POSTFIELDS, $fields);
  84. $content = curl_exec($cl);
  85. $status = curl_getinfo($cl);
  86. curl_close($cl);
  87. if (isset($status['http_code']) && $status['http_code'] == 200) {
  88. if ($data_type == 'json') {
  89. $content = json_decode($content, true);
  90. }
  91. return $content;
  92. } else {
  93. return FALSE;
  94. }
  95. }
  96. }