php专区

 首页 > php专区 > PHP高级 > 面向对象MVC > php curl封装类使用例子

php curl封装类使用例子

分享到:
【字体:
导读:
          下面整理两个php curl封装类使用例子,这两个函数可以让我们非常的方便的使用php curl相关函数,下面我们一起来看看吧. 使用函数之前我们要需要把php curl模块打开(libeay32.dll,ssleay32.dll,ph...

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

cul处理类,代码如下:

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

例子2,代码如下:

  1. //curl类 
  2. class Curl 
  3.     function Curl(){ 
  4.         return true; 
  5.     } 
  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.         } 
  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.       
  61.     function post($url$fields$userAgent = ''$httpHeaders = ''$username = ''$password = ''){ 
  62.         $ret = Curl::execute('POST'$url$fields$userAgent$httpHeaders$username$password); 
  63.         if(false === $ret){ 
  64.             return false; 
  65.         } 
  66.         if(is_array($ret)){ 
  67.             return false; 
  68.         } 
  69.         return $ret
  70.     } 
  71.       
  72.     function get($url$userAgent = ''$httpHeaders = ''$username = ''$password = ''){ 
  73.         $ret = Curl::execute('GET'$url''$userAgent$httpHeaders$username$password); 
  74.         if(false === $ret){ 
  75.             return false; 
  76.         } 
  77.         if(is_array($ret)){ 
  78.             return false; 
  79.         } 
  80.         return $ret
  81.     } 
  82.       
  83.     function create(){ 
  84.         $ch = null; 
  85.         if(!function_exists('curl_init')){ 
  86.             return false; 
  87.         } 
  88.         $ch = curl_init(); 
  89.         if(!is_resource($ch)){ 
  90.             return false; 
  91.         } 
  92.         return $ch
  93.     } 
  94. ?> 

用法,GET用法:

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

POST用法:

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

 
分享到:
php 魔术函数集中营1。__construct() 实...
1。__construct() 实例化对象时被调用,当__construct和以类名为函数名的函数同时存在时,__construct将被调用,另一个不被调用。 2。__destruct() 当删除一个对象或对象操作终止时被调用。 3。__call() 对象调用某个方法,若方法存在,则直接调用;若不存在,则会去调用__call函数。 4。__get() ...
让PHP开发者事半功倍的十大技巧
1、如何正确的创建一个网站的Index页面 创建每一个网站时,建立网站的index页面是首要做的事情之一。如果你是一个PHP新手,在编写index页面时典型的做法是只对index页面所需的内容进行编程,其它链接创建另一个页面。不过,如果想学习一种更高效的方式来实现PHP编程,可以采用“index.php?page=home”模式,许多网站都...
  •         php迷,一个php技术的分享社区,专属您自己的技术摘抄本、收藏夹。
  • 在这里……