doushuo1989 2010-05-18 02:45
浏览 8
已采纳

如何使用PHP分别从XML结果集返回owner和id属性的值?

I am using PHP to iterate over the following result set, the aim is to build a hyperlink for each result using a foreach loop. I have stored the XML result in $images, and have constructed this loop:

  foreach ($images as $image) {
  //Build link to each photo returned

  //base URL
  $flickrPhotoUrl = 'http://www.flickr.com/photos/';
  //Append user ID
  $flickrPhotoUrl .= "";
  echo $flickrPhotoUrl;       }

Here is a sample result from Flickr:

  <photos page="1" pages="10982" perpage="10" total="109813">
  <photo id="4616840471" owner="47823583@N03" secret="1b83173bc0" server="4013" farm="5" title="Strawberry Bears" ispublic="1" isfriend="0" isfamily="0"></photo>
<photo id="4616612597" owner="12658756@N08" secret="f626214382" server="4059" farm="5" title="Yarn Chef Minestrone - Grickle Grass" ispublic="1" isfriend="0" isfamily="0"></photo>
<photo id="4616469567" owner="26284268@N00" secret="6911a66838" server="4022" farm="5" title="P5130121.JPG" ispublic="1" isfriend="0" isfamily="0"></photo>
<photo id="4617076736" owner="26284268@N00" secret="8b990acba4" server="4047" farm="5" title="P5130106.JPG" ispublic="1" isfriend="0" isfamily="0"></photo>
<photo id="4616470013" owner="26284268@N00" secret="44600b3836" server="4036" farm="5" title="P5130125.JPG" ispublic="1" isfriend="0" isfamily="0"></photo>
<photo id="4616466147" owner="26284268@N00" secret="554eab8667" server="4052" farm="5" title="P5130116.JPG" ispublic="1" isfriend="0" isfamily="0"></photo>
<photo id="4617082398" owner="26284268@N00" secret="4a2b663442" server="3350" farm="4" title="P5130118.JPG" ispublic="1" isfriend="0" isfamily="0"></photo>
<photo id="4617078272" owner="26284268@N00" secret="357737017b" server="4013" farm="5" title="P5130109.JPG" ispublic="1" isfriend="0" isfamily="0"></photo>
<photo id="4617081446" owner="26284268@N00" secret="1f87726497" server="4048" farm="5" title="P5130117.JPG" ispublic="1" isfriend="0" isfamily="0"></photo>
<photo id="4617077676" owner="26284268@N00" secret="77ca9f754a" server="3330" farm="4" title="P5130108.JPG" ispublic="1" isfriend="0" isfamily="0"></photo>

  • 写回答

2条回答 默认 最新

  • douluoyou9876 2010-05-18 04:23
    关注

    You can use PHP's SimpleXML to manipulate/traverse to your XML easily. Assuming you have your resulting your resulting xml string stored in $images,

    $images_xml = simplexml_load_string($images);
    $flickr_url = "http://flickr.com/photos/";
    
    foreach($images_xml->children() as $image)
    {
         $attributes = $image->attributes();
         $image_url = $flickr_url . $attributes->id;
         //Do whatever you want to do with the link
         //for example, echo it
         echo "<br /><a href=\"". $image_url ."\">" . $attributes->title . "</a>";
    
         //Not sure if the link points to an actual image, but is the idea :)
    }
    

    Just for reference purposes http://www.php.net/manual/en/book.simplexml.php

    Hope that helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)