Read Facebook Data via Graph API using PHP

by Vincy. Last modified on July 6th, 2023.

In this tutorial, we will read data from Facebook using Graph API. This is an HTTP-based API using which we can post and get data to and from the Facebook social graph.

In this example, we will get primary data like id, name, and profile picture by sending the Facebook profile URL. We have a form input field to get Facebook profile URL. Using this URL, we can read Facebook data using PHP file_get_contents or cURL script.

View Demo

Getting Facebook Profile URL

This HTML code is used to show the form input to get Facebook profile URL from the user.

read_facebook_data

<form id="fromFacebookURL" action="" method="POST">
	<p>Enter Facebook Profile URL</p>
	<input type="text" name="profile_url" class="demoInputBox" /> <input
		type="submit" name="submit" id="btnRead" value="Read Data" />
</form>

PHP Code to Read Facebook Data

This code uses PHP file_get_contents() function to read Facebook data from the server. This function will return a JSON array, which will be decoded to display to the user.

<?php
if (isset($_POST["submit"])) {
    $url = $_POST["profile_url"];
    $facebook_data = file_get_contents($url . "?fields=id,name,picture");
    $data = json_decode($facebook_data);
}
?>

View DemoDownload

Vincy
Written by Vincy, a web developer with 15+ years of experience and a Masters degree in Computer Science. She specializes in building modern, lightweight websites using PHP, JavaScript, React, and related technologies. Phppot helps you in mastering web development through over a decade of publishing quality tutorials.

↑ Back to Top

Share this page