Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get user's name from Facebook Graph API

Tags:

php

facebook

I would like to know how is it possible to retrieve a string from an external page.

For example: In a PHP website, the user sends a facebook id, ex: 1157251270

And the website returns the name from http://graph.facebook.com/1157251270.

I hope I made it clear.

Thank you

like image 930
John Doe Avatar asked Apr 26 '11 10:04

John Doe


People also ask

What data can I get from Facebook graph API?

The Graph API is the primary way to get data into and out of the Facebook platform. It's an HTTP-based API that apps can use to programmatically query data, post new stories, manage ads, upload photos, and perform a wide variety of other tasks.

How do I use Facebook graph API and extract data?

To use the Graph API Explorer tool, go to developers.facebook.com/tools/explorer. Generate the access token you need to extract data from the Facebook Graph API by selecting Get User Access Token from the App Token drop-down menu under the name of your app.

Is Facebook graph API deprecated?

Applies to all versions. Facebook Analytics will no longer be available after June 30, 2021. Additionally, we are deprecating the App Dashboard Plugin for Marketing API. For more information visit the Business Help Center.


1 Answers

The Graph API returns JSON strings, so you can use:

echo json_decode(file_get_contents('http://graph.facebook.com/1157251270'))->name;

or more verbose:

$pageContent = file_get_contents('http://graph.facebook.com/1157251270');
$parsedJson  = json_decode($pageContent);
echo $parsedJson->name; // Romanos Fessas

See json_decode — Decodes a JSON string

like image 133
Gordon Avatar answered Oct 01 '22 15:10

Gordon