xmhafiz/fb-page-feed

Simple wrapper class to get any Facebook Page posts

Installs: 43 663

Dependents: 0

Suggesters: 0

Security: 0

Stars: 43

Watchers: 3

Forks: 9

Open Issues: 0

Type:project

1.1.5 2020-11-09 16:17 UTC

This package is auto-updated.

Last update: 2021-04-09 17:16:37 UTC


README

Build Status Coverage Packagist

It is simple wrapper class written in php to fetch posts from certain Facebook page.

Currently I am using Facebook graph API with cool guzzle and dotenv

Tested in PHP 7.0 and 7.1

Requirement

  • PHP 7.0+

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

{
   "require": {
      "xmhafiz/fb-page-feed": "^1.1"
   }
}

Usage

After installing, you need to require Composer's autoloader and add your code.

$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.

$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

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

$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:

{
    "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
  • 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