开发者

retrieve facebook details albums

开发者 https://www.devze.com 2023-04-05 23:33 出处:网络
I\'m tried retrieve the album details with this code: $facebook = new Facebook(array( \'appId\'=> \'\',

I'm tried retrieve the album details with this code:

$facebook = new Facebook(array(
  'appId'  => '',
  'secret' => '',
  'cookie' => true,
));

Facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYPEER] = false; 
$user = $facebook->getUser();
if ($user) {
  try {
    $user_profile = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    error_log($e);
    $user = null;
  }
}
if ($user) {
$access_token = $facebook->getAccessToken();
$album = file_get_contents("https://graph.facebook.com/me/albums/?access_token={$access_token}");
    pr开发者_StackOverflow中文版int_r($album); 
}else {
    $loginUrl = $facebook->getLoginUrl(
            array('canvas' => 1,
                 'fbconnect' => 0,
                 'req_perms' => 
                  'user_photos,friends_photos'
            ));
}

the code above returns an empty array,why? Can someone point my error?

Thanks in advance!


Well you need to understand what you are doing here not just follow a tutorial (or a combination of tutorials!):

  1. Don't use: Facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYPEER] = false;
  2. The first if($user) statement is not needed if you don't really need the user data $user_profile
  3. If there's a current user no need for getting the access token since the SDK will take care of that
  4. why using file_get_contents if you are using the SDK?!
  5. The new SDK implementation (following the OAuth 2.0) requires you to use scope instead of req_perms
  6. after creating the login URL you need to actually use it!

Here is a better code to get you started:

$facebook = new Facebook(array(
  'appId'  => '',
  'secret' => '',
  'cookie' => true,
));

$user = $facebook->getUser();
if ($user) {
  try {
    $user_albums = $facebook->api('/me/albums');
  } catch (FacebookApiException $e) {
    error_log($e);
    $user = null;
  }
} else {
    $loginUrl = $facebook->getLoginUrl(array('scope' => 'user_photos,friends_photos'));
    echo "<script>top.location.href = '" . $loginUrl . "';</script>";
}

I strongly recommend that you read the documentation before just trying some random code.

0

精彩评论

暂无评论...
验证码 换一张
取 消