HTTP Request Processing Classes

Source: Internet
Author: User
http://blog.qita.in/?post=496
  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 mode request (Curl)
  11. *
  12. * @param string $url the requested URL
  13. * @param integer $timeout time-out (s)
  14. * @return String (Request succeeded) | 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 Mode request
  35. *
  36. * @param string $url the requested URL
  37. * @param array of parameters for array $data request (associative array)
  38. * @param integer $timeout time-out (s)
  39. * @return String (Request succeeded) | 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
  65. * @param integer $timeout time-out (s)
  66. * @return Array $res return results
  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. }
Copy Code
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.