欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

PHP CURL封装类代码一例

程序员文章站 2022-05-14 13:31:01
...
  1. /**
  2. * CURL封装类
  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. //是否显示头部信息
  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);//设置curl超时秒数,例如将信息POST出去3秒钟后自动结束运行。
  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. }
复制代码