Erik
Erik

Reputation: 1

Publish to Facebook Wall with Graph & PHP

I'm having trouble using the open graph authorization to publish to the visitors facebook wall, and can't seem to figure out where I'm wrong. I'm using the PHP/SDK Library from Github. Here's my code:

<?php
require_once('facebook.php');
$facebook = new Facebook(array(
  'appId'  => 'APP_ID',
  'secret' => 'APP_SECRET',
  'cookie' => true,
));

$session = $facebook->getSession();

$me = null;
//session-based api call
if ($session) {
  try {
    $uid = $facebook->getUser();
    $me = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    error_log($e);
  }
}

//post to wall/stream function
if(isset($_POST['status'])) {
    try {
        $result = $facebook->api(
            '/me/feed',
            'post',
            array(
                'access_token' => 'ACCESS_TOKEN', 
                'message' => $_POST['status']
                )
            );
    } catch (FacebookApiException $e) {
        echo 'Error: ' . print_r($e, true);
    }
}
?>

<!-- HTML Form -->
<form name="" action="index.php" method="post">
    <input type="text" name="status" id="status" value="">
    <input type="submit" value="submit">
</form>

I don't know what's going on, I've combed through a billion tutorials, posts, exchanges, etc. and it seems like I'm doing everything right. Any help would be greatly appreciated.

Upvotes: 0

Views: 2944

Answers (2)

ifaour
ifaour

Reputation: 38115

Here you are not actually checking if the user is logged in before posting and I can't see where you are asking the user to login. Here's a better approach:

<?php
require_once('facebook.php');
$facebook = new Facebook(array(
  'appId'  => 'APP_ID',
  'secret' => 'APP_SECRET',
  'cookie' => true,
));

$session = $facebook->getSession();

if($session) {
    //post to wall/stream function
    if(isset($_POST['status'])) {
        try {
            $result = $facebook->api(
                '/me/feed',
                'post',
                array(
                    'message' => $_POST['status']
                    )
                );
        } catch (FacebookApiException $e) {
            echo 'Error: ' . print_r($e, true);
        }
    }
} else {
    $loginUrl = $facebook->getLoginUrl(array(
        'req_perms' => 'publish_stream'
    ));
    header("Location: $loginUrl");
}
?>

<!-- HTML Form -->
<form name="" action="index.php" method="post">
    <input type="text" name="status" id="status" value="">
    <input type="submit" value="submit">
</form>

Upvotes: 1

Femi
Femi

Reputation: 64700

With the code you've written, it is assumed that you are using the Javascript SDK to produce the facebook cookie, which is what the $facebook->getSession() call uses to log the user in: do you have the Javascript SDK setup correctly?

Also, you are assuming that whatever app you are using has obtained the appropriate permission to post to the user's wall. See http://developers.facebook.com/docs/authentication/permissions/ for more information about the required permissions. Have you obtained the requested permission from the user?

Upvotes: 0

Related Questions