php cURL cookie 值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9142964/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
cURL cookie value
提问by Matt Kaye
I'm trying to download music loop files from this site: looperman.com. I've registered as a user, and I'm trying to download the loops using cURL. When you log into looperman.com, there are a few cookies set, but by process of elimination, I notice the only on that is required for the server to see you as logged in is named 'loopermanlooperman'.
我正在尝试从此站点下载音乐循环文件:looperman.com。我已注册为用户,并且正在尝试使用 cURL 下载循环。当您登录 looperman.com 时,设置了一些 cookie,但通过消除过程,我注意到服务器看到您登录所需的唯一一个名为“loopermanlooperman”。
I've grabbed the value of that cookie, and set it as a variable. Then i pass it to the site like so:
我已经获取了那个 cookie 的值,并将它设置为一个变量。然后我将它传递给网站,如下所示:
$sessid = 'somehashedvaluehere';
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Cookie: loopermanlooperman=$sessid;"));
curl_setopt($ch, CURLOPT_URL, "http://www.looperman.com/loops/detail/$pageID");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
echo $response;
When i echo the response, I see the cookie has not been set, and the site still sees me as not logged in. What am i doing wrong? Looperman is built using CodeIgniter. I wonder if they have some measure of protection to prevent setting cookies like this?
当我回显响应时,我看到 cookie 尚未设置,并且该站点仍将我视为未登录。我做错了什么?Looperman 是使用 CodeIgniter 构建的。我想知道他们是否有一些保护措施来防止设置这样的 cookie?
///UPDATE///
///更新///
I tried COOKIE_JAR and CURLOPT_COOKIE. The cookies are still not set. I found this script from another Stack Overflow post that seems to get me most of the way there, but still cookies are set. Here it is:
我试过 COOKIE_JAR 和 CURLOPT_COOKIE。cookie 仍未设置。我从另一篇 Stack Overflow 帖子中找到了这个脚本,它似乎让我完成了大部分工作,但仍然设置了 cookie。这里是:
$loginUrl = 'http://www.looperman.com/account/login/';
$loginFields = array('user_email' => '[email protected]', 'user_password' => 'password');
getUrl($loginUrl, 'post', $loginFields);
//now you're logged in and a session cookie was generated
$remote_page_content = getUrl('http://www.looperman.com/loops/detail/200');
echo $remote_page_content;
function getUrl($url, $method='', $vars='') {
$ch = curl_init();
if ($method == 'post') {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);
}
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'D:\wamp2\www\sandbox\cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'D:\wamp2\www\sandbox\cookie.txt');
$buffer = curl_exec($ch);
curl_close($ch);
return $buffer;
}
When this returns, the contents of D:\wamp2\www\sandbox\cookie.txt are:
返回时,D:\wamp2\www\sandbox\cookie.txt 的内容为:
Netscape HTTP Cookie File http://curl.haxx.se/rfc/cookie_spec.html This file was generated by libcurl! Edit at your own risk. .looperman.com TRUE / FALSE 1329245288 loopermancspr 147f3f08a0b50f7aa527789e360abbc8 .looperman.com TRUE / FALSE 1328467688 loopermanlooperman rX1UOdqyPEKkZ7HT0x8dSLk7g9yf5sSmg%2B7zj66hLM9LSmS1z4nqFO2zkEkqsUqKEwNMvEiExqSKoU2%2BfVsxlf3C9VyucMWt41TJVDtElUUIQrZxv0BmwZYP6JCJrY7wcT1%2FO7kKxRu8YI97YD%2BWdxX3jnWu2Zme9jg%2FMggp3%2Be%2BY%2FFiAorh36FR1zTbSY66VJVj7268WgMy6KNdJ1DxieypwaMb2HYGpBMsQRxcI6RawnOIEdjbaPKYuf8hVy40
But looperman still doesn't see me as logged in :(
但是looperman仍然没有看到我登录:(
回答by Christopher Hackett
You should use CURLOPT_COOKIE
not CURLOPT_HTTPHEADER
to set the cookie values sent in the request.
您应该使用CURLOPT_COOKIE
notCURLOPT_HTTPHEADER
来设置请求中发送的 cookie 值。
curl_setopt($ch, CURLOPT_COOKIE, "loopermanlooperman=$sessid")
CURLOPT_COOKIE
The contents of the "Cookie: " header to be used in the HTTP request. Note that multiple cookies are separated with a semicolon followed by a space (e.g., "fruit=apple; colour=red")
CURLOPT_COOKIE
在 HTTP 请求中使用的“Cookie:”标头的内容。请注意,多个 cookie 用分号后跟一个空格分隔(例如,“fruit=apple; colour=red”)
It does make CURL send the cookie. Try requesting a script that outputs the contents of the headers like this;
它确实让 CURL 发送 cookie。尝试请求一个像这样输出标头内容的脚本;
<?php
echo "Your cookies \n";
print_r( $_COOKIE);
?>
It might be the site is checking the referral or host in your header. You can always try looking at the requests made in a browser (in chrome go Spanner -> Tools -> Developer Tool -> Network, now request the page and click on the request in the list. Should show all headers)
可能是该站点正在检查您标题中的引荐或主机。您可以随时尝试查看在浏览器中发出的请求(在 chrome go Spanner -> Tools -> Developer Tool -> Network 中,现在请求页面并单击列表中的请求。应该显示所有标题)