首页 > 语言 > PHP > 正文

php curl封装类使用例子

2024-09-04 11:43:46
字体:大 中 小
来源:转载
供稿:网友

下面整理两个php curl封装类使用例子,这两个函数可以让我们非常的方便的使用php curl相关函数,下面我们一起来看看吧.

使用函数之前我们要需要把php curl模块打开(libeay32.dll,ssleay32.dll,php5ts.dll,php_curl.dll)

开启php curl函数库的步骤:

1).去掉windows/php.ini 文件里;extension=php_curl.dll前面的; /*用 echo phpinfo();查看php.ini的路径*/

2).把php5/libeay32.dll,ssleay32.dll复制到系统目录windows/下

3).重启apache

php curl,代码如下:

  1. <?php 
  2. include_once('curl.class.php'); 
  3. $aa =new Curl(''); 
  4.  $curlOptions = array( 
  5.  CURLOPT_URL => "http://ww.ww.ww/addTicket.jsp", //访问URL 
  6.  CURLOPT_RETURNTRANSFER => true, //获取结果作为字符串返回 
  7.  CURLOPT_REFERER => "ww.ww.ww/zw2", 
  8.  CURLOPT_HTTPHEADER => array('X-FORWARDED-FOR:139.197.14.19', 'CLIENT-IP:127.0.0.1','Proxy-Client-IP:139.197.14.19','WL-Proxy-Client-IP:139.197.14.19' ), 
  9.  CURLOPT_HEADER => 1, //获取返回头信息 
  10.  //CURLOPT_SSL_VERIFYPEER => false, //支持SSL加密 
  11.  CURLOPT_POST => true, //发送时带有POST参数 
  12.  CURLOPT_POSTFIELDS => 'ids=897&Submit=%E6%8A%95%E7%A5%A8', //请求的POST参数字符串 
  13.  CURLOPT_TIMEOUT => $aa->timeout //等待响应的时间 
  14.  ); 
  15.  echo $aa->getResponseText($curlOptions); 
  16. ?> 

cul处理类,代码如下:

  1. <?php 
  2. class Curl 
  3. { 
  4. public $cookieFile; 
  5. public $timeout = 160; 
  6. Public function __construct($dir){ 
  7. $this->cookieFile = $this->getTemporaryCookieFileName($dir); 
  8. } 
  9. /** 
  10. * 设置CURL参数并发送请求,获取响应内容 
  11. * @access private 
  12. * @param $curlOptions array curl设置参数数组 
  13. * @return string|false 访问成功,按字符串形式返回获取的信息;否则返回false 
  14. */ 
  15. public function getResponseText($curlOptions) { 
  16. /* 设置CURLOPT_RETURNTRANSFER为true */ 
  17. if(!isset($curlOptions[CURLOPT_RETURNTRANSFER]) || $curlOptions[CURLOPT_RETURNTRANSFER] == false) { 
  18. $curlOptions[CURLOPT_RETURNTRANSFER] = true; 
  19. } 
  20. /* 初始化curl模块 */ 
  21. $curl = curl_init(); 
  22. /* 设置curl选项 */ 
  23. curl_setopt_array($curl, $curlOptions); 
  24. /* 发送请求并获取响应信息 */ 
  25. $responseText = ''; 
  26. try { 
  27. $responseText = curl_exec($curl); 
  28. if(($errno = curl_errno($curl)) != CURLM_OK) { 
  29. $errmsg = curl_error($curl); 
  30. throw new Exception($errmsg, $errno); 
  31. } 
  32. } catch (Exception $e) { 
  33. //exceptionDisposeFunction($e); 
  34. //print_r($e); 
  35. $responseText = false; 
  36. } 
  37. /* 关闭curl模块 */ 
  38. curl_close($curl); 
  39. /* 返回结果 */ 
  40. return $responseText; 
  41. } 
  42. /** 
  43. * 将Unicode字符串(u0000)转化为utf-8字符串,工具函数 
  44. * @access private 
  45. * @static 
  46. * @param $string string Unicode字符串 
  47. * @return string utf-8字符串 
  48. */ 
  49. public function unicodeToUtf8($string) { 
  50. $string = str_replace('u', '', strtolower($string)); 
  51. $length = strlen($string) / 4; 
  52. $stringResult = ''; 
  53. for($i = 0; $i < $length; $i++) { 
  54. $charUnicodeHex = substr($string, $i * 4, 4); 
  55. $unicodeCode = hexdec($charUnicodeHex); 
  56. $utf8Code = ''; 
  57. if($unicodeCode < 128) { 
  58. $utf8Code = chr($unicodeCode); 
  59. } else if($unicodeCode < 2048) { 
  60. $utf8Code .= chr(192 + (($unicodeCode - ($unicodeCode % 64)) / 64)); 
  61. $utf8Code .= chr(128 + ($unicodeCode % 64)); 
  62. } else { 
  63. $utf8Code .= chr(224 + (($unicodeCode - ($unicodeCode % 4096)) / 4096)); 
  64. $utf8Code .= chr(128 + ((($unicodeCode % 4096) - ($unicodeCode % 64)) / 64)); 
  65. $utf8Code .= chr(128 + ($unicodeCode % 64)); 
  66. } 
  67. $stringResult .= $utf8Code; 
  68. } 
  69. return $stringResult; 
  70. } 
  71. private function getTemporaryCookieFileName($dir='.') { 
  72. return (str_replace("", '/', tempnam($dir, 'tmp'))); 
  73. } 
  74. } 
  75. ?> 

例子2,代码如下:

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

用法,GET用法:

$curl = new Curl();$curl->get(‘http://www.Vevb.com/’);

POST用法:

$curl = new Curl();$curl->get(‘http://www.Vevb.com/’,‘p=1&time=0′);

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表