Facebook Graph API PHP SDK v4 - Post on Page

user3614800

I wanna give all my website authors a possiblity to post their articles on our facebook page without having go give them admin access to it.

So i created a simple form, where the author types in: URL, URL to image, message

On submit, this form will send a ajax request to facebook.php where the magic "should" happen.

The first problem occurs at "require_once". It's not possible to require all 4 files without having an error. If i get rid of the facebook exception, then everything works except the request itself. There seems to be an PHP Error, because i get no ajax response at all.

session_start();

require_once($_SERVER['DOCUMENT_ROOT'].'/sys/facebook/FacebookSession.php');
require_once($_SERVER['DOCUMENT_ROOT'].'/sys/facebook/FacebookRequest.php');
require_once($_SERVER['DOCUMENT_ROOT'].'/sys/facebook/GraphObject.php');
require_once($_SERVER['DOCUMENT_ROOT'].'/sys/facebook/FacebookRequestException.php');

use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\GraphObject;
use Facebook\FacebookRequestException;

$message = safe($_POST["message"]);
$url = safe($_POST["url"]);
$image = safe($_POST["image"]);

if($message == "" OR $url == "" OR $image == ""){
    echo "incomplete";
    return;
}

FacebookSession::setDefaultApplication('{APP ID}','{APP SECRET}');
$session = new FacebookSession('{Page Access Token}');

if($session) {
    try {
        $response = (new FacebookRequest(
            $session, 'POST', '/{Page ID}/feed', array(
                'message'       => $message,
                'link'          => $url,
                'picture'       => $image
            )
        ))->execute()->getGraphObject();
        echo "Posted with id: " . $response->getProperty('id');
    } catch(FacebookRequestException $e) {
        echo "Exception occured, code: " . $e->getCode();
        echo " with message: " . $e->getMessage();
    }
} else {
    echo "No Session available!";
}
SammyK

Update: June, 27 2014, The SDK now comes with a built-in autoloader for those who can't use composer.

require __DIR__ . '/path/to/facebook-php-sdk-v4/autoload.php';

If that doesn't automatically find the path for you, you can define it with FACEBOOK_SDK_V4_SRC_DIR.

define('FACEBOOK_SDK_V4_SRC_DIR', '/path/to/facebook-php-sdk-v4/src/Facebook/');
require __DIR__ . '/path/to/facebook-php-sdk-v4/autoload.php';

The internals of the SDK rely on several other classes that you're not including. That's why autoloading is really important here.

Autoloading With Composer

The best way to do this is to install composer. And add the SDK in a composer.json file to the root of your project.

{
    "require" : {
        "facebook/php-sdk-v4" : "4.0.*"
    }
}

Then run composer install from the command line where the composer.json file is. Then include the autoloader at the top of your script.

require_once __DIR__ . '/vendor/autoload.php';

Manual Autoloading

An alternative way to autoload these files is to replace your require_once's at the top with this solution from rm-vanda:

function facebookLoader($class) {
    require "/path/to/facebook-php-sdk-v4-master/src/" . str_replace("\\", "/", $class) . ".php";
}

spl_autoload_register("facebookLoader");

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Facebook SDK v4 and Graph Api 2.x auto post in Fan Page Wall

From Dev

Facebook SDK v4 and Graph Api 2.x auto post in Fan Page Wall

From Dev

Automatically post on wall - Facebook Graph API PHP SDK v4

From Dev

Graph API v1.0 from Facebook-PHP-SDK v4

From Dev

Migrating to Facebook PHP SDK v4, Open Graph

From Dev

Migrating to Facebook PHP SDK v4, Open Graph

From Dev

Facebook post to fan page using php sdk5 graph api 2.4

From Dev

Facebook post to fan page using php sdk5 graph api 2.4

From Dev

facebook post on page wall - PHP SDK

From Dev

post from php to my fan page facebook with facebook sdk 5 api 2.4

From Dev

post from php to my fan page facebook with facebook sdk 5 api 2.4

From Dev

Post Picture Facebook - Graph API - PHP

From Dev

Facebook Getting Event from PAGE! not user PHP sdk v4.0.x graph 2.1

From Dev

Facebook PHP SDK: How can I post on page on behalf of page

From Dev

Facebook API, get page post link (PHP)

From Dev

Facebook PHP SDK v4 Redirect when already logged

From Dev

Facebook Graph API: Making public post on user's page

From Dev

Facebook Graph API post message with link to page's wall

From Dev

Post message with picture to facebook page/feed using graph api

From Dev

Facebook Graph API: Making public post on user's page

From Dev

Facebook Graph API post message with link to page's wall

From Dev

Facebook Graph API - (#200) Permissions error on Page Post

From Dev

Facebook SDK 4.x Android Post as Facebook Page

From Dev

Facebook SDK 4.x Android Post as Facebook Page

From Dev

Facebook Graph API: issue with appsecret_proof and PHP SDK v.3.2.3

From Dev

PHP Facebook SDK how to GET graph API without token

From Dev

Get Facebook User Email Using Graph API PHP SDK

From Dev

how to use graph api access token in php for facebook sdk

From Dev

Correct Facebook Graph Api Video Insitghts Request PHP SDK

Related Related

  1. 1

    Facebook SDK v4 and Graph Api 2.x auto post in Fan Page Wall

  2. 2

    Facebook SDK v4 and Graph Api 2.x auto post in Fan Page Wall

  3. 3

    Automatically post on wall - Facebook Graph API PHP SDK v4

  4. 4

    Graph API v1.0 from Facebook-PHP-SDK v4

  5. 5

    Migrating to Facebook PHP SDK v4, Open Graph

  6. 6

    Migrating to Facebook PHP SDK v4, Open Graph

  7. 7

    Facebook post to fan page using php sdk5 graph api 2.4

  8. 8

    Facebook post to fan page using php sdk5 graph api 2.4

  9. 9

    facebook post on page wall - PHP SDK

  10. 10

    post from php to my fan page facebook with facebook sdk 5 api 2.4

  11. 11

    post from php to my fan page facebook with facebook sdk 5 api 2.4

  12. 12

    Post Picture Facebook - Graph API - PHP

  13. 13

    Facebook Getting Event from PAGE! not user PHP sdk v4.0.x graph 2.1

  14. 14

    Facebook PHP SDK: How can I post on page on behalf of page

  15. 15

    Facebook API, get page post link (PHP)

  16. 16

    Facebook PHP SDK v4 Redirect when already logged

  17. 17

    Facebook Graph API: Making public post on user's page

  18. 18

    Facebook Graph API post message with link to page's wall

  19. 19

    Post message with picture to facebook page/feed using graph api

  20. 20

    Facebook Graph API: Making public post on user's page

  21. 21

    Facebook Graph API post message with link to page's wall

  22. 22

    Facebook Graph API - (#200) Permissions error on Page Post

  23. 23

    Facebook SDK 4.x Android Post as Facebook Page

  24. 24

    Facebook SDK 4.x Android Post as Facebook Page

  25. 25

    Facebook Graph API: issue with appsecret_proof and PHP SDK v.3.2.3

  26. 26

    PHP Facebook SDK how to GET graph API without token

  27. 27

    Get Facebook User Email Using Graph API PHP SDK

  28. 28

    how to use graph api access token in php for facebook sdk

  29. 29

    Correct Facebook Graph Api Video Insitghts Request PHP SDK

HotTag

Archive