Cron, file_get_contents(), URL file-access

Discussion in 'PHP' started by skum, Sep 10, 2010.

  1. #1
    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.
     
    skum, Sep 10, 2010 IP
  2. s_ruben

    s_ruben Active Member

    Messages:
    735
    Likes Received:
    26
    Best Answers:
    1
    Trophy Points:
    78
    #2
    If you work with xml files why don't you use simplexml_load_file() function?
     
    s_ruben, Sep 10, 2010 IP
  3. skum

    skum Peon

    Messages:
    16
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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 :)).
     
    skum, Sep 10, 2010 IP
  4. skum

    skum Peon

    Messages:
    16
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Anyone got any clue?
     
    skum, Sep 10, 2010 IP
  5. skum

    skum Peon

    Messages:
    16
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    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.
     
    skum, Sep 11, 2010 IP
  6. s_ruben

    s_ruben Active Member

    Messages:
    735
    Likes Received:
    26
    Best Answers:
    1
    Trophy Points:
    78
    #6
    s_ruben, Sep 11, 2010 IP
  7. karjen

    karjen Active Member

    Messages:
    54
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    73
    #7
    You must set allow_url_fopen to on in your .htaccess or curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     
    karjen, May 2, 2016 IP
  8. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #8
    You or your host is running a different php version and settings for CLI (command line/cronjobs) than the Apache (http browser).
     
    ThePHPMaster, May 2, 2016 IP
  9. JeffH {wx}

    JeffH {wx} Greenhorn

    Messages:
    23
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    8
    #9
    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.
     
    JeffH {wx}, May 5, 2016 IP
  10. Vooler

    Vooler Well-Known Member

    Messages:
    1,146
    Likes Received:
    64
    Best Answers:
    4
    Trophy Points:
    150
    #10
    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....
     

    Attached Files:

    Vooler, May 12, 2016 IP