dongmu3187 2013-07-23 12: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 12: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

    展开全部

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部