【问题标题】:How to populate a "select" form with flickr photoset titles? (with PHP)如何使用 flickr 照片集标题填充“选择”表单? (使用 PHP)
【发布时间】:2013-12-12 08:13:41
【问题描述】:

我正在创建一个照片上传器,可以将照片直接上传到个人 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 语句都没有显示......但是,其他任何东西都没有。获得一些有经验的意见会很有帮助...谢谢!

【问题讨论】:

    标签: php json photo flickr


    【解决方案1】:

    首先,虽然 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 响应中的调用从 &lt;option&gt;&lt;?php echo $photoset['title']; ?&gt;&lt;/option&gt; 更改为 &lt;option&gt;&lt;?php echo $photoset['title']['_content']; ?&gt;&lt;/option&gt;

    这就是我能够做到这一点的方法...如果有更好、更有效的方法来做到这一点,我很想听听。

    【讨论】:

      猜你喜欢
      • 2013-04-25
      • 1970-01-01
      • 1970-01-01
      • 2015-11-30
      • 2013-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多