PHP Curl脚本可通过浏览器运行,不能用作Cron Job [英] PHP Curl script works from browser, doesn't work as Cron Job

查看:92
本文介绍了PHP Curl脚本可通过浏览器运行,不能用作Cron Job的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的PHP脚本,该脚本应该发出POST请求。代码如下:

I have a very simple PHP script that is supposed to make a POST request. The code is the following:

$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);

$result = curl_exec($ch);
curl_close($ch);
mail('myemail@gmail.com','Script run with success','Script run with success',$headers);

当我从浏览器执行它时,它可以正常工作。但是,当我尝试将其作为cron作业执行时,Curl部分将无法工作。脚本的其余部分甚至可以作为cron作业使用,因为最后我收到了确认电子邮件。

When I execute it from the browser, it works fine. However, when I try to execute it as a cron job the Curl part won't work. The rest of the script works even as a cron job, as I receive the confirmation email at the end.

这是cron条目:

*/5 * * * * /usr/bin/php /home/username/scripts/test.php

关于为什么的任何线索Curl不是作为Cron作业执行?

Any clue regarding why Curl is not executing as a cron job?

更新:我试图通过Shell运行脚本,而Curl部分也无法运行。因此:

Update: I tried to run the script via shell and the Curl part failed to run too. So:


  • 浏览器:curl有效

  • Shell:curl不起作用

  • Cron:卷曲不起作用

更新2:添加-dsafe_mode通过外壳运行时= Off使脚本运行正常。但是,将相同的标志添加到cron条目并没有任何作用。因此,我仍然需要弄清楚如何从cron进行工作。

Update 2: Adding -dsafe_mode=Off while running via shell make the script run fine. However adding the same flag to the cron entry didn't do anything. So I still need to figure out how to make it work from cron.

推荐答案

事实证明这是身份验证问题。我通过在CURL配置中添加以下两行来解决它:

It turned out to be an authentication problem. I solved it by adding the following two lines to the CURL configuration:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

希望它可以帮助某人。

这篇关于PHP Curl脚本可通过浏览器运行,不能用作Cron Job的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