所以我有 hostgator 来托管 domain.com
和 hostinger 来托管 test.com
。在那些网站上,我运行着完全相同的脚本。该脚本所做的是,它登录到外部网站 example.com
,获取登录 cookie 并将其存储。这个 cookie 可以工作几个月!然后转到 example.com/need-to-be-logged-in-to-view-this-page
并获取其内容。
但是,我遇到了一个问题。我在两家托管公司的代码完全相同。它在 hostgator 上完美运行。然而,在托管服务器上,它登录并获取 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
时在 hostgator 上
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 201
、浏览器 session
、窗口 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']
关于php - 在 cURL 中发送发布请求以获取 cookie 适用于一个虚拟主机,但不适用于另一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57137804/