Php get xml from remote url not file

I am trying to get xml data from this url.

http://cloud.tfl.gov.uk/TrackerNet/PredictionSummary/V I can enter the url directly into my browser panel and get the desired result.

I am trying to use the code below to extract xml and return it to my browser, but now in the correct domain, so it can be accessed using javascript.

<!DOCTYPE html> <html> <head> <link type='text/css' rel='stylesheet' href='style.css'/> <title>Get Started!</title> </head> <body> <p><?php /*echo file_get_contents("http://cloud.tfl.gov.uk/TrackerNet/PredictionSummary/V");*/ echo simplexml_load_file("http://cloud.tfl.gov.uk/TrackerNet/PredictionSummary/V"); ?></p> </body> </html> 

I tried both file_get_contents and simplexml_load_file but also did not develop

I assumed that the problem is that there is no file at the end of the URL. NOT ONE WORK SO FAR

+4
source share
3 answers

simplexml_load_file() returns an object, not an XML string. Even then, with your current code, XML will be lost in HTML. The following will be equivalent to visiting the URL directly:

 header('Content-type: application/xml'); echo file_get_contents('http://cloud.tfl.gov.uk/TrackerNet/PredictionSummary/V'); 

To make sure you can parse the XML, try this. It loads the XML and prints the root name of the node - in this case ROOT:

 $xml = simplexml_load_file($url); //retrieve URL and parse XML content echo $xml->getName(); // output name of root element 

Does it help?

+4
source

See documentation for

http://php.net/manual/en/simplexml_load_file

for example output output

 <?php $xml = simplexml_load_file('http://cloud.tfl.gov.uk/TrackerNet/PredictionSummary/V'); print_r($xml); ?> 

or check the manual

http://php.net/manual/en/function.echo.php

to output the return string by get_contents ()

 <?php $xml = file_get_contents("http://cloud.tfl.gov.uk/TrackerNet/PredictionSummary/V"); echo $xml; ?> 
+2
source

Try this ... Worked for me ... simple and easy.

 <?php $url=file_get_contents('http://www.w3schools.com/php/note.xml'); $xml = new SimpleXMLElement($url); echo $xml->to; ?> 
0
source

Source: https://habr.com/ru/post/1502055/


All Articles