在 stream_get_contents 中使用 curl 保存的 cookie
Use cookie saved by curl in stream_get_contents
我想发送 POST 请求,实际上 curl 比 stream_get_contents 慢 100ms,所以我想使用后者。
我有一个由 curl 保存的 cookie.txt,我如何将它用于下面的 stream_get_contents 函数。
function poster($url, $data, $optional_headers = null)
{
$params = array('http' => array(
'method' => 'POST',
'content' => $data
));
if ($optional_headers !== null) {
$params['http']['header'] = $optional_headers;
}
$ctx = stream_context_create($params);
$fp = @fopen($url, 'rb', false, $ctx);
if (!$fp) {
throw new Exception("Problem with $url, $php_errormsg");
}
$response = @stream_get_contents($fp);
if ($response === false) {
throw new Exception("Problem reading data from $url, $php_errormsg");
}
return $response;
}
我的 cookie.txt 由 curl 存储如下所示
secure.domain.com FALSE / FALSE 0 InterSecure AyDzUp5AEKz0ErJYeJF2221lIA$$
#HttpOnly_secure.domain.com FALSE / TRUE 0 ASP.NET_SessionId eqyrgo545czouimlnqc223f0qyi
我试过
$header = array(
'Referer: https://secure.domain.com/IDirectTrading/customer/login.aspx',
'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.20 Safari/537.36',
'Cookie : InterSecure = AyDzUp5AEKz0ErJYeJF2221lIA$$;ASP.NET_SessionId = eqyrgo545czouimlnqc223f0qyi'
);
$CE = poster("https://secure.domain.com/Handler.ashx",$str,$header);
但是 cookie 似乎不起作用,有没有办法将 cookie 文本文件解析为数组并将其用于 header?
根据RFC 6265:
Serialize the cookie-list into a cookie-string by processing each
cookie in the cookie-list in order:
Output the cookie's name, the %x3D ("=") character, and the cookie's value.
If there is an unprocessed cookie in the cookie-list, output the characters %x3B and %x20 ("; ").
换句话说,等号两边没有 space,每个 cookie 应该用分号和 space:
分隔
$header = [
'Cookie: InterSecure=AyDzUp5AEKz0ErJYeJF2221lIA$$; ASP.NET_SessionId=eqyrgo545czouimlnqc223f0qyi',
];
我想发送 POST 请求,实际上 curl 比 stream_get_contents 慢 100ms,所以我想使用后者。
我有一个由 curl 保存的 cookie.txt,我如何将它用于下面的 stream_get_contents 函数。
function poster($url, $data, $optional_headers = null)
{
$params = array('http' => array(
'method' => 'POST',
'content' => $data
));
if ($optional_headers !== null) {
$params['http']['header'] = $optional_headers;
}
$ctx = stream_context_create($params);
$fp = @fopen($url, 'rb', false, $ctx);
if (!$fp) {
throw new Exception("Problem with $url, $php_errormsg");
}
$response = @stream_get_contents($fp);
if ($response === false) {
throw new Exception("Problem reading data from $url, $php_errormsg");
}
return $response;
}
我的 cookie.txt 由 curl 存储如下所示
secure.domain.com FALSE / FALSE 0 InterSecure AyDzUp5AEKz0ErJYeJF2221lIA$$
#HttpOnly_secure.domain.com FALSE / TRUE 0 ASP.NET_SessionId eqyrgo545czouimlnqc223f0qyi
我试过
$header = array(
'Referer: https://secure.domain.com/IDirectTrading/customer/login.aspx',
'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.20 Safari/537.36',
'Cookie : InterSecure = AyDzUp5AEKz0ErJYeJF2221lIA$$;ASP.NET_SessionId = eqyrgo545czouimlnqc223f0qyi'
);
$CE = poster("https://secure.domain.com/Handler.ashx",$str,$header);
但是 cookie 似乎不起作用,有没有办法将 cookie 文本文件解析为数组并将其用于 header?
根据RFC 6265:
Serialize the cookie-list into a cookie-string by processing each cookie in the cookie-list in order:
Output the cookie's name, the %x3D ("=") character, and the cookie's value.
If there is an unprocessed cookie in the cookie-list, output the characters %x3B and %x20 ("; ").
换句话说,等号两边没有 space,每个 cookie 应该用分号和 space:
分隔$header = [
'Cookie: InterSecure=AyDzUp5AEKz0ErJYeJF2221lIA$$; ASP.NET_SessionId=eqyrgo545czouimlnqc223f0qyi',
];