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);
       }
    }

?>
1
  • 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. Commented Feb 16, 2015 at 10:04

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you, var_dump($document); is giving me this s3.amazonaws.com/f.cl.ly/items/3V3Y2P3L41401Q102J12/… … How can I work with that xml now. How do I loop through the elements and read specific tags?
I'd recommend you look at the DOMDocument documentation. I'll link to it in my answer.
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
@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.
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

2 Comments

How would I do this using CURL? Can you give me an example please, thank you.
Plenty example out there, here is one to get you started ... stackoverflow.com/questions/561816/…
0

Try the following:

$url = 'xml-file.xml';
$xml = simplexml_load_file($url);
print_r($xml);

Comments

-1

Try this :

$livescore_data = new SimpleXMLElement($file);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.