Skip to content
Advertisement

Facebook Graph API PHP SDK v4 – Post on Page

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 FacebookFacebookSession;
use FacebookFacebookRequest;
use FacebookGraphObject;
use FacebookFacebookRequestException;

$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!";
}

Advertisement

Answer

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");
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement