2012-07-28 39 views
1

我对编码非常陌生,并且想尝试为我的网站构建一个twitter应用程序。我得到了下面的代码来工作,但我知道有一个更好的方法来做到这一点......我只是不知道如何(可能会打印出数据到数组中,然后用foreach语句将数据打印到表中)。如果有人能指出我正确的方向,我会很感激。基本上,我期望做的是解析XML并创建变量,我可以自动调用/打印出来,而不必像手动那样做(我现在这样做)。Twitter XML数据导入到表格中使用的PHP变量

谢谢!

<!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> 

回答

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

+0

谢谢你的回复!它工作完美。如果我可能还有一个快速问题: 看起来来自XML的“(引号)”正在呈现不当(如“”),任何想法如何解决这个问题? – Ckamin5 2012-07-28 15:19:41

+0

请参阅上面的编辑。 – 2012-07-28 15:30:27