Home  >  Article  >  Backend Development  >  PHP http request processing class

PHP http request processing class

WBOY
WBOYOriginal
2016-07-25 08:42:031088browse

[PHP]代码

  1. /**
  2. * http request processing class (encapsulated based on CURL)
  3. *
  4. * @author Xiwei Ye
  5. * @version $Id$
  6. */
  7. class cls_http_request
  8. {
  9. /**
  10. * Get method request (curl)
  11. *
  12. * @param string $url requested url
  13. * @param integer $timeout timeout (s)
  14. * @return string (request successful) | false (request failed)
  15. */
  16. public static function curl_get($url, $timeout = 1)
  17. {
  18. $ch = curl_init();
  19. curl_setopt($ch, CURLOPT_URL, $url);
  20. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  21. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  22. $result = curl_exec($ch);
  23. curl_close($ch);
  24. if (is_string($result) && strlen($result))
  25. {
  26. return $result;
  27. }
  28. else
  29. {
  30. return false;
  31. }
  32. }
  33. /**
  34. * Post request
  35. *
  36. * @param string $url The requested url
  37. * @param array $data The requested parameter array (associative array)
  38. * @param integer $timeout Timeout time (s)
  39. * @return string( Request successful) | false (request failed)
  40. */
  41. public static function curl_post($url, $data, $timeout = 2)
  42. {
  43. $ch = curl_init();
  44. curl_setopt($ch, CURLOPT_URL, $url);
  45. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  46. curl_setopt($ch, CURLOPT_POST, 1);
  47. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  48. curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
  49. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  50. $result = curl_exec($ch);
  51. curl_close($ch);
  52. if (is_string($result) && strlen($result))
  53. {
  54. return $result;
  55. }
  56. else
  57. {
  58. return false;
  59. }
  60. }
  61. /**
  62. * Multiple url parallel requests
  63. *
  64. * @param array $urls url array
  65. * @param integer $timeout timeout time (s)
  66. * @return array $res return result
  67. */
  68. public static function curl_get_urls($urls, $timeout = 1)
  69. {
  70. $mh=curl_multi_init();
  71. $chs=array();
  72. foreach($urls as $url)
  73. {
  74. $ch=curl_init();
  75. curl_setopt($ch,CURLOPT_URL,$url);
  76. curl_setopt($ch,CURLOPT_HEADER,false);
  77. curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
  78. curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,1);
  79. curl_setopt($ch,CURLOPT_TIMEOUT,$timeout);
  80. curl_multi_add_handle($mh,$ch);
  81. $chs[]=$ch;
  82. }
  83. $active=null;
  84. do {
  85. $mrc=curl_multi_exec($mh,$active);
  86. }while($mrc == CURLM_CALL_MULTI_PERFORM);
  87. while($active && $mrc == CURLM_OK)
  88. {
  89. if(curl_multi_select($mh) != -1)
  90. {
  91. do{
  92. $mrc=curl_multi_exec($mh,$active);
  93. }while($mrc == CURLM_CALL_MULTI_PERFORM);
  94. }
  95. }
  96. $res=array();
  97. foreach($chs as $ch)
  98. {
  99. $res[]=curl_multi_getcontent($ch);
  100. curl_multi_remove_handle($mh,$ch);
  101. }
  102. curl_multi_close($mh);
  103. return $res;
  104. }
  105. }
复制代码
PHP, http


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn