PHP search facebook posts using Graph API (JSON)

Facebook offers a flexible and easy to use API called Graph, graph is simply a URL that once you open in a browser or within your code it return a JSON (JavaScript Object Notation ) string, in this tutorial you will learn how to use php and facebook graph API to access public posts and parse JSON results with php, We will build together a php  search engine to parse JSON returned by Facebook Graph and display results to visitors

view this in action Demo

If you don’t know what is Json, a Json string looks like this

{
"name": "John Smith",
"Age": "30",
"username": "Jsith",
"location": "Chicago"
}

Our search engine will be 2 pages

index.html
search.php

index.html content

<form action="search.php" method="post">
<input type="text" name="keyword">
<input type="submit" value="search">
</form>

in the above step we build a basic html page with a form that contains text field and submit button to submit the form to our search.php, then once a user type a keyword and hit submit we want to take that keyword and search facebook public posts suing their graph API.

Now in search.php We need to catch the submitted keyword from index.html

$keyword = $_POST['keyword'];

The graph url which will return a JSON string is https://graph.facebook.com/search
the url accepts many parameters but we will use the basic ones such as type and q (query)
you can see what the JSON results would look like before we parse them with PHP here:
https://graph.facebook.com/search?q=hello&type=post
Now lets set the graph url we will be opening with php

// set graph url
$graph_url = "https://graph.facebook.com/search?";
// add parameters to graph url
$graph_url .= "&amp;type=posts";
$graph_url .= "&amp;q=$keyword";

in the above step we have a prepared graph url with type parameter and q parameter, the q parameter is the keyword we want to search for and type is the kind of contents we need, we are going to search for posts, aren’t we?
Now that facebook graph url is ready with the keyword we want to search for, lets have php open it so it returns the JSON results

$results = file_get_contents( $graph_url );


$result
will be the variable holding the JSON data returned by Facebook Graph, We now need to parse JSON and display it as readable text or HTML content, We will parse it using php function – json_decode() -

$json = json_decode($results);

the json_decode() function in the way used above converts the JSON string into accessible objects, We will need to do a foreach loop to access those objects and print them, lets do that now

foreach( $json->data as $show ) {
echo $show->from->name . "\n";
echo $show->message . "\n";
echo $show->created_time . "\n"
echo "<hr>";
}

Now you might be wondering why used $json->data and not $json alone, the answer is because all the returned results in JSON are inside an array called “data” and also you may have noticed to show the name of poster I’m doing $json->from->name is because “from” is an array and name is an object inside that array you can see how it looks like here https://graph.facebook.com/?q=hello&type=post, In this tutorial I’m only showing name of person who made the post, message which is post content, created time is the timestamp of the post date you can access many more such as Facebook user ID , description, comments count etc. By simple doing $show->xx where xx is the object name! I hope this helps you in your next Facebook app project! you can what we have created in action here

search.php all together will look like this

$keyword = $_POST['keyword'];
$graph_url = "https://graph.facebook.com/search?";
$graph_url .= "&type=posts";
$graph_url .= "&q=$keyword";
$results = file_get_contents( $graph_url );
$json = json_decode($results);
foreach( $json->data as $show ) {
echo $show->from->name . "\n";
echo $show->message . "\n";
echo $show->created_time . "\n"
echo "<hr>";
}
Post comment as twitter logo facebook logo
Sort: Newest | Oldest

Hi thank you for your amazing example can you sent me the source to my email because when am using the sample that you post here i cant take results using keyword like an example alpha+beta samething is wrrong thank you for your time

Hello Kostas, Sorry for the late reply, but incase you still need it here is the php file http://pastebin.com/LaefdmUm and here is the html form http://pastebin.com/BraZhLiD those 2 files make up the Facebook graph search demo I have up

How would you use multiple keywords in the same search? Say I'm searching for keywords easter and ohio to both be found in the same post. BTW, putting a space in your demo search keyword field causes the script to crash.

Hello Ryan, My code is just a sample it isn't the best, if you want to use multiple keywords like "Easter and ohio" you need to have the script encode the string so it converts spaces into +, right now if you type ohio+easter it will work.