日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

php-curl-class,一个简单PHP CURL类

發布時間:2024/7/19 php 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php-curl-class,一个简单PHP CURL类 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

這里要說明一下...這個類的形成是參考了晚上前輩們的代碼加上我自己的理解見解而集成的...前輩們的代碼出處已經忘記了

我在這里感謝這些前輩們給我的啟發...希望這個類能給大家帶來幫助...如果有不足的地方...請大家多多指點指點

這是一個PHP CURL的類

public $cookieFile; ? ? ? ? ? ? ? ? ? ? ? //cookie存放路徑

public $loginUrl; ? ? ? ? ? ? ? ? ? ? ? ?//登錄鏈接

public $loginFields; ? ? ? ? ? ? ? ? ? ?//登錄參數

public $targetUrl; ? ? ? ? ? ? ? ? ? ?//目標地址

public $targetFields; ? ? ? ? ? ? ? ?//目標參數

這里是要說明的幾個參數

$cookieFile保存的是CURL獲取回來的COOKIE的文件存放的位置

$loginUrl;模擬登陸時用的鏈接

$loginFields;模擬登錄時用到的參數 如賬號密碼等

$targetUrl;模擬提交,獲取時用到的地址

$targetFields;模擬提交時用到的參數

用法簡介

$curl=new curl($cookieFile); //初始化時載入COOKIE保存位置

//模擬登錄

$curl->loginUrl="http://www.xxx.com"; ? ? ? ?//登錄鏈接

$curl->loginFields="username=xxx&pwd=xxx"; ? //登錄參數

$curl->curlLogin($ref,$header); ? ?//$ref 偽造來源地址 $header 偽造頭部和IP信息

//模擬獲取

$curl->targetUrl="http://www.xxx.com"; ? ? ? ?//目標鏈接

$curl->curGet($ref,$header); ? ? ? ? ? ? ? ?//$ref 偽造來源地址 $header 偽造頭部和IP信息

//模擬提交

$curl->targetUrl="http://www.xxx.com"; ? ? ? ?//目標鏈接

$curl->targetFields="username=xxx&pwd=xxx"; ? //提交參數

$curl->curlPost($ref,$header); ? ?//$ref 偽造來源地址 $header 偽造頭部和IP信息

====================Curl.class.php PHP代碼如下=====================================================================

class curl{

public $cookieFile; ? ? ? ? ? ? ? ? ? ?//cookie存放路徑

public $loginUrl; ? ? ? ? ? ? ? ? ? ?//登錄鏈接

public $loginFields; ? ? ? ? ? ? ? ?//參數

public $targetUrl; ? ? ? ? ? ? ? ? ? ?//目標地址

public $targetFields; ? ? ? ? ? ? ? ?//目標參數

function __construct(){

$this->cookieFile='cookie.txt';

}

//模擬登錄

function curlLogin($ref="",$head=""){

if($ref==""){

$referer="http://www.baidu.com/";

}

else{

$referer=$ref;

}

if($head==""){

$header=$this->randIP();

}

else{

$header=array_merge($head,$this->randIP());

}

$curl = curl_init(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // 啟動一個CURL會話

curl_setopt($curl, CURLOPT_URL, $this->loginUrl); ? ? ? ? ? ? ? ? ? ? // 要訪問的地址

curl_setopt($curl, CURLOPT_HTTPHEADER , $header ) ; ? ? ? ? ? ? ? ? ? ?// 偽造訪問IP

curl_setopt($curl, CURLOPT_REFERER, $referer); ? ? ? ? ? ? ? ? ? ? ? ? // 偽造來路

curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); ? ? ? ? ? ? ? ? ? ? ? ? // 對認證證書來源的檢查

curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1); ? ? ? ? ? ? ? ? ? ? ? ? // 從證書中檢查SSL加密算法是否存在

curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模擬用戶使用的瀏覽器

curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); ? ? ? ? ? ? ? ? ? ? ? ? // 使用自動跳轉

curl_setopt($curl, CURLOPT_AUTOREFERER, 1); ? ? ? ? ? ? ? ? ? ? ? ? // 自動設置Referer

curl_setopt($curl, CURLOPT_POST, 1); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // 發送一個常規的Post請求

curl_setopt($curl, CURLOPT_POSTFIELDS, $this->loginFields); ? ? ? ? // Post提交的數據包

curl_setopt($curl, CURLOPT_COOKIEJAR, $this->cookieFile); ? ? ? ? ? ? // 存放Cookie信息的文件名稱

curl_setopt($curl, CURLOPT_COOKIEFILE, $this->cookieFile); ? ? ? ? ? ? // 讀取上面所儲存的Cookie信息

curl_setopt($curl, CURLOPT_TIMEOUT, 300); ? ? ? ? ? ? ? ? ? ? ? ? ? ? // 設置超時限制防止死循環

curl_setopt($curl, CURLOPT_HEADER, 0); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // 顯示返回的Header區域內容

curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); ? ? ? ? ? ? ? ? ? ? ? ? // 獲取的信息以文件流的形式返回

$tmpInfo = curl_exec($curl); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // 執行操作

if (curl_errno($curl)) {

echo 'Errno'.curl_error($curl);

}

curl_close($curl); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // 關閉CURL會話

return $tmpInfo; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // 返回數據

}

//模擬獲取數據

