1

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:

enter image description here

How can I parse this URL and use it in PHP? This script gives me an error… "Error loading XML".

<?php

    $xml_url = "http://somedomain.com/frieventexport.php";

    if (($response_xml_data = file_get_contents($xml_url))===false){
        echo "Error fetching XML\n";
    } else {
       libxml_use_internal_errors(true);
       $data = simplexml_load_string($response_xml_data);
       if (!$data) {
           echo "Error loading XML\n";
           foreach(libxml_get_errors() as $error) {
               echo "\t", $error->message;
           }
       } else {
          print_r($data);
       }
    }

?>
matt
  • 42,713
  • 103
  • 264
  • 397
  • is there any data coming in $response_xml_data variable? sometimes URL are not allowed to be read in file_get_contents. please check your ini settings too. – kasim badami Feb 16 '15 at 10:04

4 Answers4

1

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

Brian
  • 8,418
  • 2
  • 25
  • 32
  • How would I do this using CURL? Can you give me an example please, thank you. – matt Feb 16 '15 at 10:17
  • Plenty example out there, here is one to get you started ... http://stackoverflow.com/questions/561816/php-curl-extract-an-xml-response – Brian Feb 16 '15 at 11:45
1

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:

More information about DOMDocument and usage

Community
  • 1
  • 1
halfpastfour.am
  • 5,764
  • 3
  • 44
  • 61
  • Thank you, `var_dump($document);` is giving me this https://s3.amazonaws.com/f.cl.ly/items/3V3Y2P3L41401Q102J12/Screen%20Shot%202015-02-16%20at%2015.00.03.png … How can I work with that xml now. How do I loop through the elements and read specific tags? – matt Feb 16 '15 at 14:00
  • I'd recommend you look at the DOMDocument documentation. I'll link to it in my answer. – halfpastfour.am Feb 16 '15 at 15:14
  • The documentation that you provide via link is very extensive in content and in my opinion does not help him solve his confusion with the null var_dump – Kurt Leadley Mar 27 '19 at 04:49
  • @KurtLeadley that may be, but the question is "How can I parse this URL and use it in PHP?". I gave them more then what was asked for. Also, the documentation was added after he asked for more information about the link, see the first comment above. – halfpastfour.am Mar 27 '19 at 19:12
0

Try the following:

$url = 'xml-file.xml';
$xml = simplexml_load_file($url);
print_r($xml);
halfpastfour.am
  • 5,764
  • 3
  • 44
  • 61
Arun
  • 750
  • 5
  • 12
-1

Try this :

$livescore_data = new SimpleXMLElement($file);
Mohammad
  • 28
  • 2
  • 7