使用 atom_1 和 php 显示来自公共 flickr api 的 5 个最新缩略图 [英] Displaying 5 latest thumbnails from public flickr api using atom_1 and php

查看:22
本文介绍了使用 atom_1 和 php 显示来自公共 flickr api 的 5 个最新缩略图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试显示来自以下位置的最新 5 张图像:

http://api.flickr.com/services/feeds/photos_public.gne

带有标签cars",然后将最新的 5 个缩略图输出到一个空白的 html 文档中作为一种画廊.

这是我得到的:

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');尝试 {$response = $request->send();if (200 == $response->getStatus()) {$body = $response->getBody();} 别的 {echo '意外的 HTTP 状态:'.$response->getStatus() .' ' .$response->getReasonPhrase();}} catch (HTTP_Request2_Exception $e) {回声'错误:'.$e->getMessage();}$DOM = new SimpleXMLElement($body);?>

我不知道这是否正确,也不知道如何在 html 中显示它.

解决方案

在查看您问题的提要后,它显示每个项目没有图像元素.因此访问它会给你 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">

仔细观察提要本身,发现甚至没有任何其他元素包含缩略图,而是inside内容的HTML文本> 元素.只有在 HTML 中才有缩略图的尺寸:

<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><已发布>2013-05-05T15:25:15Z</已发布><更新>2013-05-05T15:25:15Z</更新><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><内容类型=html">&lt;p&gt;&lt;a href="http://www.flickr.com/people/osvaldorainero/"&gt;Osvaldo Rainero&lt;/a>发布了一张照片:</p>&lt;p&gt;&lt;a href="http://www.flickr.com/photos/osvaldorainero/8709806523/" title="Picadas, Marco Juarez 01-05-13"&gt;&lt;img src="http://farm9.staticflickr.com/8114/8709806523_3b8d7c0418_m.jpg" width="240" height="161" alt="Picadas, Marco Juarez 01-05-13"/&gt;&lt;/a>&lt;/p></内容><作者><姓名>奥斯瓦尔多·雷内罗<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><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/"/></条目>

缩放:

&lt;p&gt;&lt;a href="http://www.flickr.com/people/osvaldorainero/"&gt;Osvaldo Rainero&lt;/a>发布了一张照片:</p>&lt;p&gt;&lt;a href="http://www.flickr.com/photos/osvaldorainero/8709806523/" title="Picadas, Marco Juarez 01-05-13"&gt;&lt;img src="http://farm9.staticflickr.com/8114/8709806523_3b8d7c0418_m.jpg" width="240" height="161" alt="Picadas, Marco Juarez 01-05-13"/&gt;&lt;/a>&lt;/p></内容>

这是用 XML 编码的 HTML.这对您使用的 simplexml 来说是一种展示,因为它只能逐字返回 HTML 开箱即用:

echo $entry->content, "\n";

输出(纯文本):

<p><a href="http://www.flickr.com/people/osvaldorainero/">Osvaldo Rainero</a>发布了一张照片:</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,而且还想在 XML 元素的节点值解析 HTML.>

因此您需要使用 HTML 解析器扩展您使用的 SimplexmlElement.这很容易做到,因为如果您的 PHP 版本附带 simplexml,那么它还附带具有 HTML 解析器的 DOMDocument 并且它可以将解析结果作为 simplexml 返回,因此这非常兼容.

因此以下内容使用 HTML 解析器扩展了 simplexml:

