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

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

问题描述

我试着从显示最新的5张图片:

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

与标签汽车,然后输出最新的5缩略图到一个空白的HTML文档作为一种画廊。

这是据我已经得到了:

 < PHP
require_once'HTTP / Request2.php';
$ =请求新HTTP_Request2();$请求 - > setUrl('http://api.flickr.com/services/feeds/photos_public.gne');
$请求 - >使用setMethod(HTTP_Request2 :: METHOD_GET);
$ URL = $请求 - >的getURL();
$ URL-> setQueryVariable('标签','汽车');
$ URL-> setQueryVariable('tagmode','任何');
$ URL-> setQueryVariable('形式','atom_1');尝试{
   $响应= $请求 - >发();
   如果(200 == $响应 - >的getStatus()){
   $体= $响应 - > getBody();
}其他{
    呼应意外的HTTP状态:。 $响应 - >的getStatus()。 ''。
         $响应 - > getReasonPhrase();
}
}赶上(HTTP_Request2_Exception $ E){
   回声错误:。 $ E-GT&;的getMessage();
}
$ DOM =新的SimpleXMLElement($体);?>

我不知道这是否是正确的,我不知道如何去在HTML中显示出来。


解决方案

寻找到你的问题的饲料后,它表明,每个项目做的的有一个的图片的元件。因此访问它会给你 NULL 这是呼应作为空字符串(隐形):

 的foreach($ DOM->条目$进入){
    呼应'< A HREF =>',用htmlspecialchars($入门>标题),'< / A>',\\ n
         '< IMG SRC =,$入门>的形象,'ALT =,用htmlspecialchars($入门>标题),'',
              '宽度=304HEIGHT =228>',\\ N的;
    ;
}

示范性输出显示的标题是可用的,但图像的src是空的:

 < A HREF => Picadas,马尔科华雷斯13年1月5日< / A>
< IMG SRC =ALT =Picadas,马可·雷斯13年1月5日WIDTH =304HEIGHT =228>

更仔细地观察到饲料本身事实证明,竟然没有包含的缩略图的,但HTML文本的 的里面的 conent 元素。并且只有HTML里面有缩略图的尺寸:

 <输入>
    <标题> Picadas,马尔科华雷斯13年1月5日< /标题>
    <链接rel =备用TYPE =text / html的HREF =htt​​p://www.flickr.com/photos/osvaldorainero/8709806523//>
    < ID>标签:flickr.com,2005:/照片/ 8709806523< / ID>
    <&出版GT; 2013-05-05T15:25:15Z< /发表>
    <&更新GT; 2013-05-05T15:25:15Z< /更新>
    < Flickr的:date_taken> 2013-05-01T15:42:01-08:00< / Flickr的:date_taken>
    < D​​C:date.Taken> 2013-05-01T15:42:01-08:00< / DC:date.Taken>
    <内容类型=HTML> &放大器;所述; P&放大器; GT;&放大器;所述; A HREF =htt​​p://www.flickr.com/people/osvaldorainero/与& GT;奥斯瓦尔多Rainero&放大器;所述; / A&放大器; GT;发布了一张照片:放大器; LT; / P&放大器; GT;&放大器; LT; P&放大器; GT;&放大器; LT; A HREF =htt​​p://www.flickr.com/photos/osvaldorainero/8709806523/称号=Picadas,马可·雷斯13年1月5日&放大器; GT; &放大器; LT; IMG SRC =htt​​p://farm9.staticflickr.com/8114/8709806523_3b8d7c0418_m.jpgWIDTH =240HEIGHT =161ALT =Picadas,马可·雷斯13年1月5日/&安培; GT;&放大器;所述; / A&放大器; GT;&放大器;所述; / p&放大器; GT;< /内容>
    <作者>
      <名称>奥斯瓦尔多Rainero< /名称>
      &LT;&URI GT; HTTP://www.flickr.com/people/osvaldorainero/< / URI&GT;
      &LT; Flickr的:NSID&GT; 91267729 @ N05&LT; / Flickr的:NSID&GT;
      <flickr:buddyicon>http://farm9.staticflickr.com/8107/buddyicons/91267729@N05.jpg?1363607055#91267729@N05</flickr:buddyicon>
    &LT; /笔者&GT;
    &LT;链接rel =圈地TYPE =图像/ JPEG的href =htt​​p://farm9.staticflickr.com/8114/8709806523_3b8d7c0418_b.jpg/&GT;
    &LT;类项=汽车计划=htt​​p://www.flickr.com/photos/tags//&GT;
    ...
    &LT;类项=arrancadas计划=htt​​p://www.flickr.com/photos/tags//&GT;
&LT; /进入&GT;