function curlGet($ref="",$head=""){

if($ref==""){

$referer="http://www.baidu.com/";

}

else{

$referer=$ref;

}

if($head==""){

$header=$this->randIP();

}

else{

$header=array_merge($head,$this->randIP());

}

$curl = curl_init(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // 啟動一個CURL會話

curl_setopt($curl, CURLOPT_URL, $this->targetUrl); ? ? ? ? ? ? ? ? ? ? // 要訪問的地址

curl_setopt($curl, CURLOPT_HTTPHEADER , $header); ? ? ? ? ? ? ? ? ? ? // 偽造訪問IP

curl_setopt($curl, CURLOPT_REFERER, $referer); ? ? ? ? ? ? ? ? ? ? ? ? // 偽造來路

curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); ? ? ? ? ? ? ? ? ? ? ? ? // 對認證證書來源的檢查

curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1); ? ? ? ? ? ? ? ? ? ? ? ? // 從證書中檢查SSL加密算法是否存在

curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模擬用戶使用的瀏覽器

curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); ? ? ? ? ? ? ? ? ? ? ? ? // 使用自動跳轉

curl_setopt($curl, CURLOPT_AUTOREFERER, 1); ? ? ? ? ? ? ? ? ? ? ? ? // 自動設置Referer

curl_setopt($curl, CURLOPT_HTTPGET, 1); ? ? ? ? ? ? ? ? ? ? ? ? ? ? // 發送一個常規的GET請求

curl_setopt($curl, CURLOPT_COOKIEFILE, $this->cookieFile); ? ? ? ? ? ? // 讀取上面所儲存的Cookie信息

curl_setopt($curl, CURLOPT_TIMEOUT, 300); ? ? ? ? ? ? ? ? ? ? ? ? ? ? // 設置超時限制防止死循環

curl_setopt($curl, CURLOPT_HEADER, 0); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // 顯示返回的Header區域內容

curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); ? ? ? ? ? ? ? ? ? ? ? ? // 獲取的信息以文件流的形式返回

$tmpInfo = curl_exec($curl); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // 執行操作

if (curl_errno($curl)) {

echo 'Errno'.curl_error($curl);

}

curl_close($curl); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // 關閉CURL會話

return $tmpInfo; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // 返回數據

}

//模擬提交數據

function curlPost($ref="",$head=""){

if($ref==""){

$referer="http://www.baidu.com/";

}

else{

$referer=$ref;

}

if($head==""){

$header=$this->randIP();

}

else{

$header=array_merge($head,$this->randIP());

}

$curl = curl_init(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // 啟動一個CURL會話

curl_setopt($curl, CURLOPT_URL, $this->targetUrl); ? ? ? ? ? ? ? ? ? ? // 要訪問的地址

curl_setopt($curl, CURLOPT_HTTPHEADER , $header); ? ? ? ? ? ? ? ? ? ? ?// 偽造訪問IP

curl_setopt($curl, CURLOPT_REFERER, $referer); ? ? ? ? ? ? ? ? ? ? ?// 偽造來路

curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); ? ? ? ? ? ? ? ? ? ? ? ? // 對認證證書來源的檢查

curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1); ? ? ? ? ? ? ? ? ? ? ? ? // 從證書中檢查SSL加密算法是否存在

curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模擬用戶使用的瀏覽器

curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); ? ? ? ? ? ? ? ? ? ? ? ? // 使用自動跳轉

curl_setopt($curl, CURLOPT_AUTOREFERER, 1); ? ? ? ? ? ? ? ? ? ? ? ? // 自動設置Referer

curl_setopt($curl, CURLOPT_POST, 1); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // 發送一個常規的Post請求

curl_setopt($curl, CURLOPT_POSTFIELDS, $this->targetFields); ? ? ? ? // Post提交的數據包

curl_setopt($curl, CURLOPT_COOKIEFILE, $this->cookieFile); ? ? ? ? ? ? // 讀取上面所儲存的Cookie信息

curl_setopt($curl, CURLOPT_TIMEOUT, 300); ? ? ? ? ? ? ? ? ? ? ? ? ? ? // 設置超時限制防止死循環

curl_setopt($curl, CURLOPT_HEADER, 0); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // 顯示返回的Header區域內容

curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); ? ? ? ? ? ? ? ? ? ? ? ? // 獲取的信息以文件流的形式返回

$tmpInfo = curl_exec($curl); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // 執行操作

if (curl_errno($curl)) {

echo 'Errno'.curl_error($curl);

}

curl_close($curl); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // 關閉CURL會話

return $tmpInfo; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // 返回數據

}

//隨機生成IP

function randIP(){

$ip_long = array(

array('607649792', '608174079'), //36.56.0.0-36.63.255.255

array('1038614528', '1039007743'), //61.232.0.0-61.237.255.255

array('1783627776', '1784676351'), //106.80.0.0-106.95.255.255

array('2035023872', '2035154943'), //121.76.0.0-121.77.255.255

array('2078801920', '2079064063'), //123.232.0.0-123.235.255.255

array('-1950089216', '-1948778497'), //139.196.0.0-139.215.255.255

array('-1425539072', '-1425014785'), //171.8.0.0-171.15.255.255

array('-1236271104', '-1235419137'), //182.80.0.0-182.92.255.255

array('-770113536', '-768606209'), //210.25.0.0-210.47.255.255

array('-569376768', '-564133889'), //222.16.0.0-222.95.255.255

);

$rand_key = mt_rand(0, 9);

$ip= long2ip(mt_rand($ip_long[$rand_key][0], $ip_long[$rand_key][1]));

$headers['CLIENT-IP'] = $ip;

$headers['X-FORWARDED-FOR'] = $ip;

$headerArr = array();

foreach( $headers as $n => $v ) {

$headerArr[] = $n .':' . $v;

}

return $headerArr;

}

}

總結

以上是生活随笔為你收集整理的php-curl-class,一个简单PHP CURL类的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。