Last active
September 24, 2020 15:55
-
-
Save josesayago/47ef06fb094fdc0893a9 to your computer and use it in GitHub Desktop.
Getting Facebook username using Graph API v2.0
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Above code will not work until pass access token like below:
$fb_graph = 'https://graph.facebook.com/'.$fb_userID."?access_token=YOUR_ACCESS_TOKEN";