如何在前端使用 php 显示带有 Wordpress 的远程 XML 文件?
我有一个问题,我需要显示一个托管在远程服务器上的 XML 文件.那个文件我想用CSS和PHP在已经分配的文件中显示它(page-xxxx.php)
I have a problem, I need to display an XML file that is hosted on a remote server. That file I want to display it with CSS and PHP in a file that is already assigned (page-xxxx.php)
我目前正在使用以下代码,但是我不太明白我在做什么:
I am currently using the following code, however I do not understand very well what I do:
<!-- API here we go!!! -->
<?php
$xmlhd = wp_remote_get('http://www.myurl.com/api/channel.php?type=hd');
$xmlparseado = simplexml_load_string($xmlhd['body']);
?>
代码中指定的 URL 显示了一个像这样的 XML 文件:
The URL specified in the code shows an XML file like this:
<programations>
<channel name="KCBS HD">
<row>
<date>july, 23</date>
<time>06:00</time>
<title><![CDATA[ WKCBS Action News ]]></title>
<description><![CDATA[ Action News, hosted by: Jenn Doe ]]></description>
<imagethumb/>
</row>
<row>
<date>July, 23</date>
<time>06:35</time>
<title><![CDATA[ KCBS Sports Center ]]></title>
<description><![CDATA[ The best scoreS from the Sportscenter stadium, hosted by: Fernando Sobalaprieta ]]></description>
<imagethumb/>
</row>
</channel>
</programations>
我想知道的是如何在页面的前端显示:
What I would like to know is how to show this in front end of a page:
- 日期
- 时间
- 说明
- 缩略图(如果存在)
注意:
XML 的内容只是一个示例,并不一定代表现实:D
The contents of the XML is just a sample example and does not necessarily represent the reality: D
提前,谢谢.
function simplexml_load_string();创建对象.
function simplexml_load_string(); creates object.
如果你尝试print_r($xmlparseado),你应该得到这个:
if you try to print_r($xmlparseado), you should get this:
SimpleXMLElement Object
(
[channel] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => KCBS HD
)
[row] => Array
(
[0] => SimpleXMLElement Object
(
[date] => july, 23
[time] => 06:00
[title] => SimpleXMLElement Object
(
)
[description] => SimpleXMLElement Object
(
)
[imagethumb] => SimpleXMLElement Object
(
)
)
[1] => SimpleXMLElement Object
(
[date] => July, 23
[time] => 06:35
[title] => SimpleXMLElement Object
(
)
[description] => SimpleXMLElement Object
(
)
[imagethumb] => SimpleXMLElement Object
(
)
)
)
)
因此使用迭代,例如对于每个,您应该访问每一行:
So using iteration, for example for each, you should access each row:
$xmlparseado = simplexml_load_string($string);
$content = '';
$rows = $xmlparseado->channel->row;
foreach($rows as $key=>$row){
if($key =='row'){
$row_string = '<ul>';
$row_string.= '<li>Date: '.$row->date.'</li>';
$row_string.= '<li>Time: '.$row->time.'</li>';
$row_string.= '</ul>';
$content.=$row_string;
}
}
echo $content;
注意:这只是一个例子,但你可以使用它的模式
note: this is just example, but you can use its pattern