【问题标题】:How to read feeds inline tag using PHP如何使用 PHP 读取提要内联标签
【发布时间】:2020-04-19 11:01:07
【问题描述】:

我有一个下面的提要 xml 代码,它显示了人们的个人资料。我想使用网站上的一些信息,但我不能。

<?xml version="1.0"?>
<feed>
    <item system="15907" performancePage="http://platform.signaltofollow.com/performance/15907" avg_per_month="37.956" avatar="" username="FX_TM" country="Russian Federation" country_flag="http://platform.signaltofollow.com/api/assets/images/country/flags_round/flag_ru.png" />
    <item system="15915" performancePage="http://platform.signaltofollow.com/performance/15915" avg_per_month="13.9571" avatar="" username="InvestTrade77" country="Russian Federation" country_flag="http://platform.signaltofollow.com/api/assets/images/country/flags_round/flag_ru.png" />
    <item system="17315" performancePage="http://platform.signaltofollow.com/performance/17315" avg_per_month="12.5121" avatar="http://platform.signaltofollow.com/api/assets/images/user/6197fdd1-771f-429c-abfe-6ff232885658.JPG?_=cd5bc00c2c26fe659b58d722dade05d8" username="B4x" country="Poland" country_flag="http://platform.signaltofollow.com/api/assets/images/country/flags_round/flag_pl.png" />
    <item system="15289" performancePage="http://platform.signaltofollow.com/performance/15289" avg_per_month="10.6175" avatar="" username="Profittrading" country="Germany" country_flag="http://platform.signaltofollow.com/api/assets/images/country/flags_round/flag_de.png" />

</feed>

我想显示这个 PHP 代码的用户名和头像,但我做不到。 请帮我 谢谢

<?php

 $url = "http://platform.signaltofollow.com/api/feed/top15TradeSystems";

 $invalidurl = false;
 if(@simplexml_load_file($url)){
  $feeds = simplexml_load_file($url);
 }else{
  $invalidurl = true;
  echo "<h2>Invalid RSS feed URL.</h2>";
 }

 $i=0;
 if(!empty($feeds)){

  echo "<h1>".$site."</h1>";
  foreach ($feeds->feed as $item) {

   $avatar = $item->item->avatar;
   $username = $item->item->username;

   if($i>=1) break;
  ?>
   <div class="post">
       <h2><?php echo $username; ?></h2>
    <img src="<?php echo $avatar; ?>">
   </div>

   <?php
    $i++;
   }
 }else{
   if(!$invalidurl){
     echo "<h2>No item found</h2>";
   }
 }
 ?>

【问题讨论】:

  • if($i>=1) 中断;将使您的代码在第一次迭代后退出您的 foreach 循环。发生什么了?你说这行不通。

标签: php html xml rss feed


【解决方案1】:

这是代码的简化值,足以突出显示目标。您可以明显地修改它以满足您的需要:

<?php
$url = "http://platform.signaltofollow.com/api/feed/top15TradeSystems";
$feeds = simplexml_load_file($url);
$pars = $feeds->xpath('//feed/item[@username]');
foreach($pars as $node) {
    $un = $node->xpath('./@username')[0];
    $av = $node->xpath('./@avatar')[0];
    if (strlen ( $av )==0) {
    $av = 'No avatar';
       }
    echo "username: ". $un ."    avatar: ". $av . "<br>";     
}
?>

输出的随机样本:

username: InvestTrade77 avatar: No avatar
username: B4x avatar: http://platform.signaltofollow.com/api/assets/images/user/6197fdd1-771f-429c-abfe-6ff232885658.JPG?_=cd5bc00c2c26fe659b58d722dade05d8

等等

【讨论】:

  • 获取节点属性时,可以使用$node['username'],而不是$node-&gt;xpath('./@username')[0]
  • @NigelRen 正确。我更喜欢坚持使用 xpath 的原因(即使在这样的情况下)是为了心理训练,因为在这种情况下属性不容易获得的情况。
  • 问题是使用 XPath 可能会非常昂贵,因此为微不足道的值执行此操作可能会产生巨大的开销。
猜你喜欢
  • 2011-03-09
  • 2020-02-08
  • 1970-01-01
  • 1970-01-01
  • 2012-12-02
  • 2017-04-25
  • 1970-01-01
  • 2020-12-22
  • 1970-01-01
相关资源
最近更新 更多