模擬瀏覽器get和post數據需要經常用到的類,
在這里收藏了幾個不錯的方法
方法一
<?php
define ( 'IS_PROXY', true ); //是否啟用代理
/* cookie文件 */
$cookie_file = dirname ( __FILE__ ) . "/cookie_" . md5 ( basename ( __FILE__ ) ) . ".txt"; // 設置Cookie文件保存路徑及文件名
/*模擬瀏覽器*/
$user_agent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.1.4322)";
function vlogin($url, $data) { // 模擬登錄獲取Cookie函數
$curl = curl_init (); // 啟動一個CURL會話
if (IS_PROXY) {
//以下代碼設置代理服務器
//代理服務器地址
curl_setopt ( $curl, CURLOPT_PROXY, $GLOBALS ['proxy'] );
}
curl_setopt ( $curl, CURLOPT_URL, $url ); // 要訪問的地址
curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, 0 ); // 對認證證書來源的檢查
curl_setopt ( $curl, CURLOPT_SSL_VERIFYHOST, 2 ); // 從證書中檢查SSL加密算法是否存在
curl_setopt ( $curl, CURLOPT_USERAGENT, $GLOBALS ['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, $data ); // Post提交的數據包
curl_setopt ( $curl, CURLOPT_COOKIEJAR, $GLOBALS ['cookie_file'] ); // 存放Cookie信息的文件名稱
curl_setopt ( $curl, CURLOPT_COOKIEFILE, $GLOBALS ['cookie_file'] ); // 讀取上面所儲存的Cookie信息
curl_setopt ( $curl, CURLOPT_TIMEOUT, 30 ); // 設置超時限制防止死循環
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 vget($url) { // 模擬獲取內容函數
$curl = curl_init (); // 啟動一個CURL會話
if (IS_PROXY) {
//以下代碼設置代理服務器
//代理服務器地址
curl_setopt ( $curl, CURLOPT_PROXY, $GLOBALS ['proxy'] );
}
curl_setopt ( $curl, CURLOPT_URL, $url ); // 要訪問的地址
curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, 0 ); // 對認證證書來源的檢查
curl_setopt ( $curl, CURLOPT_SSL_VERIFYHOST, 2 ); // 從證書中檢查SSL加密算法是否存在
curl_setopt ( $curl, CURLOPT_USERAGENT, $GLOBALS ['user_agent'] ); // 模擬用戶使用的瀏覽器
@curl_setopt ( $curl, CURLOPT_FOLLOWLOCATION, 1 ); // 使用自動跳轉
curl_setopt ( $curl, CURLOPT_AUTOREFERER, 1 ); // 自動設置Referer
curl_setopt ( $curl, CURLOPT_HTTPGET, 1 ); // 發送一個常規的Post請求
curl_setopt ( $curl, CURLOPT_COOKIEFILE, $GLOBALS ['cookie_file'] ); // 讀取上面所儲存的Cookie信息
curl_setopt ( $curl, CURLOPT_TIMEOUT, 120 ); // 設置超時限制防止死循環
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 vpost($url, $data) { // 模擬提交數據函數
$curl = curl_init (); // 啟動一個CURL會話
if (IS_PROXY) {
//以下代碼設置代理服務器
//代理服務器地址
curl_setopt ( $curl, CURLOPT_PROXY, $GLOBALS ['proxy'] );
}
curl_setopt ( $curl, CURLOPT_URL, $url ); // 要訪問的地址
curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, 0 ); // 對認證證書來源的檢查
curl_setopt ( $curl, CURLOPT_SSL_VERIFYHOST, 2 ); // 從證書中檢查SSL加密算法是否存在
curl_setopt ( $curl, CURLOPT_USERAGENT, $GLOBALS ['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, $data ); // Post提交的數據包
curl_setopt ( $curl, CURLOPT_COOKIEFILE, $GLOBALS ['cookie_file'] ); // 讀取上面所儲存的Cookie信息
curl_setopt ( $curl, CURLOPT_TIMEOUT, 120 ); // 設置超時限制防止死循環
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 delcookie($cookie_file) { // 刪除Cookie函數
unlink ( $cookie_file ); // 執行刪除
}
?>
方法二
<?php
/**
*File:curl.class.php
*CURL封裝類,本類大部分操作均支持鏈式操作
*
*example:
*
*$curl=newCurl($url);
*$curl->exec();
*//發送post數據
*$curl->post(array('username'=>'用戶名'))->exec();
*/
classCurl{
private$ch;
private$flag_if_have_run=false;
private$has_cloase=true;
publicfunction__construct($url='',$forgeIP=false){
$this->init($url,$forgeIP);
}
/**
*初始化CURL。如果CURL未被關閉,則先關閉
*
*@paramtype$url
*@return\Common\Library\Curl
*/
publicfunctioninit($url='',$forgeIP=false){
if(!$this->has_cloase){//如果上一次連接尚未結束,則先關閉
$this->close();
}
if($forgeIP){//偽造IP,將IP偽造為訪問者的IP
if(Validate::isIPAddress($forgeIP)){
$ip=$forgeIP;
}else{
$ip=$_SERVER['SERVER_ADDR'];
}
$this->set_ip($ip);
}
$this->ch=curl_init($url);
curl_setopt($this->ch,CURLOPT_RETURNTRANSFER,1);
$this->has_cloase=false;
return$this;
}
publicfunctionsetUrl($url){
curl_setopt($this->ch,CURLOPT_URL,$url);
return$this;
}
publicfunctionclose(){
if(!$this->has_close){
curl_close($this->ch);
$this->has_cloase=true;
}
}
publicfunction__destruct(){
$this->close();
}
/**
*設置頁面超時時間,支持鏈式操作
*
*@paramtype$timeout
*@return\Common\Library\Curl
*/
publicfunctionset_time_out($timeout){
curl_setopt($this->ch,CURLOPT_TIMEOUT,intval($timeout));
return$this;
}
/**
*偽造來源路徑
*
*@paramtype$referer
*@return\Common\Library\Curl
*/
publicfunctionset_referer($referer){
if(!empty($referer)){
curl_setopt($this->ch,CURLOPT_REFERER,$referer);
}
return$this;
}
/**
*設置cookie
*本方法僅發送cookie信息到遠端,不保存遠端返回的cookie信息
*
*@paramtype$cookie_file cookie文件的存儲路徑
*@return\Common\Library\Curl
*/
publicfunctionload_cookie($cookie_file){
$this->_checkCookie($cookie_file);
curl_setopt($this->ch,CURLOPT_COOKIEFILE,$cookie_file);
return$this;
}
/**
*設置cookie
*發送cookie到遠端,並保存遠端返回的cookie
*
*@paramtype$cookie_file
*@return\Common\Library\Curl
*/
publicfunctioncookie($cookie_file){
$this->_checkCookie($cookie_file);
curl_setopt($this->ch,CURLOPT_COOKIEFILE,$cookie_file);
curl_setopt($this->ch,CURLOPT_COOKIEJAR,$cookie_file);
return$this;
}
/**
*設置cookie
*本方法將不發送cookie信息,僅接收返回的cookie並保存
*
*@paramtype$cookie_file
*@return\Common\Library\Curl
*/
publicfunctionsave_cookie($cookie_file=""){
//設置緩存文件,例如a.txt
if(empty($cookie_file)){
$cookie_file=tempnam('./','cookie');
}
$this->_checkCookie($cookie_file);
curl_setopt($this->ch,CURLOPT_COOKIEJAR,$cookie_file);
return$this;
}
privatefunction_checkCookie($cookie_file){
if(!\Think\Storage::has($cookie_file)){
\Think\Storage::put($cookie_file,'');
}
}
/**
*執行curl請求
*
*@returntype
*/
publicfunctionexec(){
$str=curl_exec($this->ch);
$this->flag_if_have_run=true;
return$str;
}
/**
*設置發送POST請求。沒有調用過該方法默認使用GET方法提交
*
*@paramtype$postData post的數據,支持數組和“xxx=1&x=2”兩種格式
*@return\Common\Library\Curl
*/
publicfunctionpost($postData){
curl_setopt($this->ch,CURLOPT_POST,1);
//echo($postQuery);die;
curl_setopt($this->ch,CURLOPT_POSTFIELDS,$postData);
return$this;
}
/**
*獲取curl的信息
*
*@returntype
*@throwsException
*/
publicfunctionget_info(){
if($this->flag_if_have_run==true){
returncurl_getinfo($this->ch);
}else{
thrownewException("<h1>需先運行(執行exec),再獲取信息</h1>");
}
}
/**
*設置代理
*
*@paramtype$proxy
*@return\Common\Library\Curl
*/
publicfunctionset_proxy($proxy){
//設置代理,例如'68.119.83.81:27977'
curl_setopt($this->ch,CURLOPT_PROXYTYPE,CURLPROXY_SOCKS5);
curl_setopt($this->ch,CURLOPT_PROXY,$proxy);
return$this;
}
/**
*設置請求的IP
*
*@paramtype$ip
*@returntype
*/
publicfunctionset_ip($ip=''){
if(!empty($ip)){
curl_setopt($this->ch,CURLOPT_HTTPHEADER,array("X-FORWARDED-FOR:$ip","CLIENT-IP:$ip"));
}
return$ip;
}
}
