4

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);

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.

Here's the cron entry:

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

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

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

  • Browser: curl works
  • Shell: curl doesn't work
  • Cron: curl doesn't work

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.

Daniel Scocco
  • 7,036
  • 13
  • 51
  • 78
  • What happens when you simply run the script in the shell? It might be something as simple as you need to add the `-f` flag to the php binary - – rm-vanda Jul 11 '14 at 15:23
  • Where/how are you defining `$url`, `$fields`, and `$headers`? – Patrick Q Jul 11 '14 at 15:24
  • If you're relying on `$_SERVER` values in your cron script, it probably won't work. – Jeff Lambert Jul 11 '14 at 15:25
  • There are different `php.ini` files for webserver and CLI. Make sure the options relevant to your script are the same in both. – Barmar Jul 11 '14 at 15:25
  • $url, $fields and $headers are not the problem because when I test via the browser it works fine. I am not relying on any $_SERVER values either. – Daniel Scocco Jul 11 '14 at 15:27
  • It's possible that there your test.php script is erroring for some reason, but you are not seeing the error message (because the cron job is not setup to send error reports to you). You might want to add `MAILTO="email@domain.com"` near the beginning of your crontab so that any errors running the entries in the crontab will be emailed to you, and hopefully this will shed some light on why it's failing. – mti2935 Jul 11 '14 at 15:28
  • I believe it's related to not using the same PHP interpreter, or the same php.ini file. I am investigating. – Daniel Scocco Jul 11 '14 at 15:28
  • @Barmar, how do I select the specific php.ini file? I believe this would solve the problem. – Daniel Scocco Jul 11 '14 at 15:54
  • Use `get_cfg_var('cfg_file_path')` to get the location of the config file. Run it in the web version and CLI version and you'll see the different locations. – Barmar Jul 11 '14 at 16:19
  • @Barmar, strangely both the web version and CLI are using the same php.ini (/usr/local/lib/php.ini). Please check the 2 updates I made to the question to see if they shed some light. – Daniel Scocco Jul 11 '14 at 16:27
  • Sounds like a permissions issue then, but I'm not sure why it would affect `curl`. – Barmar Jul 11 '14 at 16:28

1 Answers1

5

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);

Hope it helps someone.

Daniel Scocco
  • 7,036
  • 13
  • 51
  • 78