PHP CURL似乎忽略cookie jar /文件 [英] PHP CURL seems to be ignoring cookie jar/file
问题描述
我使用这个....
function cURL($url, $header=NULL, $p=NULL)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:9.0.1) Gecko/20100101 Firefox/9.0.1");
curl_setopt($ch, CURLOPT_COOKIEFILE, "/tmp/TestCookies");
if ($p) {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $p);
}
$result = curl_exec($ch);
if ($result) {
return $result;
} else {
return curl_error($ch);
}
curl_close($ch);
}
我正在打这样的电话...
And I'm making a call like this...
$Headers = array(
"Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*\/*;q=0.5",
"Accept-Language: en-gb,en;q=0.5",
"Accept-Charset: windows-1251,utf-8;q=0.7,*;q=0.7");
$a = $this->cURL("https://www.mysite.com/",$Headers, null);
我希望这会收到一个cookie并写入文件 / tmp / TestCookies
。
I'd expect this to receive a cookie and write it to the file /tmp/TestCookies
.
该网站肯定会返回 Set-Cookie
headers - 如果我转储
The site definitely returns Set-Cookie
headers - I can see them if I dump $a
, however, the file in question is never created.
如果这是一个权限问题,我创建它与触摸
和 chmod 777
'd it - 该文件现已存在,但它为空。
In case it's a permissions issue, I created it with touch
and chmod 777
'd it - The file now exists but it's empty.
我做错了什么?
推荐答案
curl常数分为
-
CURL_COOKIEFILE
作为从 - 和
CURL_COOKIEJAR
作为数据存储区读取的cookies.txt源写入返回
CURL_COOKIEFILE
as the cookies.txt source which is read from- and
CURL_COOKIEJAR
as the datastore that is written back to
您必须提供 _COOKIEFILE
使用空字符串来启用它IIRC,但 _COOKIEJAR
如果你想要它们存储。通常将两个选项设置为相同的名称。
You do have to provide _COOKIEFILE
with an empty string to enable it IIRC, but _COOKIEJAR
if you want them stored. Normally set both options to the same name.
这篇关于PHP CURL似乎忽略cookie jar /文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!