PHP: read a php url as xml and parse it

后端 未结 4 1818
误落风尘
误落风尘 2021-01-24 17:20

i got a url like this one http://somedomain.com/frieventexport.php and the content of this url is a plain XML structure if I check out the source-code of the site:<

相关标签:
4条回答
  • 2021-01-24 17:44

    Try the following:

    $url = 'xml-file.xml';
    $xml = simplexml_load_file($url);
    print_r($xml);
    
    0 讨论(0)
  • 2021-01-24 17:45

    One of the best options in my opinion is to use CURL to get the raw XML data from the url:

    $curl = curl_init();
    curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1 );
    curl_setopt( $curl, CURLOPT_URL, "http://somedomain.com/frieventexport.php" );
    
    $xml = curl_exec( $curl );
    curl_close( $curl );
    

    You can then use DOMDocument to parse the xml:

    $document = new DOMDocument;
    $document->loadXML( $xml );
    

    I would also recommend using <![CDATA[]> tags in your XML. Please read the following:

    • What does <![CDATA[]]> in XML mean?
    • CDATA Sections in XML

    More information about DOMDocument and usage

    • PHP.net DOMDocument documentation
    • W3Schools DOMDocument example
    0 讨论(0)
  • 2021-01-24 17:59

    file_get_contents() is usually disabled if this is shared hosting, if not you can set allow_url_fopen in php.ini (however beware of the security risks). You can check the setting of this using php_info() or var_dump(ini_get('allow_url_fopen')) to show if it's allowed or not.

    If you cannot do the above, you can use CURL to fetch external content

    0 讨论(0)
  • 2021-01-24 17:59

    Try this :

    $livescore_data = new SimpleXMLElement($file);
    
    0 讨论(0)
提交回复
热议问题