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!):
- Don't use:
Facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYPEER] = false;
- The first
if($user)
statement is not needed if you don't really need the user data$user_profile
- If there's a current user no need for getting the access token since the SDK will take care of that
- why using
file_get_contents
if you are using the SDK?! - The new SDK implementation (following the OAuth 2.0) requires you to use
scope
instead ofreq_perms
- 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.
精彩评论