class HtmledSimpleXML 扩展 SimpleXMLElement{/*** 将元素内容解析为 HTML 并返回* body 元素吧.** @param string $xpath (可选) 指定要返回的不同元素** @return SimpleXMLElement*/公共函数 html($xpath = '//body') {$doc = 新的 DOMDocument();$doc->loadHTML($this);$xml = simplexml_import_dom($doc->documentElement);列表($body) = $xml->xpath($xpath);返回 $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"/>

这应该与您要查找的内容非常接近.当然,您也可以像使用任何其他 Simplexml 元素一样获取图像元素并访问它的属性:

$thumb = $entry->content->html('//img');echo '标题:', $entry->title, "\n",'拇指:', $thumb['src'], "\n",'大小:', $thumb['width'], ' x ', $thumb['height'], "\n";

输出(纯文本):

Title: Picadas, Marco Juarez 01-05-13拇指:http://farm9.staticflickr.com/8114/8709806523_3b8d7c0418_m.jpg尺寸:240 x 161

我希望到目前为止这是有帮助的.

上次我在 Stackoverflow 上扩展 SimpleXMLElement 是为了展示如何在 中解析底层 XML 结构中的 CSV 数据href="https://stackoverflow.com/q/16281205/367456">PHP simplexml xpath 在包含制表符分隔文本的 ELEMENT 中搜索值?.

Im trying to display the latest 5 images from:

http://api.flickr.com/services/feeds/photos_public.gne

with the tag "cars" and then output the latest 5 thumbnails into a blank html document as a type of gallery.

This is as far as i have gotten:

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

?>

I don't know if this is correct and i'm not sure how to go about displaying it in html.

解决方案

After looking into the feed of your question, it shows that each item does not have an image element. Therefore accessing it will give you NULL which is echoed as an empty string (invisible):

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

The exemplary output shows that the title is available, but the image src is empty:

<a href="">Picadas, Marco Juarez  01-05-13</a>
<img src="" alt="Picadas, Marco Juarez  01-05-13" width="304" height="228">

Looking closer into the feed itself it turns out that there is not even any other element containing the thumbnail but the HTML text inside the conent element. And only inside that HTML there are the dimensions of the thumbnail image:

<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">           &lt;p&gt;&lt;a href="http://www.flickr.com/people/osvaldorainero/"&gt;Osvaldo Rainero&lt;/a&gt; posted a photo:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://www.flickr.com/photos/osvaldorainero/8709806523/" title="Picadas, Marco Juarez  01-05-13"&gt;&lt;img src="http://farm9.staticflickr.com/8114/8709806523_3b8d7c0418_m.jpg" width="240" height="161" alt="Picadas, Marco Juarez  01-05-13" /&gt;&lt;/a&gt;&lt;/p&gt;

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

Zoom:

<content type="html">           &lt;p&gt;&lt;a href="http://www.flickr.com/people/osvaldorainero/"&gt;Osvaldo Rainero&lt;/a&gt; posted a photo:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://www.flickr.com/photos/osvaldorainero/8709806523/" title="Picadas, Marco Juarez  01-05-13"&gt;&lt;img src="http://farm9.staticflickr.com/8114/8709806523_3b8d7c0418_m.jpg" width="240" height="161" alt="Picadas, Marco Juarez  01-05-13" /&gt;&lt;/a&gt;&lt;/p&gt;

</content>

This is HTML encoded in XML. And this is kind of a show-stopper for simplexml you use, because it can only return the HTML verbatim out of the box:

echo $entry->content, "\n";

Output (plain text):

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

This is the important part to understand: You do not only want to parse the XML from the feed, but you additionally want to parse HTML inside the node-value of an XML element.

So you need to extend the SimplexmlElement you use with a HTML parser. That is easy to do because if your PHP version ships with simplexml, it also ships with DOMDocument which has an HTML parser and it can return the parsed result as simplexml so this is pretty compatible.

So the following extends simplexml with a HTML parser:

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

It already allows to pass an xpath query to specify the concrete element you want to retrieve, normally that is the body tag from inside the HTML this is why it is set as default. Even if your HTML does not have that tag in there, it actually exists in the DOM, therefore this default is never wrong. But anyway in your case you're interested in the img tag. Thanks to simpelxml we can even output it directly as XML and can spare to even create the HTML by hand.

Usage example:

$DOM = new HtmledSimpleXML($body);

foreach ($DOM->entry as $entry) {
    echo '<a href="">', $entry->title, '</a>', "\n",
         $entry->content->html('//img')->asXML(), "\n";
    ;
}

The exemplary output for a single entry then is:

<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"/>

Which should come pretty close to what you're looking for. Naturally you can as well just aquire the image-element and access it's attributes like with any other Simplexmlelement:

$thumb = $entry->content->html('//img');
echo 'Title: ', $entry->title, "\n",
     'Thumb: ', $thumb['src'], "\n",
     'Size : ', $thumb['width'], ' x ', $thumb['height'], "\n";

Output (plain text):

Title: Picadas, Marco Juarez  01-05-13
Thumb: http://farm9.staticflickr.com/8114/8709806523_3b8d7c0418_m.jpg
Size : 240 x 161

I hope this is helpful so far.

Last time I extended SimpleXMLElement on Stackoverflow was to show how to parse CSV data inside the underlying XML structure in PHP simplexml xpath search for value in an ELEMENT containing tab delimited text?.

这篇关于使用 atom_1 和 php 显示来自公共 flickr api 的 5 个最新缩略图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