首页 > 后端开发 > php教程 > Curl封装类Curl.class.php及调用方法

Curl封装类Curl.class.php及调用方法

WBOY
发布: 2016-07-25 08:54:35
原创
1051 人浏览过
  1. //curl类
  2. class Curl
  3. {
  4. function Curl(){
  5. return true;
  6. }
  7. function execute($method, $url, $fields='', $userAgent='', $httpHeaders='', $username='', $password=''){
  8. $ch = Curl::create();
  9. if(false === $ch){
  10. return false;
  11. }
  12. if(is_string($url) && strlen($url)){
  13. $ret = curl_setopt($ch, CURLOPT_URL, $url);
  14. }else{
  15. return false;
  16. }
  17. //是否显示头部信息
  18. curl_setopt($ch, CURLOPT_HEADER, false);
  19. //
  20. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  21. if($username != ''){
  22. curl_setopt($ch, CURLOPT_USERPWD, $username . ':' . $password);
  23. }
  24. $method = strtolower($method);
  25. if('post' == $method){
  26. curl_setopt($ch, CURLOPT_POST, true);
  27. if(is_array($fields)){
  28. $sets = array();
  29. foreach ($fields AS $key => $val){
  30. $sets[] = $key . '=' . urlencode($val);
  31. }
  32. $fields = implode('&',$sets);
  33. }
  34. curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
  35. }else if('put' == $method){
  36. curl_setopt($ch, CURLOPT_PUT, true);
  37. } // bbs.it-home.org
  38. //curl_setopt($ch, CURLOPT_PROGRESS, true);
  39. //curl_setopt($ch, CURLOPT_VERBOSE, true);
  40. //curl_setopt($ch, CURLOPT_MUTE, false);
  41. curl_setopt($ch, CURLOPT_TIMEOUT, 10);//设置curl超时秒数
  42. if(strlen($userAgent)){
  43. curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
  44. }
  45. if(is_array($httpHeaders)){
  46. curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeaders);
  47. }
  48. $ret = curl_exec($ch);
  49. if(curl_errno($ch)){
  50. curl_close($ch);
  51. return array(curl_error($ch), curl_errno($ch));
  52. }else{
  53. curl_close($ch);
  54. if(!is_string($ret) || !strlen($ret)){
  55. return false;
  56. }
  57. return $ret;
  58. }
  59. }
  60. function post($url, $fields, $userAgent = '', $httpHeaders = '', $username = '', $password = ''){
  61. $ret = Curl::execute('POST', $url, $fields, $userAgent, $httpHeaders, $username, $password);
  62. if(false === $ret){
  63. return false;
  64. }
  65. if(is_array($ret)){
  66. return false;
  67. }
  68. return $ret;
  69. }
  70. function get($url, $userAgent = '', $httpHeaders = '', $username = '', $password = ''){
  71. $ret = Curl::execute('GET', $url, '', $userAgent, $httpHeaders, $username, $password);
  72. if(false === $ret){
  73. return false;
  74. }
  75. if(is_array($ret)){
  76. return false;
  77. }
  78. return $ret;
  79. }
  80. function create(){
  81. $ch = null;
  82. if(!function_exists('curl_init')){
  83. return false;
  84. }
  85. $ch = curl_init();
  86. if(!is_resource($ch)){
  87. return false;
  88. }
  89. return $ch;
  90. }
  91. }
  92. ?>
复制代码

1,GET用法:

  1. $curl = new Curl();
  2. $curl->get(‘http://bbs.it-home.org/’);
复制代码

2,POST用法

  1. $curl = new Curl();
  2. $curl->get(‘http://bbs.it-home.org/’, ‘p=1&time=0′);
复制代码


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