dongmu3187 2013-07-23 20:19 采纳率: 0%
浏览 51
已采纳

如何使用php在前端使用Wordpress显示远程XML文件?

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']);
?>

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:

  • date
  • time
  • description
  • thumbnail (if exists)

Note:

The contents of the XML is just a sample example and does not necessarily represent the reality: D

In advance, thanks.

  • 写回答

1条回答 默认 最新

  • dpjpo746884 2013-07-23 20:34
    关注

    function simplexml_load_string(); creates object.

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算