Title: curl not working in php script and crontab but does through browser
Tags: php;curl;crontab
Question: I have a php script that runs a curl command and retrieves data. afterwards I update the database with the data. This works fine for me when I execute the script directly but not when using crontab. My crontab line is:
* * * * * /usr/bin/php http://mydomain.com/script.php
When I use this through crontab I see the database updated but not the content received from the curl function.
As well, If I try to run the php script from ssh it works fine as well.
Comment: I tried doing so firstly... It did not work so I tried changing it to the full url.

Comment: Does the script reside on a remote machine? If not, why not just do: `/usr/bin/php /path/to/script.php` ?


Here is another answer: Actually, I managed to work it out just now by adding
!/usr/bin/php
to the top of the php script. Im not exactly sure why did that fix the issue but now it is working.
Comment for this answer: But the php is indeed working. Only the part of cURL is not working within the script.
Comment for this answer: Because with that line you're somewhtat telling which language the script is in. If there's no such declaration, it usually defaults to bash, and bash wouldn't be able to run a php script.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 2, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2014:00:04/27/14', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<php><curl><crontab>', 'Title': 'curl not working in php script and crontab but does through browser', 'ViewCount': 1157, 'id': 23323576, 'user_AboutMe': '', 'user_AccountId': 1587088.0, 'user_DisplayName': 'nimi', 'user_Reputation': 827.0, 'user_Views': 75.0, 'user_id': 1471166.0}}