Search code examples
phpcronfile-get-contents

PHP file_get_contents not working from CRON


I'm using PHP with a CRON job to get the contents of a URL using:

$content = file_get_contents("http://www.example.com");

I then use this content to send as an HTML newsletter. I'm running PHP 7, and I have enabled the relevant PHP INI settings:

allow_url_fopen = On

However, the $content variable is still turning up empty and my error_log is showing the following error:

PHP Warning:  file_get_contents(http://www.example.com): 
failed to open stream: no suitable wrapper could be found 
in /home/xxxx/public_html/cron.php on line xxx

Everything else in the cron.php is working as expected.

Additionally if I manually visit the cron.php via a browser, file_get_contents is working perfectly.

Running WHM/cPanel etc.

Any help is greatly appreciated.


Solution

  • Get path to php.ini in you script:

    php_ini_loaded_file();
    

    set path to php.ini:

    /opt/php/7.3/bin/php -c /var/www/u1234567/php-bin/php.ini -f /var/www/u1234567/public_html/site.com/script.php