简体   繁体   English

Graph API获取所有照片

[英]Graph API Get all photos

i have a website where people can upload pictures to be rated/liked etc. I am developing a function where if they don't want to upload pictures they can import them from there Facebook account. 我有一个网站,人们可以在其中上传要评分/喜欢的图片等。我正在开发一项功能,如果他们不想上传图片,可以从那里的Facebook帐户导入图片。

I can login/logout the user from Facebook using the PHP Graph API but i am not sure on the request i need to get the logged in persons photos. 我可以使用PHP Graph API从Facebook登录/注销用户,但是我不确定是否需要获取登录的人的照片。 I have setup the login url to the user_photos permissions. 我已将登录URL设置为user_photos权限。

I had a feature like this a few years ago where i would use the FQL to query the images but now i am using the new Facebook v4 Graph API. 几年前,我有一个类似的功能,我可以使用FQL查询图像,但现在我正在使用新的Facebook v4 Graph API。

Any help will be appreciated. 任何帮助将不胜感激。

FacebookSession::setDefaultApplication( 'ID', 'SECRET' );
$helper = new     FacebookRedirectLoginHelper('URL');
$permissions = array(
'user_photos'
);
try {
if ( isset( $_SESSION['access_token'] ) ) {
    // Check if an access token has already been set.
    $session = new FacebookSession( $_SESSION['access_token'] );
} else {
    // Get access token from the code parameter in the URL.
    $session = $helper->getSessionFromRedirect();
}
} catch( FacebookRequestException $ex ) {

// When Facebook returns an error.
print_r( $ex );
} catch( \Exception $ex ) {

// When validation fails or other local issues.
print_r( $ex );
}
if ( isset( $session ) ) {

// Retrieve & store the access token in a session.
$_SESSION['access_token'] = $session->getToken();
// Logged in
echo 'Successfully logged in!';
} else {

// Generate the login URL for Facebook authentication.
$loginUrl = $helper->getLoginUrl($permissions);
echo '<a href="' . $loginUrl . '">Login</a>';
}

After user has made the login you can get photos like so: 用户登录后,您可以获取如下照片:

FacebookSession::setDefaultApplication($APP_ID, $APP_SECRET);  

$session = new FacebookSession($access_token);

$response = (new FacebookRequest($session, 'GET', '/me/photos?fields=source&limit=100'))->execute();
$object = $response->getGraphObject();
$arr = self::convertToAssoc($object);

put in the limit param your value. 将极限值放入您的值。

here is how you convert the facebook-object: 这是如何转换facebook-object:

public static function convertToAssoc($graphObject){
        $arr = $graphObject->asArray();
        if ( !array_key_exists('data', $arr) ){
            $arr['data'] = '';
        }
        $arr_data = $arr['data'];

        $data = [];
        foreach($arr_data as $item){
            $data[] = get_object_vars($item);   
        }

        return $data;    
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM