How to display remote XML file with Wordpress in f

2019-08-28 22:27发布

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.

标签: php wordpress
1条回答
何必那么认真
2楼-- · 2019-08-28 23:00

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

查看更多
登录 后发表回答