下面總結(jié)了在php中有兩種可以模仿用戶進(jìn)入登錄或post數(shù)據(jù)的實(shí)現(xiàn)方法,對(duì)大家很有用,有需要的朋友可參考一下.
通過(guò)curl函數(shù):PHP中的CURL函數(shù)庫(kù)(Client URL Library Function)
curl_close — 關(guān)閉一個(gè)curl會(huì)話
curl_copy_handle — 拷貝一個(gè)curl連接資源的所有內(nèi)容和參數(shù)
curl_errno — 返回一個(gè)包含當(dāng)前會(huì)話錯(cuò)誤信息的數(shù)字編號(hào)
curl_error — 返回一個(gè)包含當(dāng)前會(huì)話錯(cuò)誤信息的字符串
curl_exec — 執(zhí)行一個(gè)curl會(huì)話
curl_getinfo — 獲取一個(gè)curl連接資源句柄的信息
curl_init — 初始化一個(gè)curl會(huì)話
curl_multi_add_handle — 向curl批處理會(huì)話中添加單獨(dú)的curl句柄資源
curl_multi_close — 關(guān)閉一個(gè)批處理句柄資源
curl_multi_exec — 解析一個(gè)curl批處理句柄
curl_multi_getcontent — 返回獲取的輸出的文本流
curl_multi_info_read — 獲取當(dāng)前解析的curl的相關(guān)傳輸信息
curl_multi_init — 初始化一個(gè)curl批處理句柄資源
curl_multi_remove_handle — 移除curl批處理句柄資源中的某個(gè)句柄資源
curl_multi_select — Get all the sockets associated with the cURL extension, which can then be "selected"
curl_setopt_array — 以數(shù)組的形式為一個(gè)curl設(shè)置會(huì)話參數(shù)
curl_setopt — 為一個(gè)curl設(shè)置會(huì)話參數(shù)
curl_version — 獲取curl相關(guān)的版本信息
curl_init()函數(shù)的作用初始化一個(gè)curl會(huì)話,curl_init()函數(shù)唯一的一個(gè)參數(shù)是可選的,表示一個(gè)url地址。
curl_exec()函數(shù)的作用是執(zhí)行一個(gè)curl會(huì)話,唯一的參數(shù)是curl_init()函數(shù)返回的句柄。
curl_close()函數(shù)的作用是關(guān)閉一個(gè)curl會(huì)話,唯一的參數(shù)是curl_init()函數(shù)返回的句柄。
例,代碼如下:
- $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)."&";
- }//開源代碼Vevb.com
- $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,我們要抓取數(shù)據(jù),可能是登錄以后的內(nèi)容,這個(gè)時(shí)候我們就要用到curl的模擬登錄功能了,代碼如下:
- function checklogin( $user, $password )
- {
- if ( emptyempty( $user ) || emptyempty( $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 ) )//開源代碼Vevb.com
- {
- return 0;
- }else{
- return 1;
- }
- }
- define( "USERAGENT", $_SERVER['HTTP_USER_AGENT'] );
- define( "COOKIEJAR", tempnam( "/tmp", "cookie" ) );
- define( "TIMEOUT", 500 );
- echo checklogin("zhangying215","xtaj227");
2.通過(guò)fsockopen
.PHP fsockopen函數(shù)說(shuō)明:
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() ).就是返回一個(gè)文件句柄
開啟PHP fsockopen這個(gè)函數(shù),PHP fsockopen需要 PHP.ini 中 allow_url_fopen 選項(xiàng)開啟,代碼如下:
- $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);
如果出現(xiàn):Warning: fsockopen() has been disabled for security reasons in D:…cos-html-cachecos-html-cache.php on line 35
換了其他版本的cos-html-cache,還是不行,后來(lái)找到下面的方法,結(jié)果不行,因?yàn)楹瘮?shù)都被禁用了,大家試下,很少有我這樣的情況的,用其他替代函數(shù).
一、如何禁用fsockopen()
下面是兩種常用的禁用fsockopen的方法.
1、修改php.ini,將 disable_functions = 后加入 fsockopen
2、修改php.ini,將 allow_url_fopen = On 改為 allow_url_fopen = Off
二、如何解決fsockopen函數(shù)被禁用
1、如果服務(wù)器沒(méi)有同時(shí)禁用pfsockopen,那么直接將fsockopen函數(shù)替換為pfsockopen.
具體操作:搜索程序中的字符串fsockopen替換為 pfsockopen示例如下.
修改前:$fp = fsockopen($host,80,$errno,$errstr,30);
修改后:$fp = pfsockopen($host,80,$errno, $errstr,30);
新聞熱點(diǎn)
疑難解答