cURL cookie 值

发布于 2025-01-02 15:58:22 字数 2630 浏览 2 评论 0原文

我正在尝试从以下网站下载音乐循环文件: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' => '[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 的内容为:

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

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

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;

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///

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

请你别敷衍 2025-01-09 15:58:22

您应该使用 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 -> 工具 -> 开发人员工具 -> 网络,现在请求页面并单击列表中的请求。应该显示所有标头)

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

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

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

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);
?>

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文