https://github.com/xmhafiz/fb-page-feed
Simple PHP package to get any Facebook Page posts
https://github.com/xmhafiz/fb-page-feed
facebook-graph-api facebook-page fb-page-feed hacktoberfest php
Last synced: 6 months ago
JSON representation
Simple PHP package to get any Facebook Page posts
- Host: GitHub
- URL: https://github.com/xmhafiz/fb-page-feed
- Owner: xmhafiz
- Created: 2017-03-25T17:35:48.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2023-04-19T19:44:58.000Z (about 3 years ago)
- Last Synced: 2025-10-20T15:42:11.429Z (9 months ago)
- Topics: facebook-graph-api, facebook-page, fb-page-feed, hacktoberfest, php
- Language: PHP
- Homepage:
- Size: 123 KB
- Stars: 66
- Watchers: 3
- Forks: 14
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Get Facebook Page Feed
[](https://travis-ci.org/xmhafiz/fb-page-feed)
[](https://codecov.io/gh/xmhafiz/fb-page-feed)
[](https://packagist.org/packages/xmhafiz/fb-page-feed)
It is simple wrapper class written in php to fetch posts from certain Facebook page.
Currently I am using [Facebook graph API](https://developers.facebook.com/docs/graph-api) with cool [guzzle](https://github.com/guzzle/guzzle) and [dotenv](https://github.com/vlucas/phpdotenv)
Tested in PHP 7.0, 7.1, 7.4, 8.0
## Requirement
- PHP 7.0+
## Installation
#### Step 1: Getting Facebook App
- Go to [Facebook developer](https://developers.facebook.com/apps/) 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
```
{
"require": {
"xmhafiz/fb-page-feed": "^1.2"
}
}
```
## Usage
After installing, you need to require Composer's autoloader and add your code.
```php
$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)
```php
$data = fb_feed()->setAppId($fbAppId)
->setSecretKey($fbSecretKey)
->setPage($fbPageName)
->fetch();
```
#### Set Module
```php
$data = fb_feed()->setAppId($fbAppId)
->setSecretKey($fbSecretKey)
->setPage($fbPageName)
->setModule("feeds")
->fetch();
```
#### Custom Maximum Post Shown
```php
// only show 5 post maximum
$data = fb_feed()->setAppId($fbAppId)
->setSecretKey($fbSecretKey)
->setPage($fbPageName)
->feedLimit(5)
->fetch();
```
#### Filter By Keyword
```php
// only show 5 post maximum
$data = fb_feed()->setAppId($fbAppId)
->setSecretKey($fbSecretKey)
->setPage($fbPageName)
->findKeyword("#LaravelCommunity")
->fetch();
```
#### Change Request Field
```php
// 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](https://github.com/xmhafiz/fb-page-feed/pull/7).
```php
$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
```php
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
```
$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:
```json
{
"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.
## To use with **dotenv**
- Look at [example code](https://github.com/xmhafiz/fb-page-feed/blob/master/example/index.php)
- 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](http://opensource.org/licenses/MIT)