Skip to content

Instantly share code, notes, and snippets.

@josesayago
Last active September 24, 2020 15:55
Show Gist options
  • Save josesayago/47ef06fb094fdc0893a9 to your computer and use it in GitHub Desktop.
Save josesayago/47ef06fb094fdc0893a9 to your computer and use it in GitHub Desktop.
Getting Facebook username using Graph API v2.0
<?php
/**
* file_get_contents
*/
// Get User ID from FB SDK, this example uses Facebook PHP SDK v4
$fb_userID = $fb_data['me']['id'];
$fb_graph = 'https://graph.facebook.com/'.$fb_userID;
$fb_request = file_get_contents($fb_graph); // Replace with curl_get_contents if this does not work
$fb_response = json_decode($fb_request);
// Print username
print $fb_response->username;
/**
* CURL (Mediatemple)
*/
public function curl_get_contents( $url ) {
$request = curl_init();
curl_setopt($request, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($request, CURLOPT_HEADER, 0);
curl_setopt($request, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($request, CURLOPT_URL, $url);
curl_setopt($request, CURLOPT_FOLLOWLOCATION, TRUE);
$data = curl_exec($request);
curl_close($request);
return $data;
}
?>
@JavedBoqo
Copy link

Above code will not work until pass access token like below:
$fb_graph = 'https://graph.facebook.com/'.$fb_userID."?access_token=YOUR_ACCESS_TOKEN";

@jatavpawan
Copy link

not working

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment