2014-03-30 25 views
0

我有一些代碼通過捲曲登錄到一個網站:遷移捲曲請求wget的請求登錄到網站不能正常工作

$url = "https://www.site.net/post/login.page"; 
    $cookie = "cookie.txt"; 

    $postdata = "screenName=$username&kclq=$password&submitEvent=1&TCNK=authenticationEntryComponent&enterClicked=true&ajaxSupported=yes"; 

    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (iPhone; CPU iPhone OS 7_1 like Mac OS X) AppleWebKit/537.51.2 (KHTML, like Gecko) Version/7.0 Mobile/11D167 Safari/9537.53"); 
    curl_setopt($ch, CURLOPT_TIMEOUT, 200); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_COOKIE, 1); 
    curl_setopt($ch, CURLOPT_COOKIESESSION, 1); 
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie); 
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie); 
    curl_setopt($ch, CURLOPT_REFERER, "https://www.site.net/Index.page"); 

    curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata); 
    curl_setopt($ch, CURLOPT_POST, 1); 
    $result = curl_exec($ch); 

我需要能夠改變用戶代理上的蒼蠅,和我根據我的瞭解,不能真的這樣做。所以我決定遷移到WGET:

shell_exec("wget -qO- --max-redirect=1 --save-cookies=\"cookie.txt\" --referer=\"https://www.site.net/Index.page\" --user-agent=\"Mozilla/5.0 (iPhone; CPU iPhone OS 7_1 like Mac OS X) AppleWebKit/537.51.2 (KHTML, like Gecko) Version/7.0 Mobile/11D167 Safari/9537.53\" --post-data=\"screenName=$username&kclq=$password&submitEvent=1&TCNK=authenticationEntryComponent&enterClicked=true&ajaxSupported=yes\" https://www.site.net/post/login.page"); 

但是,這甚至沒有將cookie保存到文件中。我在做什麼錯了/我應該修改什麼?

+0

所有你需要做的是改變用戶代理選項字符串......爲什麼開關什麼時候你已經有了代碼? – Brad

+0

它似乎沒有工作。基本上我使用cURL登錄,然後通過WGET獲取頁面。但是,傳遞'--user-agent'似乎並不會切換用戶代理。 – Someone

+0

請勿使用wget。你有cURL ...你爲什麼要用cURL和wget? – Brad

回答

0

它可能發生,你的wget版本不支持--max-redirect=1因此,從您的命令中刪除該參數,然後再試一次。它應該適合你。

或者,捲曲命令行:

curl -L -X POST --cookie-jar "cookie.txt" --referer "https://www.site.net/Index.page" --user-agent "Mozilla/5.0 (iPhone; CPU iPhone OS 7_1 like Mac OS X) AppleWebKit/537.51.2 (KHTML, like Gecko) Version/7.0 Mobile/11D167 Safari/9537.53" --data "screenName=$username&kclq=$password&submitEvent=1&TCNK=authenticationEntryComponent&enterClicked=true&ajaxSupported=yes" http://localhost/post.php 

另一個選項,代理從你的PHP腳本以這種方式改變:

在你的PHP腳本的頂部添加以下行:

$agent = "Mozilla 6.0"; 
if(isset($_GET['agent'])){ 
    $agent = $_GET['agent']; 
} 

並改變你的這條線的捲曲碼CURLOPT_USERAGENT具有下列之一:

curl_setopt($ch, CURLOPT_USERAGENT, $agent); 

現在從瀏覽器瀏覽你的PHP腳本像這樣:

http://yourdomain/code.php?agent=Opera 9.0