【问题标题】:cURL doesn't set a cookie anymore, but why?cURL 不再设置 cookie,但为什么呢?
【发布时间】:2012-02-16 04:09:51
【问题描述】:

我的 cURL 脚本在我的 localhostanymore 不起作用(所以请记住它之前确实起作用)(所以它确实在我的外部主机上起作用,因此:它可能是服务器设置):

这个脚本之前在我的本地主机上运行良好(它仍然在我的主机上运行)。没有改变。

  • 也许我在本地主机上运行此脚本超过 3000 次这一事实很有用。
  • 我在 Windows 7 上运行,使用 WampServer 设置主机。
  • 我可能更改了设置,这会影响 cookie 的写入。但是是哪一个?

真正的问题:cURL 没有设置 cookie!应该打开哪些 apache 模块来写入 cookie(在 .txt 文件中)?我正在运行 wampserver。

请注意我已经在使用:

    curl_setopt($curlTable, CURLOPT_COOKIEJAR, 'cookie.txt');
    curl_setopt($curlTable, CURLOPT_COOKIEFILE, 'cookie.txt');

而那个 php.ini 是:

extension=php_curl.dll is uncommented
  • 附带问题:curl_close 是否取消设置 cookie?如果没有设置 cookiejar 选项?
  • 主要问题:为什么 curl 不像它应该做的那样写一个 cookie(并且在我的外部主机上,而不是在我的 LOCALHOST 上。

其他信息:

phpinfo()

curl
cURL support        enabled
cURL Information    7.21.7
Age                 3
Features
AsynchDNS           Yes
Debug               No
GSS-Negotiate       Yes
IDN                 No
IPv6                Yes
Largefile           Yes
NTLM                Yes
SPNEGO              No
SSL                 Yes
SSPI                Yes
krb4                No
libz                Yes
CharConv            No
Protocols           dict, file, ftp, ftps, gopher, 
                    http, https, imap, imaps, ldap, pop3,
                    pop3s, rtsp, scp, sftp, smtp, smtps, telnet, tftp
Host                i386-pc-win32
SSL Version         OpenSSL/0.9.8r
ZLib Version        1.2.5
libSSH Version      libssh2/1.2.7 

目前使用:

preg_match('/name="csrf" value="(.*?)"/', $getTokenCurlData, $token);

$postFields = array(
    'user'     => $userNum,
    'paswoord' => $userPass,
    'login'    => 'loginform',
    'csrf'     => $token[1]);

// 'user='.$userNum.'&paswoord='.$userPass.'&login=loginform&csrf='.$token[1]

$postData = http_build_query($postFields);

    $curlTable = curl_init();
    curl_setopt($curlTable, CURLOPT_URL, 'link');
    curl_setopt($curlTable, CURLOPT_COOKIEJAR, 'cookie.txt');
    curl_setopt($curlTable, CURLOPT_COOKIEFILE, 'cookie.txt');
    curl_setopt($curlTable, CURLOPT_ENCODING, 'gzip');
    curl_setopt($curlTable, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($curlTable, CURLOPT_POST, true);
    $tableData = curl_exec($curlTable);
    if (!$tableData) echo 'post problem?'.$tableData;
    if ($tableData == false)
{
    echo 'Curl error: ' . curl_error($curlTable);
}

    curl_close($curlTable);
// Here I further process my data.

【问题讨论】:

  • /var/log/apache/errors.log 它说什么?或者你登录的任何地方。在 PHP 中启用显示错误
  • 你怎么知道它不起作用。什么位不工作?
  • 它返回 false,但没有给出错误。看看我对这个答案的评论:stackoverflow.com/a/8961232/574700.
  • curl_setopt($curl, CURLOPT_COOKIEFILE, 'cookies.txt'); 用于存储cookies。
  • @ZombieHunte:是的,我知道,但它没有设置 cookie。应该启用哪些选项来写入文件?

标签: php cookies curl setcookie


【解决方案1】:

我可以看到该代码有两处错误:

您在创建资源和执行资源之间切换变量名称:

$curl = curl_init();                 // $curl
$siteSource = curl_exec($curlTable); // $curlTable

您提出了一个帖子请求,但没有设置内容类型,您应该这样做:

$postFields = array(
  'user' => $userNum,
  'paswoord' => $userPass,
  'login' => 'loginform'
);
// This ensures all strings are properly encoded
$postData = http_build_query($postFields);

// ...

// curl *should* do this by itself, but best to do it explicitly
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
  'Content-Type: application/x-www-form-urlencoded',
  'Content-Length: '.strlen($postData),
  'Connection: close'
));

