Eric Eric - 1 month ago 9
PHP Question

Facebook scope PHP

I'm trying to work with Facebook's default PHP website integration and authentication.
When a user logs in, I'm able to request permissions to get various information from the user.





This question is kind of old. Please take note of harry33166's comment.

"If someone using Facebook SDK v4.4 is stumbling across this question, the answer by Dhiraj won't >work in that case as there is a small change. You don't have to include redirect_uri in the array >passed to getLoginUrl.

As per v4.4, getLoginUrl should work like:


$loginUrl = $facebook->getLoginUrl(
array(
'scope' => 'email,user_checkins, user_birthday'
)
);



The various scopes can be found here: Facebook permissions."





It was really easy to figure out how to do it with the JavaScript one, but I'm confused where to specify the scope in the PHP example.

Basically, I just want to request for permission to access their email & location and maybe some other information.

Here is the code I'm working with:
(It's modified slightly, but it is almost an exact match to what they had.)

<?php
define('YOUR_APP_ID', '---------');
define('YOUR_APP_SECRET', '------------');

//Uses the PHP SDK. Download from https://github.com/facebook/php-sdk
require 'facebook.php';

$facebook = new Facebook(array(
'appId' => YOUR_APP_ID,
'secret' => YOUR_APP_SECRET,
));

$FBUID = $facebook->getUser();
?>

<?php if ($FBUID) {
$userInfo = $facebook->api('/' + $FBUID); ?>

<?php $FBName = $userInfo['name'];?>
<!--
Welcome <? // = $userInfo['name'] ?> -->

<?php } else { ?>
<div id="fb-root"></div>
<fb:login-button></fb:login-button>
<?php } ?>

<script>
window.fbAsyncInit = function() {
FB.init({
appId : '---------------',
status : true,
cookie : true,
xfbml : true,
oauth : true,
});

FB.Event.subscribe('auth.login', function(response) {
window.location.reload();
});
};

(function(d){
var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
d.getElementsByTagName('head')[0].appendChild(js);
}(document));
</script>

Answer

This is how you typically specify scope in PHP (check the documentation):

$facebook = new Facebook(array(
    'appId'  => AppInfo::appID(),
    'secret' => AppInfo::appSecret(),
));

$loginUrl = $facebook->getLoginUrl(
    array(
        'scope'         => 'read_stream,publish_stream,publish_actions,manage_pages,email,user_checkins',
        'redirect_uri'  => $redirectURI
    )
);

I hope this helps.