【问题标题】:Why won't the images that are grabbed from flickr display?为什么从 flickr 抓取的图像不显示?
【发布时间】:2013-10-18 14:38:19
【问题描述】:

我正在学习 flickr api,同时在项目中使用它,但遇到了一个问题……我在 flickr 检索照片数据的地方有它(如下所示)

但它本身不显示图像......我认为这是因为它没有使用下面的src=" " 信息构建<img /> 标签是我正在尝试使用的代码。 ..

(我的 class.flickr.php 代码文件)

class Flickr{

    private $flickr_key;
    private $flickr_secret;
    private $format = 'json';

    // Setting up flickr_key and flickr_secret
    public function __construct( $flickr_key ) {

        $this->flickr_key = $flickr_key;
    }

    public function searchPhotos( $query = '', $tags = '' ){ // Begin searchPhotos 

        $urlencoded_tags = array( 'animals', 'design', 'phones');

        if ( !empty( $args )) {

            $tags_r = explode( ',', $tags );
            foreach ( $tags_r as $tag ) {

                $urlencoded_tags[] = urlencode( $tag );
            }
        }

        // Construct the url
        $url  = 'http://api.flickr.com/services/rest/?';
        $url .= 'method=flickr.photos.search';
        $url .= '&text=' . urlencode( $query );
        $url .= '&tags=' . implode( ',', $urlencoded_tags );
        $url .= '&sort=relevance';
        $url .= '&safe_search=1';
        $url .= '&content_type=4';
        $url .= '&api_key=' . $this->flickr_key;
        $url .= '&format' . $this->format;
        $url .= '&per_page=10';

        // Calling url using curl
        $curl = curl_init();

        curl_setopt_array( $curl, array(

            CURLOPT_TIMEOUT => 120,
            CURLOPT_URL => $url,            
        ));

        if ( !curl_exec( $curl )) {

            die ( 'Error: "' . curl_error( $curl ) . '" - Code: ' . curl_errno( $curl ));
        }

        // Get search results
        $result = file_get_contents( $url );

        // Remove the unneccessary strings that wraps the result returned from the API
        $json = substr( $result, strlen( "jsonFlickrApi("), strlen( $result ) - strlen( "jsonFlickrApi(") - 1 );

        $photos = array();
        $data = json_decode( $json, true );

        // Check if the status didn't fail
        if ( $data['stat'] != 'fail' ) {

            /** Return only the data for the photos 
                as that's the only thing that we need
            */
            $photos = $data['photos']['photo'];
            return $photos;
        } else {

            return false;       
        }
    } // end searchPhotos

}

(我在页面模板文件中使用的方法调用)

<?php // Flickr search photos test

require_once( 'class.flickr.php' );

global $flickr_key;

$flickr = new Flickr( 'this_is_my_api-key_placeholder...' );

$query = "Event Photo Uploadr";

$results = $flickr->searchPhotos( $query, $tag );
if ( !empty( $results )) {

    foreach( $results as $photo ) {

        $src = "http://farm" . $photo['farm'] . ".static.flickr.com/" . $photo['server'] . '/' . $photo['id'] . '_' . $photo['secret'] . '.jpg';
        ?>

        <img  src="<?php echo $src; ?>"/>

    <?php }                 
}

正如我所说,我是 flickr api 的新手...到目前为止,我很幸运能够做到这一点(感谢 stackoverflow、Wordpress Answers 和开发者朋友),因此非常感谢任何有用的输入! ;)

【问题讨论】:

  • 您是否尝试过手动点击您正在构建的网址?例如剪切并粘贴一个到浏览器地址栏中?如果这不起作用,那么您没有正确构建 url,必须弄清楚它应该是什么。
  • @MarcB,刚刚测试过,它的格式正确并可以抓取图像。但是谢谢我什至没有想过要测试它!仍在研究我的调试技能。 ;)
  • @MarcB,它仍然没有在我的网站上显示链接或图像。
  • 所以备份代码链。例如 var_dump($results) 在您尝试从 api 获取数据之后。看看事情从哪里开始失败。照原样,您的代码只是假设成功,当您使用外部资源时,这不是一件好事。你应该假设一切都会失败,并将成功视为一个惊喜。
  • @MarcB:我在我的 $json var 上做了一个var_dump();,就像这样......var_dump(json_decode( $json, true )); 它返回null。任何想法如何解决这个问题?

标签: php flickr


【解决方案1】:

好的,这听起来很傻,但是,真正的问题是我错过了=。我不是在开玩笑,我有这个$url .= '&amp;format' . $this-&gt;format;(它制作了api,默认情况下返回xml)应该是这个$url .= '&amp;format=' . $this-&gt;format;(这是获取我的$format var的正确方法)。我还清理了我的代码...

(主 flickr 功能文件)

<?php

class Flickr{

private $flickr_key;
private $flickr_secret;
private $format = 'json';

// Setting up flickr_key and flickr_secret
public function __construct( $flickr_key ) {

    $this->flickr_key = $flickr_key;
}

public function searchPhotos( $query = '', $tags = '' ){ // Begin searchPhotos 

    $urlencoded_tags = array( 'animals', 'design', 'phones');

    if ( !empty( $args )) {

        $tags_r = explode( ',', $tags );
        foreach ( $tags_r as $tag ) {

            $urlencoded_tags[] = urlencode( $tag );
        }
    }

    // Construct the url
    $url  = 'http://api.flickr.com/services/rest/?';
    $url .= 'method=flickr.photos.search';
    $url .= '&text=' . urlencode( $query );
    $url .= '&tags=' . implode( ',', $urlencoded_tags );
    $url .= '&sort=relevance';
    $url .= '&safe_search=1';
    $url .= '&content_type=4';
    $url .= '&api_key=' . $this->flickr_key;
    $url .= '&format=' . $this->format;
    $url .= '&per_page=10';

    // Get search results
    $result = file_get_contents( $url );

    // Remove the unnecessary strings that wraps the result returned from the API
    $json = substr( $result, strlen( "jsonFlickrApi(" ), strlen( $result ) - strlen( "jsonFlickrApi(" ) - 1 );

    $photos = array();
    $data = json_decode( $json, true );

    // Check if the status didn't fail
    if ( $data['stat'] != 'fail' ) {

        // Return only the data for the photos as that's the only thing that we need
        $photos = $data['photos']['photo'];
        return $photos;

    } else {

        return false;       
    }
} // end searchPhotos

}

我在方法调用中唯一更改的是添加一个 alt 标记,例如 alt="&lt;?php echo $photo['title']?&gt;",并在 else 语句中添加一个错误消息,echo "Failed to construct url successfully";

【讨论】:

    猜你喜欢
    • 2015-09-30
    • 1970-01-01
    • 1970-01-01
    • 2020-11-22
    • 2015-12-08
    • 2014-07-15
    • 1970-01-01
    • 1970-01-01
    • 2022-12-14
    相关资源
    最近更新 更多