本文介绍了cURL cookie值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要从这个网站下载音乐循环文件:looperman.com。我注册为用户,我正在尝试使用cURL下载循环。当你登录looperman.com时,有几个cookie被设置,但是通过消除过程,我注意到,只有这样才能让服务器看到你的登录名为loopermanlooperman。

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'.

我已经抓取了该cookie的值,并将其设置为一个变量。然后我把它传递到这样的网站:

I've grabbed the value of that cookie, and set it as a variable. Then i pass it to the site like so:

$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?

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?

/// UPDATE ///

我试过COOKIE_JAR和CURLOPT_COOKIE。 Cookie仍未设置。我发现这个脚本从另一个Stack Overflow的帖子,似乎让我大部分的方式在那里,但仍然饼干设置。这是:

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:

$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;
  }



当此返回时,D:\wamp2 \www\\ \\ sandbox\cookie.txt是:

When this returns, the contents of D:\wamp2\www\sandbox\cookie.txt are:


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仍然看不到我登录:(

But looperman still doesn't see me as logged in :(

推荐答案

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

You should use CURLOPT_COOKIE not CURLOPT_HTTPHEADER to set the cookie values sent in the request.

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



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

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")

它会让CURL发送cookie 。请尝试请求一个输出标题内容的脚本,如下所示:

It does make CURL send the cookie. Try requesting a script that outputs the contents of the headers like this;

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

这可能是网站正在检查引用或主机在您的标题。你可以随时尝试查看在浏览器中的请求(在chrome go扳手 - >工具 - >开发人员工具 - >网络,现在请求页面并点击列表中的请求,应显示所有标题)

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)

这篇关于cURL cookie值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 18:39