1

我目前正在尝试将 flickr api 添加到我的自定义插件中......并且失败了。:( 我试图使用本教程Flickr API 入门来学习基本的代码结构,然后我将参考 flickr 的应用程序花园以获取文档来做我想做的事情;但是,它不起作用。

我有什么...我构建了一个新的 PHP 文件并将其命名为“class.flickr.php”并添加了教程中列出的代码...

<?php

class Flickr{

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

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

    $this->myApiKey = $flickr_key;
    $thix->myApiSecret = $flickr_secret;
}

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

    $urlencoded_tags = array();

    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_RETURNTRANSFER => 0,
        CURLOPT_TIMEOUT => 120,
        CURLOPT_URL => $url,            
    ));

    if ( !curl_exec( $curl )) {

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

    print_r( curl_getinfo( $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

}

然后尝试使用以下代码在我的页面模板(与“class.flickr.php”位于同一文件夹中)中调用上述方法...

<?php // Flickr search photos test

    require_once( 'class.flickr.php' );

$flickr = new Flickr( $api_key, $api_secret);

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

    foreach( $results as $photo ) {

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

    <img  src="<?php echo $src; ?>" alt="<?php echo $photo['title']; ?>" />

    <?php }                 
}

但是,唉,我得到的只是一个空白屏幕。任何帮助,将不胜感激。;)

4

1 回答 1

0

这是我写的关于一些主机file_get_contents最近在通话中变得更加严格的内容。基本上,您需要使用 curl 调用,而不仅仅是原始的file_get_contents.

这是我为解决此问题而给出的答案-它应该可以帮助您解决您的情况(我在其中包含了一些示例代码): Wordpress simplexml_load_file() parse error

于 2013-10-17T13:55:42.967 回答