A case of PHP Curl encapsulation class Code

Source: Internet
Author: User
  1. /**
  2. * Curl Package Class
  3. * by bbs.it-home.org
  4. */
  5. Class Curl_class
  6. {
  7. function Curl_class ()
  8. {
  9. return true;
  10. }
  11. function Execute ($method, $url, $fields = ', $userAgent = ', $httpHeaders = ', $username = ', $password = ')
  12. {
  13. $ch = Curl_class::create ();
  14. if (false = = = $ch)
  15. {
  16. return false;
  17. }
  18. if (is_string ($url) && strlen ($url))
  19. {
  20. $ret = curl_setopt ($ch, Curlopt_url, $url);
  21. }
  22. Else
  23. {
  24. return false;
  25. }
  26. Whether to display header information
  27. curl_setopt ($ch, Curlopt_header, false);
  28. //
  29. curl_setopt ($ch, Curlopt_returntransfer, true);
  30. if ($username! = ")
  31. {
  32. curl_setopt ($ch, Curlopt_userpwd, $username. ':' . $password);
  33. }
  34. $method = Strtolower ($method);
  35. if (' post ' = = $method)
  36. {
  37. curl_setopt ($ch, Curlopt_post, true);
  38. if (Is_array ($fields))
  39. {
  40. $sets = Array ();
  41. foreach ($fields as $key = $val)
  42. {
  43. $sets [] = $key. '=' . UrlEncode ($val);
  44. }
  45. $fields = Implode (' & ', $sets);
  46. }
  47. curl_setopt ($ch, Curlopt_postfields, $fields);
  48. }
  49. else if (' put ' = = $method)
  50. {
  51. curl_setopt ($ch, Curlopt_put, true);
  52. }
  53. curl_setopt ($ch, curlopt_progress, true);
  54. curl_setopt ($ch, Curlopt_verbose, true);
  55. curl_setopt ($ch, Curlopt_mute, false);
  56. curl_setopt ($ch, Curlopt_timeout, 3);//sets the curl timeout in seconds, for example, post the message 3 seconds after the automatic end of the run.
  57. if (strlen ($userAgent))
  58. {
  59. curl_setopt ($ch, curlopt_useragent, $userAgent);
  60. }
  61. if (Is_array ($httpHeaders))
  62. {//bbs.it-home.org
  63. curl_setopt ($ch, Curlopt_httpheader, $httpHeaders);
  64. }
  65. $ret = curl_exec ($ch);
  66. if (Curl_errno ($ch))
  67. {
  68. Curl_close ($ch);
  69. Return Array (Curl_error ($ch), Curl_errno ($ch));
  70. }
  71. Else
  72. {
  73. Curl_close ($ch);
  74. if (!is_string ($ret) | |!strlen ($RET))
  75. {
  76. return false;
  77. }
  78. return $ret;
  79. }
  80. }
  81. Function post ($url, $fields, $userAgent = ', $httpHeaders = ', $username = ', $password = ')
  82. {
  83. $ret = Curl_class::execute (' POST ', $url, $fields, $userAgent, $httpHeaders, $username, $password);
  84. if (false = = = $ret)
  85. {
  86. return false;
  87. }
  88. if (Is_array ($ret))
  89. {
  90. return false;
  91. }
  92. return $ret;
  93. }
  94. function Get ($url, $userAgent = ', $httpHeaders = ', $username = ', $password = ')
  95. {
  96. $ret = Curl_class::execute (' GET ', $url, ', $userAgent, $httpHeaders, $username, $password);
  97. if (false = = = $ret)
  98. {
  99. return false;
  100. }
  101. if (Is_array ($ret))
  102. {
  103. return false;
  104. }
  105. return $ret;
  106. }
  107. function Create ()
  108. {
  109. $ch = null;
  110. if (!function_exists (' Curl_init '))
  111. {
  112. return false;
  113. }
  114. $ch = Curl_init ();
  115. if (!is_resource ($ch))
  116. {
  117. return false;
  118. }
  119. return $ch;
  120. }
  121. }
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.