php curl封装类使用例子

下面整理两个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. 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. 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. function get($url, $userAgent = '', $httpHeaders = '', $username = '', $password = ''){
  72. $ret = Curl::execute('GET', $url, '', $userAgent, $httpHeaders, $username, $password);
  73. if(false === $ret){
  74. return false;
  75. }
  76. if(is_array($ret)){
  77. return false;
  78. }
  79. return $ret;
  80. }
  81. function create(){
  82. $ch = null;
  83. if(!function_exists('curl_init')){
  84. return false;
  85. }
  86. $ch = curl_init();
  87. if(!is_resource($ch)){
  88. return false;
  89. }
  90. return $ch;
  91. }
  92. }
  93. ?>

用法,GET用法:

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

POST用法:

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