cookie不能与cron调用php脚本

时间:2012-10-26 09:44:20

标签: php zend-framework cookies curl cron

我已经在每分钟设置了一个curl cron调用,在我的php db请求中没有涉及cookie变量时可以正常工作:

$stmt4= $dbh->query("SELECT * FROM schedule_confs WHERE uyi=0  
AND callable=1 AND id_admin=2 "); 

如果查询涉及这样的cookie:

 $id_user_conecte = Zend_Registry::get("iduser");   

$stmt4= $dbh->query("SELECT * FROM schedule_confs WHERE uyi=0  
AND callable=1 AND id_admin=".$id_user_conecte." "); 

这不起作用,因为没有检测到变量$id_user_conecte因此没有数据库请求,顺便说一句,我使用的是zend。如果我只是回显$id_user_conecte它就可以了,所以我的zend控制器没问题。 这是正常的,有卷曲的cron没有检测到文档中的cookie,或者我错过了什么?

1 个答案:

答案 0 :(得分:0)

我想你应该在你的CURL请求中设置cookie

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://blog.yousoft.ru/');
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIE,$cookie);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_USERAGENT, 'PHP Bot (http://blog.yousoft.ru)');
$data = curl_exec($ch);
$header=substr($data,0,curl_getinfo($ch,CURLINFO_HEADER_SIZE));
$body=substr($data,curl_getinfo($ch,CURLINFO_HEADER_SIZE));
preg_match_all("/Set-Cookie: (.*?)=(.*?);/i",$header,$res);
$cookie='';
foreach ($res[1] as $key => $value) {
 $cookie.= $value.'='.$res[2][$key].'; ';
};
curl_close($ch);

或者

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://blog.yousoft.ru/');
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_USERAGENT, 'PHP Bot (http://blog.yousoft.ru)');
$data = curl_exec($ch);
curl_close($ch);

参考文献(俄语):http://blog.yousoft.ru/2010/03/31/ispolzuem-curl-php-chast-4-rabotaem-s-cookies-2-sposoba/