Keramot UL Islam
Posted on April 2, 2020
With the help of Facebook rest API, we can create a Facebook feed on our website. Nowadays every business has a Facebook page. A Facebook feed on the website may help to boost business.
In this article, I’ll show how to get data from a Facebook page.
For fetching data from Facebook you need two things:
- Page ID
- Access Token
Get these data from Here.
Facebook provides some endpoints to access data. But for authentication, you need those two pieces of information.
$page_id = 'your facebook page id';
$token = 'your access token';
$url = 'https://graph.facebook.com/v4.0/' . $page_id . '/posts?' . '&access_token=' . $token;
$fb_posts = wp_remote_get( $url );
$posts = wp_remote_retrieve_body( $fb_posts );
print_r( json_decode( $posts ) );
Now You are getting data but only basic data. For making a feed you need more depth information right?
You can also add a query in the endpoint.
$url_queries = 'fields=status_type,created_time,from,message,story,full_picture,permalink_url,attachments.limit(1){type,media_type,title,description,unshimmed_url},comments.summary(total_count),reactions.summary(total_count)';
$page_id = 'your facebook page id';
$token = 'your access token';
$url = 'https://graph.facebook.com/v4.0/' . $page_id . '/posts?' . $url_queries . '&access_token=' . $token;
$fb_posts = wp_remote_get( $url );
$posts = wp_remote_retrieve_body( $fb_posts );
print_r( json_decode( $posts ) );
Now with some HTML and CSS, you can make a pretty visualization of the feed.
Congratulations, you have created the feed. But if you think deeply, you will find that it’s not good for performance. Because at every page refresh it’ll fetch data from the Facebook database, which is time-consuming.
So, you need to cash your data for better performance. WordPress has a mechanism called transient
. It is used to cash data for a certain period of time...
Read More from my original article
________________________
Related Post:
Posted on April 2, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.