doufu6504 2011-07-20 09:53
浏览 79
已采纳

CURL不保存Cookies(PHP)

I've been struggling with Logging in Wordpress for a while now with CURL (PHP). I think I have tried everything, but it seems that I've missed something. I can't get CURL to save my Cookies when I login. The weird thing, is that even though I can't save cookies to file, everything still works more or less, except Upgrading Worpdress Plugins (that is what I am working on currently). I can login to wordpress, and I can even set an automatic ugprade preg_matching the link and curling on it - Wordpress core upgrades with no problems, but for some reason when it comes to plugins - I just can't do it. I had a LOT guesses last night where the problem lies, but nothing did do. Now when I decided to compare the cookies I get from CURL and in FireFox - it turns out that there are no cookies from CURL.

Okay, So here I set the Options (don't mind the commented stuff, I've been checking everything commenting things out and putting them in...).

$cookieFile = 'cookies.txt'; 
$channel = curl_init();

curl_setopt_array($channel, array (
    CURLOPT_SSL_VERIFYPEER => FALSE,
    CURLOPT_TIMEOUT =>  60,
    CURLOPT_FOLLOWLOCATION =>  1,
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_COOKIEFILE => $cookieFile,
    CURLOPT_COOKIEJAR => $cookieFile,
    CURLOPT_AUTOREFERER =>  true,
    CURLOPT_POSTFIELDS =>   $loginPostData,
    CURLOPT_POST =>  1,
    #CURLOPT_FRESH_CONNECT => 1
    #CURLOPT_FAILONERROR => 1
));
   # curl_setopt ($channel, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
#
# Shorten curl_exec for our $channel
#
function copen($openUrl, $custom = FALSE) {
global $channel;global $siteUrl;
if($custom) {$siteUrl = "";}
curl_setopt ($channel, CURLOPT_URL, $siteUrl.$openUrl);
$result = curl_exec ($channel) or die(curl_error($channel));
echo "<h3> Opening: $openUrl </h3>";
return $result;
}

So I did a search, and tried to get the cookies, and failed with this:

     preg_match('/^Set-Cookie: (.*?);/mi', $result, $m);
     echo(parse_url($m[1]));

I also tried echoing the $cookieFile with file_get_contents() - empty. I checked the file about 15 times now - it is a 777 writable file. I tried writing to it with file_put_contents() and succeeded, so there is error in the file location and/or permissions. I am obviously missing something, I just can't see it.

Best Regards :)

  • 写回答

4条回答 默认 最新

  • dslfjrmz70457 2011-07-20 20:25
    关注

    Okay, So I found the solution. After matching the link to crawl with preg_match() from a HTML content, you need to escape the link first (for a reason unknown to me, yet). We do this with: html_entity_decode($match)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)