【讨论】:

  • 第一个只是采集错误。在我的代码中,这是正确完成的。我已经编辑了我的帖子。感谢您的关注。我还包括了构建功能,谢谢。添加了您提供的标题选项。但仍然是空白页。 (它返回假)
  • 您能否提供您尝试访问的网站的实际 URL,以便我自己进行测试?
【解决方案2】:

您可能在登录过程中被禁止。这将为您提供有关该问题的更多信息:

// change 
// if (!$siteSource) echo 'Help!';
// to
if ($siteSource === false)
{
    echo 'Curl error: ' . curl_error($curl);
}

编辑:您可以尝试其他一些选择(与 SSL 相关的问题不止一次解决了我的问题):

curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.0.3705; .NET CLR 1.1.4322; Media Center PC 4.0)');
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_VERBOSE, 1);

// following is very important
// TRUE to follow any "Location: " header that the server sends
// as part of the HTTP header (note this is recursive, PHP will follow
// as many "Location: " headers that it is sent, unless CURLOPT_MAXREDIRS is set).
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);

编辑 2:在 opt 之后将为您提供返回的标头(仅用于调试)。除了确保 cookie.txt 被正确使用(可定位和可写)。

curl_setopt($curl, CURLOPT_HEADER, true);

这就是我所能做的一切。现在吃午饭!


编辑 3:Cookie 相关的东西:

$cookie_file = PATH_TO_YOUR_COOKIE_FILE . '/cookie.txt';

if (! file_exists($cookie_file) || ! is_writable($cookie_file))
{
    echo 'Cookie file missing or not writable.';
    exit;
}

// you already added the following, I put it again just to remark their utility
curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie_file);
curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie_file);

编辑 4:始终在开头检查:

if ( ! extension_loaded('curl'))
{
    echo "You need to load/activate the curl extension.";
}

如果您收到此错误,请在 php.ini 中激活 curl 取消注释/删除前面的 ;

windows:
;extension=php_curl.dll // or curl.dll
linux:
;extension=php_curl.so // or curl.so

并重新启动网络服务器。如果你没有找到这一行,那么你需要安装 curl:

// ubuntu
sudo apt-get install php5-curl

// centos
sudo yum install php5-curl

对于 Windows 或您的 wampserver,它也一定很简单。

【讨论】:

  • if ($siteSource === false) echo 'Curl error: ' 。 curl_error($curl);这不是假的。虽然这有效: if ($siteSource == false) echo 'Curl error: ' 。 curl_error($curl); (少一个等号)但是没有给出错误。所以它输出:'卷曲错误:'
  • 我添加了一些想法。我告诉你,我从来没有使用 curl 输过一场战斗,所以要坚持不懈。祝你好运...
  • 添加了所有这些。没用。我又更新了我的问题。
  • 使用=== 是正确的(不要更改它)。如果与 == 一起使用,则表示服务器不返回任何内容。这就是为什么curl_error 给出一个空字符串(没有发生错误)。玩我最后的建议。确定一切正常。
  • 编辑 3 完成。我希望所有这些编辑都是有用的。你知道我的意思? 8) 嗯?
【解决方案3】:

虽然这个问题有点过时,但我今天遇到了同样的问题,并没有用这里的任何建议解决它。 cookie 未保存的原因仅仅是缺少调用

curl_close()

如果 curl 请求后没有调用 curl_close cookies 不保存。

我花了大约一个小时才找到......也许可以节省你的时间:-)

【讨论】:

    【解决方案4】:

    在我的情况下有两个问题: 1)服务器不接受没有价值的cookie 2) 我没有使用 CURLOPT_COOKIEFILE 选项

    每个问题都通过将其分解为单独的部分并单独解决每个问题来解决。

    所以我建议你划分你的问题: 1) 检查是这个 COOKIEFILE 问题还是网站 - 没有文件直接发送 cookie 值 - 可能是服务器问题 2)如果这没问题 - 检查文件内容可能有问题(就像我的情况一样)

    【讨论】:

      猜你喜欢
      • 2020-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-30
      • 1970-01-01
      • 1970-01-01
      • 2014-05-26
      相关资源
      最近更新 更多