SimpleXML - I/O warning : failed to load external entity

后端 未结 4 1437
深忆病人
深忆病人 2020-12-28 14:09

I\'m trying to create a small application that will simply read an RSS feed and then layout the info on the page.

All the instructions I find make this seem simplist

相关标签:
4条回答
  • 2020-12-28 14:48

    You can also load the content with cURL, if file_get_contents insn't enabled on your server.

    Example:

    $ch = curl_init();  
    
    curl_setopt($ch,CURLOPT_URL,"http://feeds.bbci.co.uk/sport/0/football/rss.xml?edition=int");
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
    
    $output = curl_exec($ch);
    
    curl_close($ch);
    
    $items = simplexml_load_string($output);
    
    0 讨论(0)
  • 2020-12-28 14:58

    simplexml_load_file() interprets an XML file (either a file on your disk or a URL) into an object. What you have in $feed is a string.

    You have two options:

    • Use file_get_contents() to get the XML feed as a string, and use e simplexml_load_string():

      $feed = file_get_contents('...');
      $items = simplexml_load_string($feed);
      
    • Load the XML feed directly using simplexml_load_file():

      $items = simplexml_load_file('...');
      
    0 讨论(0)
  • 2020-12-28 15:09

    this also works:

    $url = "http://www.some-url";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $xmlresponse = curl_exec($ch);
    $xml=simplexml_load_string($xmlresponse);
    

    then I just run a forloop to grab the stuff from the nodes.

    like this:`

    for($i = 0; $i < 20; $i++) {
    $title = $xml->channel->item[$i]->title;
    $link = $xml->channel->item[$i]->link;
    $desc = $xml->channel->item[$i]->description;
    $html .="<div><h3>$title</h3>$link<br />$desc</div><hr>";
    }
    echo $html;
    

    ***note that your node names will differ, obviously..and your HTML might be structured differently...also your loop might be set to higher or lower amount of results.

    0 讨论(0)
  • 2020-12-28 15:12
    $url = 'http://legis.senado.leg.br/dadosabertos/materia/tramitando';
    $xml = file_get_contents("xml->{$url}");
    $xml = simplexml_load_file($url);
    
    0 讨论(0)
提交回复
热议问题