I've encountered a problem while setting up a cron job. The script runs fine if I open it in a browser. When cron executes it it returns the following error messages through email: Warning: file_get_contents(): URL file-access is disabled in the server configuration in .......... on line 180 Warning: file_get_contents(.............): failed to open stream: no suitable wrapper could be found in ................ on line 180 Code (markup): Line 180 is the following: $xmlstring=file_get_contents($feed); Code (markup): Where $feed is the url of an XML-file. Would really appreciate any help on why this suddenly doesn't work. Thank you.
The xml-feeds can be quite large which makes simplexml_load_file() to exceed my allowed memory size. Instead I use: $xmlstring=file_get_contents($feed); $xml=new SimpleXMLElement($xmlstring); Which works fine (well, until now ).
I tried another approach, with curl instead: $ch=curl_init(); $timeout=5; curl_setopt($ch,CURLOPT_URL,$feed); curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout); $file_contents=curl_exec($ch); curl_close($ch); $xml=new SimpleXMLElement($file_contents); Code (markup): This returns another error when running cron (works fine in browser): Fatal error: Cannot instantiate non-existent class: simplexmlelement Code (markup): Seems weird that it does recognize the class in a browser but not when executed by cron.
You must set allow_url_fopen to on in your .htaccess or curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
You or your host is running a different php version and settings for CLI (command line/cronjobs) than the Apache (http browser).
You need to ticket your web hosting company, allow_url_fopen is a php feature that they can disable, the CLI may use a different PHP configuration and not have the related extension loaded, especially true if you are using something like PHP Selector in the web stack.
Problem: Your server configuration doesn't allow function file_get_contents to read anything other than local files, and your script is trying to read an http url. Assumption: It is your script and you coded it so you know how to modify it. Solution: a) Use same curl code that you posted in your 2nd reply and that stores curl result in variable $file_contents $ch=curl_init(); $timeout=5; curl_setopt($ch,CURLOPT_URL,$feed); curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout); $file_contents=curl_exec($ch); curl_close($ch); Code (markup): b) Download attached file called xml2array.php, and use in your script to create php array out of the read XML, this way require_once 'xml2array.php'; $array = xml2array($file_contents); //check parsed XML print_r($array); // << use this array Code (markup): I hope it helps.. check attachment. Stay well....