Friday, 18 October 2013

PHP - Facebook Family Member Names Pulling

<?php
$APP_ID = '';
$APP_SECRET = '';
$ACCESS_URL = 'http://localhost/facebook/'; // This would be the Canvas URL or Web Request URL
$code = isset($_REQUEST["code"])?$_REQUEST["code"]:"";

// Authenticated USER
if(empty($code)) {
    $dialog_url = 'https://www.facebook.com/dialog/oauth?client_id='. $APP_ID . '&redirect_uri=' . urlencode($ACCESS_URL) ;
    echo("<script>top.location.href='" . $dialog_url . "'</script>");
}

// Get User Access Token
$token_url = 'https://graph.facebook.com/oauth/access_token?client_id='.$APP_ID .'&redirect_uri='.urlencode($ACCESS_URL).'&client_secret='.$APP_SECRET.'&code='.$code;

// Acess Token Based on Token URL"
$access_token = substr(file_get_contents($token_url), 13);

// Get Family Members Details using FQL Queries
$fqlQueryRequestUrl = 'https://graph.facebook.com/'
.'fql?q={"all+friends":"SELECT+name+FROM+family+WHERE+profile_id=me()",'
. '"my+name":"SELECT+name+FROM+user+WHERE+uid=me()"}'
. '&access_token=' . $access_token;
$fqlQueryResult = file_get_contents($fqlQueryRequestUrl);
$fqlQueryObject = json_decode($fqlQueryResult, true);

?>
<h3><?php print $fqlQueryObject['data'][1]['fql_result_set'][0]['name'];?> Family Members List</h3>
<ul>
<?php foreach($fqlQueryObject['data'][0]['fql_result_set'] as $eachFamilyMember) { ?>
    <li><?php print $eachFamilyMember['name']; ?></li>
<?php } ?>
</ul>

No comments:

Post a Comment

Thank you so much for providing your valuable feedback. I will will look into them and update my skills & technologies accordingly.