1

我对编码很陌生,想尝试为我的网站构建一个 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>
4

1 回答 1

1

您可以为此使用 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

于 2012-07-28T05:41:18.403 回答