当前位置:首页 > PHP教程 > php面向对象 > 列表

php curl封装类使用例子

发布:smiling 来源: PHP粉丝网  添加日期:2014-09-12 13:43:16 浏览: 评论:0 

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

例子2,代码如下:

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

用法,GET用法:

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

POST用法:

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

Tags: curl封装类 curl使用

分享到: