下面總結了在php中有兩種可以模仿使用者進入登入或post資料的實現方法,對大家很有用哦,有需要的朋友可參考一下。
通過curl函數
PHP中的CURL函數庫(Client URL Library Function)
curl_close — 關閉一個curl會話
curl_copy_handle — 拷貝一個curl串連資源的所有內容和參數
curl_errno — 返回一個包含當前會話錯誤資訊的數字編號
curl_error — 返回一個包含當前會話錯誤資訊的字串
curl_exec — 執行一個curl會話
curl_getinfo — 擷取一個curl串連資源控制代碼的資訊
curl_init — 初始化一個curl會話
curl_multi_add_handle — 向curl批處理會話中添加單獨的curl控制代碼資源
curl_multi_close — 關閉一個批處理控制代碼資源
curl_multi_exec — 解析一個curl批處理控制代碼
curl_multi_getcontent — 返回擷取的輸出的文字資料流
curl_multi_info_read — 擷取當前解析的curl的相關傳輸資訊
curl_multi_init — 初始化一個curl批處理控制代碼資源
curl_multi_remove_handle — 移除curl批處理控制代碼資源中的某個控制代碼資源
curl_multi_select — Get all the sockets associated with the cURL extension, which can then be "selected"
curl_setopt_array — 以數組的形式為一個curl設定會話參數
curl_setopt — 為一個curl設定會話參數
curl_version — 擷取curl相關的版本資訊
curl_init()函數的作用初始化一個curl會話,curl_init()函數唯一的一個參數是可選的,表示一個url地址。
curl_exec()函數的作用是執行一個curl會話,唯一的參數是curl_init()函數返回的控制代碼。
curl_close()函數的作用是關閉一個curl會話,唯一的參數是curl_init()函數返回的控制代碼。
例
| 代碼如下 |
複製代碼 |
$post_data = array(); $post_data['clientname'] = "test08"; $post_data['clientpasswd'] = "test08"; $post_data['submit'] = "submit"; $url='http://xxx.xxx.xxx.xx/xx/xxx/top.php'; $o=""; foreach ($post_data as $k=>$v) { $o.= "$k=".urlencode($v)."&"; } $post_data=substr($o,0,-1); $ch = curl_init(); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_URL,$url); //為了支援cookie curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt'); curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); $result = curl_exec($ch); |
模仿使用者登入
類比登入到sina
我們要抓取資料,可能是登入以後的內容,這個時候我們就要用到curl的類比登入功能了。
| 代碼如下 |
複製代碼 |
<?php function checklogin( $user, $password ) { if ( empty( $user ) || empty( $password ) ) { return 0; } $ch = curl_init( ); curl_setopt( $ch, CURLOPT_REFERER, "http://mail.sina.com.cn/index.html" ); curl_setopt( $ch, CURLOPT_HEADER, true ); curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); curl_setopt( $ch, CURLOPT_USERAGENT, USERAGENT ); curl_setopt( $ch, CURLOPT_COOKIEJAR, COOKIEJAR ); curl_setopt( $ch, CURLOPT_TIMEOUT, TIMEOUT ); curl_setopt( $ch, CURLOPT_URL, "http://mail.sina.com.cn/cgi-bin/login.cgi" ); curl_setopt( $ch, CURLOPT_POST, true ); curl_setopt( $ch, CURLOPT_POSTFIELDS, "&logintype=uid&u=".urlencode( $user )."&psw=".$password ); $contents = curl_exec( $ch ); curl_close( $ch ); if ( !preg_match( "/Location: (.*)/cgi/index.php?check_time=(.*)n/", $contents, $matches ) ) { return 0; }else{ return 1; } } define( "USERAGENT", $_SERVER['HTTP_USER_AGENT'] ); define( "COOKIEJAR", tempnam( "/tmp", "cookie" ) ); define( "TIMEOUT", 500 ); echo checklogin("zhangying215","xtaj227"); ?> |
2.通過fsockopen
.PHP fsockopen函數說明:
Open Internet or Unix domain socket connection(開啟通訊端連結)
Initiates a socket connection to the resource specified by target .
fsockopen() returns a file pointer which may be used together with the other file functions (such as fgets() , fgetss() , fwrite() , fclose() , and feof() ).就是返回一個檔案控制代碼
開啟PHP fsockopen這個函數
PHP fsockopen需要 PHP.ini 中 allow_url_fopen 選項開啟。
| 代碼如下 |
複製代碼 |
$URL=‘http://xxx.xxx.xxx.xx/xx/xxx/top.php'; $post_data['clientname'] = "test08"; $post_data['clientpasswd'] = "test08"; $post_data['submit'] = "ログイン"; $referrer=""; // parsing the given URL $URL_Info=parse_url($URL); // Building referrer if($referrer=="") // if not given use this script as referrer $referrer=$_SERVER["SCRIPT_URI"]; // making string from $data foreach($post_data as $key=>$value) $values[]="$key=".urlencode($value); $data_string=implode("&",$values); // Find out which port is needed - if not given use standard (=80) if(!isset($URL_Info["port"])) $URL_Info["port"]=80; // building POST-request: $request.="POST ".$URL_Info["path"]." HTTP/1.1n"; $request.="Host: ".$URL_Info["host"]."n"; $request.="Referer: $referrern"; $request.="Content-type: application/x-www-form-urlencodedn"; $request.="Content-length: ".strlen($data_string)."n"; $request.="Connection: closen"; $request.="n"; $request.=$data_string."n"; $fp = fsockopen($URL_Info["host"],$URL_Info["port"]); fputs($fp, $request); while(!feof($fp)) { $result .= fgets($fp, 128); } fclose($fp);
|
如果出現
Warning: fsockopen() has been disabled for security reasons in D:…cos-html-cachecos-html-cache.php on line 35
換了其他版本的cos-html-cache,還是不行。後來找到下面的方法。 (結果不行,因為函數都被禁用了。)
大家試下,很少有我這樣的情況的,用其他替代函數。
一、如何禁用fsockopen()
下面是兩種常用的禁用fsockopen的方法。
1、修改php.ini,將 disable_functions = 後加入 fsockopen
2、修改php.ini,將 allow_url_fopen = On 改為 allow_url_fopen = Off
二、如何解決fsockopen函數被禁用
1、如果伺服器沒有同時禁用pfsockopen,那麼直接將fsockopen函數替換為pfsockopen。
具體操作:搜尋程式中的字串 fsockopen( 替換為 pfsockopen( 。樣本如下
修改前:
$fp = fsockopen($host, 80, $errno, $errstr, 30);
修改後:
$fp = pfsockopen($host, 80, $errno, $errstr, 30);
http://www.bkjia.com/PHPjc/444664.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/444664.htmlTechArticle下面總結了在php中有兩種可以模仿使用者進入登入或post資料的實現方法,對大家很有用哦,有需要的朋友可參考一下。 通過curl函數 PHP中的...