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类的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: oc 画一个圆弧_UG建模一个蜗杆的方法
- 下一篇: python选择某一行_Python常用