使用curl在stream_get_contents中保存的cookie



我想发送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;
}

curl存储的我的cookie.txt如下所示

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文本文件解析成数组并将其用于标头?

根据RFC 6265:

通过处理每个cookie列表将cookie列表序列化为cookie字符串cookie列表中的cookie(按顺序(:

  1. 输出cookie的名称、%x3D("="(字符和cookie的值。

  2. 如果cookie列表中存在未处理的cookie,则输出字符%x3B和%x20("(。

换句话说,等号周围没有空格,每个cookie都应该用分号和空格分隔:

$header = [
'Cookie: InterSecure=AyDzUp5AEKz0ErJYeJF2221lIA$$; ASP.NET_SessionId=eqyrgo545czouimlnqc223f0qyi',
];

最新更新