将 cURL 请求迁移到 WGET 请求以登录网站不起作用


Migrating cURL request to WGET request for logging into website not working

我有一些代码可以通过cURL登录网站:

    $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 保存到文件中。 我做错了什么/我应该修改什么?

您的wget版本可能不支持--max-redirect=1 因此,请从命令中删除该参数,然后重试。它应该对你有用。

或者,curl 命令行:

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 代码:

curl_setopt($ch, CURLOPT_USERAGENT, $agent);

现在像这样从浏览器浏览你的 php 脚本:

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