如何使用flickr photoset标题填充“选择”表单? (用PHP)

时间:2013-11-26 22:54:26

标签: php json photo flickr

我正在制作照片上传器,将照片直接上传到个人flickr帐户......现在我正在尝试拨打所有照片(按照标题)并填充我的select下拉列表跟他们。不幸的是,我一直在尝试的一切都没有用......所以我来这里是为了帮助你们有经验的人! ;)

这是我正在使用的代码: 来自我的class.flickr.php

文件
// Code being used for the purpose of calling photoset titles
public function getPhotosets() {

    // Function specific variables
    $flickr_api_call        = "http://api.flickr.com/services/rest/";
    $method                 = "flickr.photosets.getList";
    $nsid                   = 'my user id';

    $url_parameters = array(
        'method'                =>$method,
        'oauth_consumer_key'    =>$this->flickr_key,
        'user_id'               =>$nsid,
        'format'                =>$this->format,
        'nojsoncallback'        =>'1',
        'oauth_nonce'           =>$this->nonce,
        'oauth_timestamp'       =>$this->timestamp,
        'oauth_version'         =>'1.0',    
    );

    $parameters_string = "";

    foreach ( $url_parameters as $key=>$value ) $parameters_string .= "$key=" . urlencode( $value ) . "&";

    $url = $flickr_api_call . "?" . $parameters_string;

    $photosets = array();
    $data = json_decode(file_get_contents($url), true);

    if ( $data['stat'] != 'fail' ) {

        $photosets = $data['photosets']['photoset'];
        return $photosets;
    } else {

        return false;
    }

    var_dump( $photosets );
} // end getPhotosets

用于在我的主文档中调用getPhotosets方法的代码:

<select class="categorize-options">

<?php 
    require_once( 'class.flickr.php' );

    $flickr = new Flickr( 'api key', 'api shared secret' );
    $results = $flickr->getPhotosets();

    if ( !empty( $results )):
        foreach( $results as $photoset ):?>

            <option><?php echo $photoset['title']; ?></option>

        <?php endforeach;
    else:

        echo "This isn't working!!! :)";
    endif; 
?>
</select>

令我感到困惑的是,var_dump()else语句都没有显示......但是,两者都没有。获得一些有经验的投入将是一个很大的帮助...谢谢!

1 个答案:

答案 0 :(得分:0)

首先,虽然flickr文档说您不需要方法flickr.photosets.getList的授权......但这是错误的。您需要运行这样的授权功能:

// Get list of photosets (for their titles)
public function getPhotosets() {

    // Function specific variables
    $flickr_api_call        = $this->flickr_rest_call;
    $method                 = "flickr.photosets.getList";
    $nsid                   = 'user_id';
    $access_token           = "access_token";
    $access_token_secret    = "access_token_secret";

    $url  = "format=" . $this->format;
    $url .= "&method=" . $method;
    $url .= "&nojsoncallback=1";
    $url .= "&oauth_consumer_key=" . $this->flickr_key;
    $url .= "&oauth_nonce=" . $this->nonce;
    $url .= "&oauth_signature_method=" . $this->sig_method;
    $url .= "&oauth_timestamp=" . $this->timestamp;
    $url .= "&oauth_token=" . $access_token;
    $url .= "&oauth_version=1.0";
    $url .= "&user_id=" . urlencode( $nsid );

    $baseurl            = "GET&" . urlencode( $flickr_api_call ) . "&" . urlencode( $url );
    $hashkey            = $this->flickr_secret . "&" . $access_token_secret;
    $oauth_signature    = base64_encode( hash_hmac( 'sha1', $baseurl, $hashkey, true ));

    $url_parameters = array(
        'method'                =>$method,
        'oauth_consumer_key'    =>$this->flickr_key,
        'user_id'               =>$nsid,
        'format'                =>$this->format,
        'nojsoncallback'        =>'1',
        'oauth_nonce'           =>$this->nonce,
        'oauth_timestamp'       =>$this->timestamp,
        'oauth_signature_method'=>$this->sig_method,
        'oauth_version'         =>'1.0',
        'oauth_token'           =>$access_token,
        'oauth_signature'       =>$oauth_signature
    );

    /* Now that we have encoded the parameters for our ouath_signature
    * and have reformated them for the url we need to send... we must
    * re-urlencode them too. */

    $parameters_string = "";
    foreach ( $url_parameters as $key=>$value )
        $parameters_string .= "$key=" . urlencode( $value ) . "&";

    $url = $flickr_api_call . "?" . $parameters_string;

注意:每次使用nsid时,您需要urlencode两次值!原因:@,如果只编码一次将返回此%40,但为了保持%您必须编码两次,结果如下:%2540

其次,将来自json响应的调用从<option><?php echo $photoset['title']; ?></option>更改为:<option><?php echo $photoset['title']['_content']; ?></option>

这就是我能够实现这一目标的方法......如果有更好,更有效的方法,我很乐意听到它。