I am trying to get the xml data from this url.
我试图从这个网址获取xml数据。
http://cloud.tfl.gov.uk/TrackerNet/PredictionSummary/V I can enter the url directly into my browser bar and get the required result.
http://cloud.tfl.gov.uk/TrackerNet/PredictionSummary/V我可以直接在我的浏览器栏中输入网址并获得所需的结果。
I am trying to use the code below to fetch the xml and return it to my browser but now in the correct domain so it can be accessed by javascript.
我正在尝试使用下面的代码来获取xml并将其返回到我的浏览器,但现在在正确的域中,以便可以通过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 have tried both file_get_contents and simplexml_load_file but neither have worked out
我已经尝试了file_get_contents和simplexml_load_file,但都没有解决
I had assumed the problem was with the fact that there is not a file at the end of url. NONE OF BELOW WORKING SO FAR
我认为问题在于url末尾没有文件。以下没有任何工作
3 个解决方案
#1
4
simplexml_load_file()
returns an object rather than the XML string. Even then, with your current code, the XML would be lost within HTML. The following would be equivalent to visiting the URL directly:
simplexml_load_file()返回一个对象而不是XML字符串。即使这样,使用当前代码,XML也会在HTML中丢失。以下内容相当于直接访问URL:
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 out the name of the root node - in this case, ROOT:
要确保您可以解析XML,请尝试此操作。它加载XML并打印出根节点的名称 - 在本例中为ROOT:
$xml = simplexml_load_file($url); //retrieve URL and parse XML content
echo $xml->getName(); // output name of root element
Does this help?
这有帮助吗?
#2
2
See the documentation for
请参阅文档
http://php.net/manual/en/simplexml_load_file
http://php.net/manual/en/simplexml_load_file
for an example to print the 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
http://php.net/manual/en/function.echo.php
to output the string return by file_get_contents()
通过file_get_contents()输出字符串返回
<?php
$xml = file_get_contents("http://cloud.tfl.gov.uk/TrackerNet/PredictionSummary/V");
echo $xml;
?>
#3
0
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;
?>
#1
4
simplexml_load_file()
returns an object rather than the XML string. Even then, with your current code, the XML would be lost within HTML. The following would be equivalent to visiting the URL directly:
simplexml_load_file()返回一个对象而不是XML字符串。即使这样,使用当前代码,XML也会在HTML中丢失。以下内容相当于直接访问URL:
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 out the name of the root node - in this case, ROOT:
要确保您可以解析XML,请尝试此操作。它加载XML并打印出根节点的名称 - 在本例中为ROOT:
$xml = simplexml_load_file($url); //retrieve URL and parse XML content
echo $xml->getName(); // output name of root element
Does this help?
这有帮助吗?
#2
2
See the documentation for
请参阅文档
http://php.net/manual/en/simplexml_load_file
http://php.net/manual/en/simplexml_load_file
for an example to print the 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
http://php.net/manual/en/function.echo.php
to output the string return by file_get_contents()
通过file_get_contents()输出字符串返回
<?php
$xml = file_get_contents("http://cloud.tfl.gov.uk/TrackerNet/PredictionSummary/V");
echo $xml;
?>
#3
0
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;
?>