cURL cookie值

时间:2012-02-04 17:47:55

标签: php curl session-cookies

我正在尝试从此网站下载音乐循环文件:looperman.com。我已注册为用户,我正在尝试使用cURL下载循环。当你登录looperman.com时,有一些cookie被设置,但是通过消除过程,我注意到服务器看到你登录的唯一需要的是'loopermanlooperman'。

我抓住了该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;

当我回应响应时,我看到cookie尚未设置,并且该网站仍然看到我没有登录。我做错了什么? Looperman是使用CodeIgniter构建的。我想知道他们是否有一些保护措施来防止像这样设置cookie?

/// UPDATE ///

我尝试过COOKIE_JAR和CURLOPT_COOKIE。 Cookie仍未设置。我从另一个Stack Overflow帖子中找到了这个脚本,它似乎让我大部分都在那里,但仍然设置了cookie。这是:

$loginUrl = 'http://www.looperman.com/account/login/';
$loginFields = array('user_email' => 'login@site.com', '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;
  }

返回时,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

但是looperman仍然没有看到我登录:(

1 个答案:

答案 0 :(得分:11)

您应该使用CURLOPT_COOKIE而不是CURLOPT_HTTPHEADER来设置请求中发送的Cookie值。

curl_setopt($ch, CURLOPT_COOKIE, "loopermanlooperman=$sessid")
  

<强> CURLOPT_COOKIE

     

要在HTTP请求中使用的“Cookie:”标头的内容。请注意,多个cookie用分号后跟一个空格分隔(例如“fruit = apple; color = red”)

     

http://www.php.net/manual/en/function.curl-setopt.php/

确实让CURL发送了Cookie 。尝试请求输出标题内容的脚本,如下所示;

<?php
echo "Your cookies \n";
print_r( $_COOKIE);
?>

可能是网站正在检查您的标题中的引荐或主持人。您可以随时尝试查看在浏览器中发出的请求(在chrome中使用Spanner - &gt;工具 - &gt;开发者工具 - &gt;网络,现在请求页面并单击列表中的请求。应显示所有标题)< / p>