将Twitter XML数据转换为要在表中使用的PHP变量


Twitter XML data into PHP variables to be used in a table

我对编码很陌生,想尝试为我的网站构建一个twitter应用程序。我得到了以下代码,但我知道有更好的方法……我只是不知道如何(可能将数据打印到数组中,然后用foreach语句打印到表中?)。如果有人能为我指明正确的方向,我将不胜感激。基本上,我想做的是解析XML并创建变量,这些变量我可以自动调用/打印出来,而不必手动执行(我现在这样做)。

谢谢!

<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" type="text/css" href="bootstrap.css" />
<title> Some Title </title>
</head>
<body>
<div class="container">
    <div class="row-fluid">
    <div class="span2">
        <!--Sidebar content-->
        <button>Carmelo Anthony</button>
    </div>
    <div class="span10">
        <!--Body content-->
        <?php
        $xmldata = 'https://twitter.com/statuses/user_timeline/carmeloanthony.xml';
        $open = fopen($xmldata, 'r');
        $content = stream_get_contents($open);
        fclose($open);
        $xml = new SimpleXMLElement($content);
        ?>
            <table class="table table-striped">
                <tr>
                    <td> <img src=" <? echo $xml->status[0]->user->profile_image_url; ?>"/> </td>
                    <td><strong> <? echo $xml->status[0]->user->name; ?></strong> <i>@<? echo $xml->status[0]->user->screen_name; ?></i> <br /> <? echo $xml->status[0]->text; ?></td>  
                    <td><? echo date("M j",strtotime($xml->status[0]->created_at)); ?></td>
                </tr>
                <tr>
                    <td> <img src=" <? echo $xml->status[1]->user->profile_image_url; ?>"/> </td> 
                    <td><strong> <? echo $xml->status[1]->user->name; ?></strong> <i>@<? echo $xml->status[1]->user->screen_name; ?></i> <br />  <? echo $xml->status[1]->text; ?></td>
                    <td><? echo date("M j",strtotime($xml->status[1]->created_at)); ?></td>
                </tr>
                <tr>
                    <td> <img src=" <? echo $xml->status[2]->user->profile_image_url; ?>"/> </td> 
                    <td><strong> <? echo $xml->status[2]->user->name; ?></strong> <i>@<? echo $xml->status[2]->user->screen_name; ?></i> <br />  <? echo $xml->status[2]->text; ?></td>
                    <td><? echo date("M j",strtotime($xml->status[2]->created_at)); ?></td>
                </tr>
                <tr>
                    <td> <img src=" <? echo $xml->status[3]->user->profile_image_url; ?>"/> </td> 
                    <td><strong> <? echo $xml->status[3]->user->name; ?></strong> <i>@<? echo $xml->status[3]->user->screen_name; ?></i> <br />  <? echo $xml->status[3]->text; ?></td>
                    <td><? echo date("M j",strtotime($xml->status[3]->created_at)); ?></td>
                </tr>
                <tr>
                    <td> <img src=" <? echo $xml->status[4]->user->profile_image_url; ?>"/> </td> 
                    <td><strong> <? echo $xml->status[4]->user->name; ?></strong> <i>@<? echo $xml->status[4]->user->screen_name; ?></i> <br />  <? echo $xml->status[4]->text; ?></td>
                    <td><? echo date("M j",strtotime($xml->status[4]->created_at)); ?></td>
                </tr>
            </table>
    </div>
</div>
</body>
</html>

您可以为此使用foreach循环:

    <!-- ...html code... -->
    <?php
    $xmldata = 'https://twitter.com/statuses/user_timeline/carmeloanthony.xml';
    $open = fopen($xmldata, 'r');
    $content = stream_get_contents($open);
    fclose($open);
    $xml = new SimpleXMLElement($content);
    ?>
        <table class="table table-striped">
            <?php
            foreach($xml->status as $status)
            { ?>
            <tr>
                <td> <img src=" <?php echo $status->user->profile_image_url; ?>" /> </td>
                <td><strong> <?php echo $status->user->name; ?></strong> <i>@<?php echo $status->user->screen_name; ?></i> <br /> <?php echo $status->text; ?></td>  
                <td><?php echo date("M j",strtotime($status->created_at)); ?></td>
            </tr>
              <?php
            }
            ?>
        </table>
        <!-- rest of the code... -->

供参考:http://php.net/manual/en/control-structures.foreach.php

编辑

对于奇怪的字符问题,请尝试使用:html_entity_decode

解码字符串