cURL不再设置cookie,但为什么?

Sup*_*Spy 4 php cookies curl setcookie

我的cURL脚本在我的本地主机不再工作(所以请记住它之前的DID工作)(因此它可以在我的外部主机上工作,因此:它可能是服务器设置):

这个脚本在我的localhost之前工作正常(它仍然在我的主机上).没有改变.

  • 也许我在本地主机上运行这个脚本超过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');
Run Code Online (Sandbox Code Playgroud)

那个php.ini是:

extension=php_curl.dll is uncommented
Run Code Online (Sandbox Code Playgroud)
  • 附带问题:curl_close是否取消了cookie?如果没有设置cookiejar选项?
  • 主要问题:为什么不像他们应该做的那样卷曲写一个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 
Run Code Online (Sandbox Code Playgroud)

目前使用:

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.
Run Code Online (Sandbox Code Playgroud)

Ale*_*php 10

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

curl_close()
Run Code Online (Sandbox Code Playgroud)

如果未保存卷曲请求cookie后未调用curl_close .

花了我一个小时才发现....也许可以节省你的时间:-)


Igo*_*rra 7

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

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

编辑:您可以尝试的其他一些选项(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);
Run Code Online (Sandbox Code Playgroud)

编辑2:以下选项将返回返回的标题(仅用于调试).除了确保cookie.txt正确使用(可定位和可写).

curl_setopt($curl, CURLOPT_HEADER, true);
Run Code Online (Sandbox Code Playgroud)

这就是我能为自己做出的贡献.现在去吃午饭!


编辑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);
Run Code Online (Sandbox Code Playgroud)

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

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

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

windows:
;extension=php_curl.dll // or curl.dll
linux:
;extension=php_curl.so // or curl.so
Run Code Online (Sandbox Code Playgroud)

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

// ubuntu
sudo apt-get install php5-curl

// centos
sudo yum install php5-curl
Run Code Online (Sandbox Code Playgroud)

对于Windows或您的wampserver,它也必须很容易.