在 cURL 中发送发布请求以获取 cookie 适用于一个网络托管,但不适用于另一个网络托管



所以我有hostgator来托管domain.com和hostinger来托管test.com。在这些网站中,我正在运行完全相同的脚本。脚本的作用是,它登录到外部网站example.com,获取登录cookie并存储它。这个饼干可以工作几个月!然后转到example.com/need-to-be-logged-in-to-view-this-page并获取其内容。

但是,我面临一个问题。我在两家托管公司都有完全相同的代码。它在主机器上完美运行。但是,在hostinger上,它会登录并获取cookie,但是cookie仅在登录时起作用一次。

因此,我在hostgator上生成了cookie并将该cookie复制到hostinger,它运行良好。所以我想知道,cURL 中是否有某种设置或 hostgator 可能已启用的默认设置,而 hostinger 可能没有启用,这使得在 hostinger 上生成的 cookie 在登录时只持续一秒钟。

function get_login_cookie($ch, $headerLine) {
if (strncmp($headerLine, "set-cookie: coolCookie=", 20) == 0) {
$endPos = strpos($headerLine, ";");
$coolCookie = substr($headerLine, 20, ($endPos - 20));
file_put_contents("cookie.txt", $coolCookie);
}
return strlen($headerLine);
}
function login($username, $password) {
$fields = [
"username"      => $username,
"password"      => $password
];
$fields_string = http_build_query($fields);
$ch = curl_init("https://example.com/login");
curl_setopt($ch, CURLOPT_HEADERFUNCTION, "get_login_cookie");
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_exec($ch);
}

所以我尝试从get_login_cookie()函数打印$headerLiner。它们非常不同。Hostinger 和 hostgator 如何没有完全相同的代码具有相同的响应。

当我打印出$headerLiner时在主机主机上

HTTP/2 201 
server: nginx/1.15.6
date: Sun, 21 Jul 2019 23:59:04 GMT
content-type: application/json; charset=UTF-8
content-length: 479
set-cookie: coolCookie=THIS_IS_THE_COOKIE; Domain=.example.com; Path=/; Expires=Sun, 28-Jul-19 23:59:04 UTC; HttpOnly
expires: Thu, 19 Nov 1981 08:52:00 GMT
cache-control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
pragma: no-cache
vary: Accept,Cookie,Authorization,X-EXAMPLE-Sauce
location: https://api.example.com/login/8gybiuf8gybundfguino
p3p: CP='TST'
set-cookie: browser_session=browser_gfdhfdguy46ytyrty/oMnIg; Domain=.example.com; Path=/; Expires=Sun, 21-Jul-69 23:59:04 UTC; HttpOnly
set-cookie: window_session=window_45gh56yjhttrghf4; Domain=.example.com; Path=/; HttpOnly
set-cookie: coolCookie=THIS_IS_THE_COOKIE; Domain=.example.com; Path=/; Expires=Sun, 28-Jul-19 23:59:04 UTC; HttpOnly
x-example-rnd: p3456ythth4567yt

当我在托管器上打印$headerLine时。Hostinger的响应缺少HTTP/2 201browser sessionwindow session

server: nginx/1.15.6
date: Sun, 21 Jul 2019 23:46:41 GMT
content-type: application/json; charset=UTF-8
content-length: 180
set-cookie: coolCookie=THIS_IS_THE_COOKIE; expires=Sun, 28-Jul-2019 23:46:41 GMT; Max-Age=604800; path=/; domain=.example.com; HttpOnly
expires: Thu, 19 Nov 1981 08:52:00 GMT
cache-control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
pragma: no-cache
set-cookie: coolCookie=THIS_IS_THE_COOKIE; expires=Sun, 28-Jul-2019 23:46:41 GMT; Max-Age=604800; path=/; domain=.example.com; HttpOnly
vary: Accept,Cookie,Authorization,X-EXAMPLE-Sauce
x-example-rnd: OD4wQjIDT5ug0C

问题是 API 返回了400错误代码。这可能是由于服务器之间的 curl 用户代理存在差异(导致 API 阻止请求)。

curl_setopt($ch, CURLOPT_HTTPHEADER, ['User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36']

最新更新