我试图显示最新的5张图片:
http://api.flickr.com/services/feeds/photos_public.gne
标记为“cars”,然后将最新的5个缩略图输出为空白的html文档,作为一种图库。
这是我得到的:
<?php
require_once 'HTTP/Request2.php';
$request = new HTTP_Request2();
$request->setUrl('http://api.flickr.com/services/feeds/photos_public.gne');
$request->setMethod(HTTP_Request2::METHOD_GET);
$url = $request->getUrl();
$url->setQueryVariable('tags', 'cars');
$url->setQueryVariable('tagmode', 'any');
$url->setQueryVariable('format', 'atom_1');
try {
$response = $request->send();
if (200 == $response->getStatus()) {
$body = $response->getBody();
} else {
echo 'Unexpected HTTP status: ' . $response->getStatus() . ' ' .
$response->getReasonPhrase();
}
} catch (HTTP_Request2_Exception $e) {
echo 'Error: ' . $e->getMessage();
}
$DOM = new SimpleXMLElement($body);
?>
我不知道这是否正确,我不知道如何在html中显示它。
答案 0 :(得分:0)
在查看问题的摘要后,它会显示不的每个项目都有图像元素。因此,访问它会给你NULL
,它被回显为一个空字符串(不可见):
foreach ($DOM->entry as $entry) {
echo '<a href="">', htmlspecialchars($entry->title), '</a>', "\n",
'<img src="', $entry->image, '" alt="', htmlspecialchars($entry->title), '" ',
'width="304" height="228">', "\n";
;
}
示例输出显示标题可用,但图像src为空:
<a href="">Picadas, Marco Juarez 01-05-13</a>
<img src="" alt="Picadas, Marco Juarez 01-05-13" width="304" height="228">
仔细观察Feed本身,事实证明甚至没有任何其他元素包含缩略图,但HTML文本在 conent >元素。只有在HTML内部才有缩略图的尺寸:
<entry>
<title>Picadas, Marco Juarez 01-05-13</title>
<link rel="alternate" type="text/html" href="http://www.flickr.com/photos/osvaldorainero/8709806523/"/>
<id>tag:flickr.com,2005:/photo/8709806523</id>
<published>2013-05-05T15:25:15Z</published>
<updated>2013-05-05T15:25:15Z</updated>
<flickr:date_taken>2013-05-01T15:42:01-08:00</flickr:date_taken>
<dc:date.Taken>2013-05-01T15:42:01-08:00</dc:date.Taken>
<content type="html"> <p><a href="http://www.flickr.com/people/osvaldorainero/">Osvaldo Rainero</a> posted a photo:</p>
<p><a href="http://www.flickr.com/photos/osvaldorainero/8709806523/" title="Picadas, Marco Juarez 01-05-13"><img src="http://farm9.staticflickr.com/8114/8709806523_3b8d7c0418_m.jpg" width="240" height="161" alt="Picadas, Marco Juarez 01-05-13" /></a></p>
</content>
<author>
<name>Osvaldo Rainero</name>
<uri>http://www.flickr.com/people/osvaldorainero/</uri>
<flickr:nsid>91267729@N05</flickr:nsid>
<flickr:buddyicon>http://farm9.staticflickr.com/8107/buddyicons/91267729@N05.jpg?1363607055#91267729@N05</flickr:buddyicon>
</author>
<link rel="enclosure" type="image/jpeg" href="http://farm9.staticflickr.com/8114/8709806523_3b8d7c0418_b.jpg"/>
<category term="cars" scheme="http://www.flickr.com/photos/tags/"/>
...
<category term="arrancadas" scheme="http://www.flickr.com/photos/tags/"/>
</entry>
缩放:
<content type="html"> <p><a href="http://www.flickr.com/people/osvaldorainero/">Osvaldo Rainero</a> posted a photo:</p>
<p><a href="http://www.flickr.com/photos/osvaldorainero/8709806523/" title="Picadas, Marco Juarez 01-05-13"><img src="http://farm9.staticflickr.com/8114/8709806523_3b8d7c0418_m.jpg" width="240" height="161" alt="Picadas, Marco Juarez 01-05-13" /></a></p>
</content>
这是用XML编码的HTML。对于你使用的simplexml来说,这是一种显示阻止,因为它只能逐字逐句地返回HTML:
echo $entry->content, "\n";
输出(纯文本):
<p><a href="http://www.flickr.com/people/osvaldorainero/">Osvaldo Rainero</a> posted a photo:</p>
<p><a href="http://www.flickr.com/photos/osvaldorainero/8709806523/" title="Picadas, Marco Juarez 01-05-13"><img src="http://farm9.staticflickr.com/8114/8709806523_3b8d7c0418_m.jpg" width="240" height="161" alt="Picadas, Marco Juarez 01-05-13" /></a></p>
这是要理解的重要部分:您不仅要从源解析XML,还要解析HTML 里面的 XML元素的节点值。
因此,您需要扩展使用HTML解析器的SimplexmlElement。这很容易做到,因为如果您的PHP版本附带simplexml,它还附带DOMDocument,它有一个HTML解析器和它可以将解析后的结果作为simplexml返回,因此这非常兼容。
因此,下面使用HTML解析器扩展simplexml:
class HtmledSimpleXML extends SimpleXMLElement
{
/**
* Parses element content as HTML and returns the
* body element of it.
*
* @param string $xpath (optional) specify a different element to return
*
* @return SimpleXMLElement
*/
public function html($xpath = '//body') {
$doc = new DOMDocument();
$doc->loadHTML($this);
$xml = simplexml_import_dom($doc->documentElement);
list($body) = $xml->xpath($xpath);
return $body;
}
}
它已经允许传递一个xpath查询来指定你想要检索的具体元素,通常是HTML内部的body
标记,这就是为什么它被设置为默认值。即使您的HTML中没有该标记,它实际上也存在于DOM中,因此这个默认值永远不会出错。但无论如何,在你的情况下,你对img标签感兴趣。感谢simpelxml,我们甚至可以直接将其作为XML输出,甚至可以手动创建HTML。
用法示例:
$DOM = new HtmledSimpleXML($body);
foreach ($DOM->entry as $entry) {
echo '<a href="">', $entry->title, '</a>', "\n",
$entry->content->html('//img')->asXML(), "\n";
;
}
单个条目的示例性输出是:
<a href="">Picadas, Marco Juarez 01-05-13</a>
<img src="http://farm9.staticflickr.com/8114/8709806523_3b8d7c0418_m.jpg" width="240" height="161" alt="Picadas, Marco Juarez 01-05-13"/>
这应该与您正在寻找的内容非常接近。当然,你也可以只获取image-element并访问它的属性,就像使用任何其他Simplexmlelement一样:
$thumb = $entry->content->html('//img');
echo 'Title: ', $entry->title, "\n",
'Thumb: ', $thumb['src'], "\n",
'Size : ', $thumb['width'], ' x ', $thumb['height'], "\n";
输出(纯文本):
Title: Picadas, Marco Juarez 01-05-13
Thumb: http://farm9.staticflickr.com/8114/8709806523_3b8d7c0418_m.jpg
Size : 240 x 161
我希望到目前为止这有用。
I extended SimpleXMLElement on Stackoverflow was to show how to parse CSV data inside the underlying XML structure中的上一次PHP simplexml xpath search for value in an ELEMENT containing tab delimited text?。