变焦:

 &lt;内容类型=HTML&GT; &放大器;所述; P&放大器; GT;&放大器;所述; A HREF =htt​​p://www.flickr.com/people/osvaldorainero/与&amp; GT;奥斯瓦尔多Rainero&放大器;所述; / A&放大器; GT;发布了一张照片:放大器; LT; / P&放大器; GT;&放大器; LT; P&放大器; GT;&放大器; LT; A HREF =htt​​p://www.flickr.com/photos/osvaldorainero/8709806523/称号=Picadas,马可·雷斯13年1月5日&放大器; GT; &放大器; LT; IMG SRC =htt​​p://farm9.staticflickr.com/8114/8709806523_3b8d7c0418_m.jpgWIDTH =240HEIGHT =161ALT =Picadas,马可·雷斯13年1月5日/&安培; GT;&放大器;所述; / A&放大器; GT;&放大器;所述; / p&放大器; GT;&LT; /内容&GT;

这是XML HTML EN codeD。这是怎样的一个表明,塞了SimpleXML的使用,因为它只能逐字返回HTML开箱:

 回声$入门&gt;内容,\\ n;

输出(纯文本):

 &LT; P&GT;&LT; A HREF =htt​​p://www.flickr.com/people/osvaldorainero/&GT;奥斯瓦尔多Rainero&LT; / A&GT;发布了一张照片:LT; / P&GT;&LT; P&GT;&LT; A HREF =htt​​p://www.flickr.com/photos/osvaldorainero/8709806523/称号=Picadas,马可·雷斯13年1月5日&GT;&LT; IMG SRC =HTTP: //farm9.staticflickr.com/8114/8709806523_3b8d7c0418_m.jpgWIDTH =240HEIGHT =161ALT =Picadas,马可·雷斯13年1月5日/&GT;&LT; / A&GT;&LT; / p&GT;

这是最重要的部分就明白了:你不仅要分析从进料的XML,但你还需要解析HTML的 的内部的XML元素的节点值

所以,你需要扩展你一个HTML解析器使用的SimpleXMLElement。这是很容易做到,因为如果你的PHP版本附带的SimpleXML,它也附带DOM文档里面有一个HTML解析器的的它可以返回解析结果作为SimpleXML的,所以这是pretty兼容。

所以下面的扩展simplexml的一个HTML解析器:

 类HtmledSimpleXML扩展的SimpleXMLElement
{
    / **
     *解析元素内容为HTML,并返回
     *它的主体元素。
     *
     * @参数字符串$的XPath(可选)指定一个不同的元素返回
     *
     * @返回的SimpleXMLElement
     * /
    公共职能HTML($的XPath ='//体'){
        $ DOC =新的DOMDocument();
        $ doc-&GT; loadHTML($本);
        $ XML = simplexml_import_dom($ doc-&GT; documentElement);
        列表($体)= $ XML的&GT;的XPath($ XPath的);
        返回$体;
    }
}

它已经允许通过XPath查询指定要检索的混凝土构件,通常,它是从HTML中的标记这就是为什么它被设置为默认值。即使你的HTML没有该标记在那里,它实际上存在于DOM,因此该默认值是永远没有错。但无论如何,你的情况你感兴趣的img标签。由于simpelxml我们甚至可以将它输出直接作为XML和可以抽出来,即使手工创建的HTML。

用法示例:

  $ DOM =新HtmledSimpleXML($体);的foreach($ DOM-&GT;条目$进入){
    回声'&LT; A HREF =&GT;',$入门&GT;的标题,'&LT; / A&GT;',\\ n
         $入门级GT&;&内容 - GT; HTML('// IMG') - GT; asXML(),\\ n;
    ;
}

对单个项目的示范性输出则是:

 &LT; A HREF =&GT; Picadas,马尔科华雷斯13年1月5日&LT; / A&GT;
&LT; IMG SRC =htt​​p://farm9.staticflickr.com/8114/8709806523_3b8d7c0418_m.jpgWIDTH =240HEIGHT =161ALT =Picadas,马可·雷斯13年1月5日/&GT;

这应该是来pretty接近你在找什么。当然,你可以也只是AQUIRE图像元素,访问它的属性就像任何其他的SimpleXMLElement:

  $拇指= $入门&GT;&内容 -  GT; HTML('// IMG');
回声'标题:',$入门&GT;的标题,\\ n
     拇指:',$拇指['SRC'],\\ n
     '尺寸:',$拇指['宽度'],'X',$拇指['高度'],\\ n;

输出(纯文本):

 标题:Picadas,马尔科华雷斯13年1月5日
拇指:http://farm9.staticflickr.com/8114/8709806523_3b8d7c0418_m.jpg
尺寸:240×​​161

我希望这是有益至今。

href=\"http://stackoverflow.com/a/16372255/367456\">我伸出的SimpleXMLElement#2上次在<里面一个CSV数据HREF =htt​​p://stackoverflow.com/q/16281205/367456> 包含PHP的SimpleXML XPath搜索的价值在元素制表符分隔文本?的。

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

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

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