我正在尝试使用带有以下XML的API:
<movies>
<movie>
<images>
<image type="poster" url="http://cf1.imgobject.com/posters/b7a/4bc91de5017a3c57fe00bb7a/i-am-legend-original.jpg" size="original" width="675" height="1000" id="4bc91de5017a3c57fe00bb7a"/>
<image type="poster" url="http://cf1.imgobject.com/posters/b7a/4bc91de5017a3c57fe00bb7a/i-am-legend-mid.jpg" size="mid" width="500" height="741" id="4bc91de5017a3c57fe00bb7a"/>
<image type="poster" url="http://cf1.imgobject.com/posters/b7a/4bc91de5017a3c57fe00bb7a/i-am-legend-cover.jpg" size="cover" width="185" height="274" id="4bc91de5017a3c57fe00bb7a"/>
</images>
</movie>
</movies>
有人可以给我一个PHP代码示例,我应该使用它来获取图片网址大小=“封面”吗?
感谢。
答案 0 :(得分:2)
SimpleXML可以做到这一点,因为缺少一个更好的词,简单地说:
$xml = new SimpleXMLElement($str);
$xpath = $xml->xpath("/movies/movie/images/image[@size = 'cover']");
echo $xpath[0]['url'];
答案 1 :(得分:0)
使用XML Parser,DOMDocument,SimpleXML等加载xml。
http://se.php.net/manual/en/refs.xml.php
然后您可以使用XPath来选择图像。 http://www.w3schools.com/xpath/xpath_syntax.asp
使用属性大小=封面
获取影片的XPath/movies/movie/images/image[@size=cover]
看起来像一个很好的教程:http://ditio.net/2008/12/01/php-xpath-tutorial-advanced-xml-part-1/
答案 2 :(得分:0)
<?php
$string = <<<XML
<?xml version='1.0'?>
<movies>
<movie>
<images>
<image type="poster" url="http://cf1.imgobject.com/posters/b7a/4bc91de5017a3c57fe00bb7a/i-am-legend-original.jpg" size="original" width="675" height="1000" id="4bc91de5017a3c57fe00bb7a"/>
<image type="poster" url="http://cf1.imgobject.com/posters/b7a/4bc91de5017a3c57fe00bb7a/i-am-legend-mid.jpg" size="mid" width="500" height="741" id="4bc91de5017a3c57fe00bb7a"/>
<image type="poster" url="http://cf1.imgobject.com/posters/b7a/4bc91de5017a3c57fe00bb7a/i-am-legend-cover.jpg" size="cover" width="185" height="274" id="4bc91de5017a3c57fe00bb7a"/>
</images>
</movie>
</movies>
XML;
$xml = simplexml_load_string($string);
foreach($xml->movie->images->image as $image) {
if(strcmp($image['size'],"cover") == 0)
echo $image['url'];
}
?>
答案 3 :(得分:0)
$ xml = simplexml_load_string($ string2); foreach($ xml-&gt; movie-&gt; images-&gt; image as $ image){
if(strcmp($image['size'],"cover"))
// echo $image['url'];
?>
&#34;宽度=&#34; 200像素&#34;高度=&#34; 100像素&#34;&GT;