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

例子2,代码如下:

  1. //curl类 
  2. class Curl 
  3. { 
  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. } 
  96. ?> 

用法,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技术的分享社区,专属您自己的技术摘抄本、收藏夹。
  • 在这里……