In this tutorial, we are going to read data from Facebook by 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 are going to get some basic data like id, name, and profile picture by sending Facebook profile URL. We have a form input field to get Facebook profile URL. Using this URL we can read Facebook data by using PHP file_get_contents or cURL script.
This HTML code is used to show the form input to get Facebook profile URL from the user.
<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>
This code uses PHP file_get_contents() function to read Facebook data from server. This function will return 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);
}
?>