首页 > 后端开发 > php教程 > PHP CURL封装类代码一例

PHP CURL封装类代码一例

WBOY
发布: 2016-07-25 08:55:50
原创
914 人浏览过
  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. }
复制代码


来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板