Search code examples
phphtmlxmlrssfeed

How to read feeds inline tag using PHP


I have a below feed xml code Which displays people's profile. I want to use some information on the site but I can't.

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

I want show Username and Avatar this PHP code but I can't do it. please help me Thanks

<?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>";
   }
 }
 ?>

Solution

  • This is a simplified value of your code, just enough to highlight the targets. You can obviusly modify it to suit your needs:

    <?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>";     
    }
    ?>
    

    Random sample of the output:

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

    etc.