Get Facebook Page Feed
, (*1)
It is simple wrapper class written in php to fetch posts from certain Facebook page., (*2)
Currently I am using Facebook graph API with cool guzzle and dotenv, (*3)
Tested in PHP 7.0, 7.1, 7.4, 8.0, (*4)
Requirement
Installation
Step 1: Getting Facebook App
- Go to Facebook developer website
- Click "Add a New App" and fill in details
- On your Dashboard, get the "App ID" and "App Secret"
- Yeah, you are ready to code
Step 2: Install from composer
composer require xmhafiz/fb-page-feed
Alternatively, you can specify as a dependency in your project's existing composer.json file, (*5)
{
"require": {
"xmhafiz/fb-page-feed": "^1.2"
}
}
Usage
After installing, you need to require Composer's autoloader and add your code., (*6)
$data = fb_feed()->setAppId($fbAppId)
->setSecretKey($fbSecretKey)
->setPage($fbPageName)
->fetch();
$data = fb_feed()->fetch(); // must have .env
$config = [
'secret_key' => '',
'app_id' => '',
'page_name' => '',
'access_token' => '',
];
$data = fb_feed($config)->fetch();
$data = FbFeed::make($config)->fetch();
Default (maximum post is 100)
$data = fb_feed()->setAppId($fbAppId)
->setSecretKey($fbSecretKey)
->setPage($fbPageName)
->fetch();
Set Module
$data = fb_feed()->setAppId($fbAppId)
->setSecretKey($fbSecretKey)
->setPage($fbPageName)
->setModule("feeds")
->fetch();
Custom Maximum Post Shown
// only show 5 post maximum
$data = fb_feed()->setAppId($fbAppId)
->setSecretKey($fbSecretKey)
->setPage($fbPageName)
->feedLimit(5)
->fetch();
Filter By Keyword
// only show 5 post maximum
$data = fb_feed()->setAppId($fbAppId)
->setSecretKey($fbSecretKey)
->setPage($fbPageName)
->findKeyword("#LaravelCommunity")
->fetch();
Change Request Field
// only show 5 post maximum
$data = fb_feed()->setAppId($fbAppId)
->setSecretKey($fbSecretKey)
->setPage($fbPageName)
->fields("id,message") //default 'id,message,created_time'
->fields(["id","message"]) // can be array
->fetch();
To get owner's fb page feed using userAccessToken
Details to get userAccessToken
can refer here., (*7)
$data = fb_feed()
->setAccessToken($userAccessToken)
->setPage($fbPageName)
->fetch();
Code Example
Change the $fbSecretKey
and $fbAppId
based on the "App ID" and "App Secret" in Step 1, (*8)
<?php
require_once 'vendor/autoload.php';
$fbSecretKey='580c7...';
$fbAppId='237...';
$fbPageName='LaravelCommunity';
$response = fb_feed()->setAppId($fbAppId)->setSecretKey($fbSecretKey)->setPage($fbPageName)->findKeyword("#tutorial")->fetch();
//or
$response = fb_feed()->setCredential($fbAppId, $fbSecretKey)->setPage($fbPageName)->findKeyword("#tutorial")->fetch();
header('Content-type: application/json');
echo json_encode($data);
Using ENV
FB_SECRET_KEY=absbo123o233213
FB_APP_ID=123123123123
FB_PAGENAME=pagename
Then, Just, (*9)
$response = fb_feed()->findKeyword("#AirSelangor")->fetch();
Method
Method |
Param |
Description |
setAppId |
String |
FB Application ID (Default is in .env) |
setSecretKey |
String |
FB Application Secret ID (Default is in .env) |
setCredential |
String, String |
Set Both Secret and App Id (Default is in .env) |
fields |
String |
List of Attributes (Default : id,message,created_time,from,permalink_url,full_picture ) |
setPage |
String |
Set Page Name (Default is in .env) |
findKeyword |
String | Array |
Filter String by certain Keywords |
feedLimit |
Integer |
Set result count limit |
Result
You should getting data similarly like below:, (*10)
{
"error": false,
"status_code": 200,
"data": [
{
"id": "365155643537871_1321961834523909",
"message": "The APPDATA or COMPOSER_HOME environment variable must be set for composer to run correctly\"\nwhat bug?",
"created_time": "2017-05-14T15:45:30+0000",
"from": {
"name": "Phạm Nam",
"id": "424522607913714"
},
"permalink_url": "https://www.facebook.com/LaravelCommunity/posts/1321961834523909"
},
{
"id": "365155643537871_1766722286972894",
"message": "https://www.youtube.com/channel/UCQ6fynaWa81JqPzOBMmBTSw\nLaravel BAsic To Advance LEarning Step by STep",
"created_time": "2017-05-13T07:18:53+0000",
"from": {
"name": "Wasiim Khan",
"id": "1766622610316195"
},
"permalink_url": "https://www.facebook.com/photo.php?fbid=1766722286972894&set=o.365155643537871&type=3",
"full_picture": "https://scontent.xx.fbcdn.net/v/t1.0-9/18403359_1766722286972894_2242179936023685636_n.jpg?oh=679c3e230ef55759ebe0e42239318e27&oe=597B1F7D"
},
{
"id": "365155643537871_1320698884650204",
"message": "ai cho em hou noi nay bi sao vay.\nIntegrity constraint violation: 1048 Column 'order' cannot be null",
"created_time": "2017-05-13T05:05:27+0000",
"from": {
"name": "Trong Phạm Sr.",
"id": "891899864284241"
},
"permalink_url": "https://www.facebook.com/LaravelCommunity/posts/1320698884650204"
}
]
}
Notes
To use 'Page Public Content Access', your use of this endpoint must be reviewed and approved by Facebook. To submit this 'Page Public Content Access' feature for review please read our documentation on reviewable features: https://developers.facebook.com/docs/apps/review., (*11)
To use with dotenv
- Look at example code
- copy the
env.example
file to .env
and make sure fill all the required environment variable (FB_SECRET_KEY
, FB_APP_ID
, FB_PAGENAME
)
License
Licensed under the MIT license, (*12)