HttpHelper.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /**
  3. * HTTP 请求辅助类
  4. */
  5. class HttpHelper {
  6. const METHOD_GET = 'GET';
  7. const METHOD_POST = 'POST';
  8. private $_connectTimeout = 60;
  9. private $_timeout = 60;
  10. /**
  11. * Http post请求
  12. * @param type $url http url address
  13. * @param type $data post params name => value
  14. */
  15. public function post($url, $data = array()){
  16. $queryString = $this->buildHttpQueryString($data, self::METHOD_POST);
  17. $response = $this->makeHttpRequest($url, self::METHOD_POST,$queryString);
  18. return $response;
  19. }
  20. /**
  21. * http get 请求
  22. * @param type $url http url address
  23. * @param type $data get params name => value
  24. */
  25. public function get($url,$data = array()) {
  26. if(!empty($data)){
  27. $url .= "?" . $this->buildHttpQueryString($data, self::METHOD_GET);
  28. }
  29. $response = $this->makeHttpRequest($url, self::METHOD_GET);
  30. return $response;
  31. }
  32. /**
  33. * 构造并发送一个http请求
  34. * @param type $url
  35. * @param type $method
  36. * @param type $postFields
  37. * @return type
  38. */
  39. public function makeHttpRequest($url,$method,$postFields = null) {
  40. $ch = curl_init();
  41. curl_setopt($ch, CURLOPT_URL, $url);
  42. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  43. if(self::METHOD_POST == $method){
  44. curl_setopt($ch, CURLOPT_POST, 1);
  45. if(!empty($postFields)){
  46. curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
  47. }
  48. }
  49. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->_connectTimeout);
  50. curl_setopt($ch, CURLOPT_TIMEOUT, $this->_timeout);
  51. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  52. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  53. $result = curl_exec($ch);
  54. curl_close($ch);
  55. return $result;
  56. }
  57. /**
  58. * 构造http请求的查询字符串
  59. * @param array $params
  60. * @param type $method
  61. * @return string
  62. */
  63. public function buildHttpQueryString(array $params, $method = self::METHOD_GET) {
  64. if(empty($params)){
  65. return '';
  66. }
  67. if(self::METHOD_GET == $method){
  68. $keys = array_keys($params);
  69. $values = $this->urlEncode(array_values($params));
  70. $params = array_combine($keys, $values);
  71. }
  72. $fields = array();
  73. foreach ($params as $key => $value) {
  74. $fields[] = $key . '=' . $value;
  75. }
  76. return implode('&',$fields);
  77. }
  78. /**
  79. * url encode 函数
  80. * @param type $item 数组或者字符串类型
  81. * @return type
  82. */
  83. public function urlEncode($item) {
  84. if(is_array($item)){
  85. return array_map(array(&$this,'urlEncode'), $item);
  86. }
  87. return rawurlencode($item);
  88. }
  89. /**
  90. * url decode 函数
  91. * @param type $item 数组或者字符串类型
  92. * @return type
  93. */
  94. public function urlDecode($item){
  95. if(is_array($item)){
  96. return array_map(array(&$this,'urlDecode'), $item);
  97. }
  98. return rawurldecode($item);
  99. }
  100. }