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('[email protected]','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.

11
  • 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 - Commented Jul 11, 2014 at 15:23
  • Where/how are you defining $url, $fields, and $headers? Commented Jul 11, 2014 at 15:24
  • If you're relying on $_SERVER values in your cron script, it probably won't work. Commented Jul 11, 2014 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. Commented Jul 11, 2014 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. Commented Jul 11, 2014 at 15:27

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.