国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > 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. 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.survivalescaperooms.com/’);

POST用法:

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 台湾省| 沾化县| 临西县| 迭部县| 灌阳县| 鄢陵县| 子长县| 张家口市| 淮阳县| 安化县| 惠安县| 白山市| 黎平县| SHOW| 新田县| 和田市| 磐石市| 丰顺县| 昆明市| 余庆县| 文安县| 太湖县| 永修县| 武宣县| 武胜县| 鸡东县| 淅川县| 洞头县| 五寨县| 舟曲县| 德保县| 鹤岗市| 墨脱县| 嘉定区| 吉隆县| 浮山县| 法库县| 珲春市| 许昌县| 蕲春县| 鄱阳县|