Php 卷曲不';我不再做饼干了,但为什么?
我的cURL脚本在我的本地主机上不再工作了(请记住它以前确实工作过)(因此它在我的外部主机上工作,因此:它可能是服务器设置): 这个脚本以前在我的本地主机上运行得很好(在我的主机上仍然如此)。没有什么变化Php 卷曲不';我不再做饼干了,但为什么?,php,cookies,curl,setcookie,Php,Cookies,Curl,Setcookie,我的cURL脚本在我的本地主机上不再工作了(请记住它以前确实工作过)(因此它在我的外部主机上工作,因此:它可能是服务器设置): 这个脚本以前在我的本地主机上运行得很好(在我的主机上仍然如此)。没有什么变化 也许我已经在本地主机上运行了3000多次这个脚本这一事实很有用 我在Windows7上运行,使用WampServer设置主机 我可能更改了一个设置,这会影响cookies的编写。但是哪一个呢 真正的问题:cURL不会设置cookie!写cookie(在.txt文件中)应该使用哪些apach
- 也许我已经在本地主机上运行了3000多次这个脚本这一事实很有用
- 我在Windows7上运行,使用WampServer设置主机
- 我可能更改了一个设置,这会影响cookies的编写。但是哪一个呢
curl_setopt($curlTable, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($curlTable, CURLOPT_COOKIEFILE, 'cookie.txt');
extension=php_curl.dll is uncommented
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.
而php.ini是:
curl_setopt($curlTable, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($curlTable, CURLOPT_COOKIEFILE, 'cookie.txt');
extension=php_curl.dll is uncommented
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.
- 附带问题:curl\u close是否取消了cookie?如果没有设置cookiejar选项?
- 主要问题:为什么不像它应该做的那样编写cookie(在我的外部主机上,而不是本地主机上)
其他资料: phpinfo() 当前使用:
curl_setopt($curlTable, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($curlTable, CURLOPT_COOKIEFILE, 'cookie.txt');
extension=php_curl.dll is uncommented
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.
我可以看出该代码有两个错误: 您可以在创建资源和执行资源之间切换变量名称:
$curl = curl_init(); // $curl
$siteSource = curl_exec($curlTable); // $curlTable
您发出了post请求,但未设置内容类型,应执行以下操作:
$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'
));
您可能在登录过程中被禁止。这将为您提供有关该问题的更多信息:
// change
// if (!$siteSource) echo 'Help!';
// to
if ($siteSource === false)
{
echo 'Curl error: ' . curl_error($curl);
}
编辑:您可以尝试的其他选项(与SSL相关的选项多次解决了我的问题):
编辑2:以下选项将为您提供返回的标题(仅用于调试)。此外,请确保cookie.txt被正确使用(可定位和可写) 在我身边我能做的就这些了。现在来吃午饭吧
编辑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
然后重新启动web服务器。如果未找到此行,则需要安装:
// ubuntu
sudo apt-get install php5-curl
// centos
sudo yum install php5-curl
对于windows或您的wampserver,这也一定很简单。虽然这个问题有点过时,但我今天也遇到了同样的问题,没有用这里的任何建议来解决它。没有保存cookie的原因只是缺少对的调用
curl_close()
如果在curl请求后未调用curl\u close,则不会保存cookies。
我花了大约一个小时才发现……也许可以节省您的时间:-)在我的情况下,有两个问题: 1) 服务器未接受没有值的cookie 2) 我没有使用CURLOPT_COOKIEFILE选项 每一个问题都是通过把它分为不同的部分来解决的,并且每个问题都是自己解决的 因此,我建议您将问题分为: 1) 检查是否存在cookie文件问题或网站-直接发送cookie值而不发送文件-可能存在服务器问题
2) 如果这一切正常-检查文件内容是否有问题(如我的情况)/var/log/apache/errors.log它说了什么?无论你在哪里登录。在PHPHow中启用显示错误您知道它不工作吗。哪个位不起作用?它返回false,但没有给出错误。看看我对这个答案的评论:.
curl_setopt($curl,CURLOPT_COOKIEFILE,'cookies.txt')
用于存储cookie。@ZombieHunte:是的,我知道,但它不设置cookie。写入文件应启用哪些选项?第一个选项只是采集错误。在我的代码中,这是正确的。我已经编辑了我的帖子。谢谢你的注意。我还包括了构建功能,谢谢。添加了您提供的标题选项。但还是一页空白。(返回false)您是否能够提供您尝试访问的站点的实际URL,以便我自己测试它?如果($siteSource==false)回显“Curl error:”。旋度误差($curl);这不是假的。虽然这是可行的:如果($siteSource==false)回显“Curl error:”。旋度误差($curl);(少一个等号)但没有给出错误。所以它输出:“Curl error:”我添加了一些想法。我告诉你,我从未在使用curl的战斗中失败过,所以要坚持不懈。祝你好运……所有这些都加上了。没用。我再次更新了我的问题。使用==
是正确的(不要更改它)。如果与==
一起工作,则表示服务器不返回任何内容。这就是为什么curl\u error
给出一个空字符串(没有发生错误)。认真考虑我最后的建议。确保一切正常。编辑3完成。我希望所有这些编辑都是有用的。你知道我的意思吗?8) 嗯?