This article will teach you how to access public Facebook data using PHP. In this example we will be retrieving the talking about count from places around Raleigh, North Carolina that have Facebook pages.
- Write a function in the PHP programming language.
- Use that function to make a call to the Facebook API.
- Display the results in your web browser.
What You’ll Need
- Facebook developers account
- App Token
- Local development environment
Using the Graph API Explorer to Test Your Query
Lets say you want to get Facebook’s talking about count for places around Raleigh, NC. But before we start writing any code lets use Facebook’s Graph API Explorer to create and test our query.
As a logged in user, the default node is your account (“me”). The default query string shows the endpoints “id” and “name”. The results are returned as a “key” : “value” pair in JSON format. We want to gather talking about counts in the Raleigh, North Carolina area. To do that we are going to use the “search” node and a query string that tells Facebook what kind of data we’re looking for.
Here are the query results.
As you can see from the screenshot above our query returns a JSON object named “data”. The data object contains the query results we specified in the “fields” portion of the URL, “name” and “talking_about_count”. The “id” field is included by default.
Lets take a closer look at the URL. The entire URL looks like this:
https://graph.facebook.com/v2.9/search?q=&type=place¢er=35.7796,-78.6382&fields=name,talking_about_count&access_token=EAACEdEose0cBAAYsFyvCn7ZCzp3hntkLVbEJ9DZAgCTgqGEhhJqx0pZBCUhKpZCBNei3jDAnzYnaSS5O4biBJt5cUzhCdvZAxpzVKugKHYSYkR37BFHpbBi4jP7ZBSxUwZA3mSxpduCYhmhCqAgZBET992j4Toz6zk4hhhVVFm0ZCPRg3Coze7cFhKqTf42UMAQQZD The access token above is a temporary one automatically generated by the Graph API explorer. It is located directly above the search box. If you were to copy the above code and paste it into a new browser window this is what you’d see.
*Note, a valid access token is required to view the results in your browser. By the time you read this the one above will be expired.
It’s important to understand what each part of the query is doing. By understanding how it works you’ll be able to create your own queries based on your individual needs. Here’s a visual explanation of our query string.
There are 6 primary components to our search query.
- URL to the Graph API
- The node we are accessing.
- Query parameters
- Type of data we’re looking for.
- The geographic location of the data we’re looking for.
- The specific fields we want to see for each data point.
When we first opened the Graph API Explorer page the default node was set to the logged in user, aka “me”. We’ve changed to the the search node because we’re searching for all data points in a geographic region. The “?” character is a delimiter. Everything that follows “?” is part of the query string.
This string of information is sent to the Facebook server where it can interpret it using server side code. This is the essence of an API. If you were to send the same query string to a Twitter API URL it would have no idea what “talking_about_count” refers to. This is a data set that is unique to Facebook and only the code on Facebook’s servers will know what we want when we request “talking_about_count” data.
Each chunk after the “?” is part of the query string. The first part, “q=” is where you could add specific parameters to the query. We’ve included it here to demonstrate its use but for our purposes here you could’ve left it out and the results would’ve been the same. To demonstrate, if you only wanted the results that contained ‘North Carolina State Capitol’ then your query would’ve looked like this.
There are many different types of root nodes you can access using the Graph API. We are using “place” to return a list of places and their associated talking about counts.
In order to use the place node we need to define a location by either entering the address, the center point as a latitude, longitude pair or the place name in the q= part of the query.
We’re using the center parameter and defining it with geographic coordinates. You’re probably most familiar with each place being referred to as a Facebook Page.
These are business, location, brand, and event pages, not personal pages. Each page shares some of its data publicly like their name, cover photo, description, address, email and other information that will help people find them online.
We’re interested in the talking about count. Facebook’s talking about count is a great metric for measuring engagement. Unlike the semi static metric of likes and check ins, talking about count is highly dynamic and reflective of the individual pages’ current engagement. This number is the aggregate count of all the times that page has been liked, shared, mentioned, and commented on.
Accessing the Facebook Graph API using PHP
Handy as it may be, the Graph API Explorer isn’t very helpful if you’re wanting to use the data in an online application. In this simple example I’m going to show you how to retrieve the data using a custom PHP function. In later tutorials I’ll show
you ways to store that data in a MySQL database.
When you open this file in your browser window you’ll see something that looks like this.
Why would you want public Facebook data?
The reasons you want data are going to be determined by the goals of your application. For this example we wanted to see the talking about count for place pages around Raleigh, North Carolina.
Web applications that utilize data via APIs offer a rich user experience by aggregating information in more informative ways. Once you know how to get the data, then you just need to learn how to store, manipulate and display it. This will enable you to create unique applications using readily available information.
What can you do with public Facebook data?
The things you can do with the data are as broad as the reasons you’d want to do them. My educational background was in Geography, GIS specifically.
No, Geography is not just learning where places are in the world. Geographic Science is the study of spatial relations. Geographers ask questions that get answers about why things are located where they are, why are some areas rich and some poor, why is water distributed this way, etc. APIs’ provide rich data that can be used to create dynamic web applications for showing spatial relationships.
I taught myself how to code by creating an application I named “Heatery”. The Heatery App uses the same talking about count data shown in this example. I take that
same data and filter it using more custom PHP functions and display the results on a Google Map. Here’s the output.
The Heatery App uses the Facebook and Google Maps APIs’ to show the most talked about places in your search area.Heatery has evolved into a convenient way for me to monitor the progress of social media accounts my company manages. This data is showing the most talked about places around Oriental, NC.
This is the post I originally wrote on the topic of gathering public data using the Facebook Graph API. I hope you’ve found this tutorial informative. If you have any specific questions or if you’d like to discuss applying these principals to your website, contact me and I’ll get back in touch as soon as I can.
Until next time,
Subscribe for premium content
Subscribers receive members only content delivered straight to their inbox. Admission is free. Inspiration is priceless.