PUG
PUG

Reputation: 4472

Posting on user's facebook wall using php 2011

Nothing gets posted to the wall, execution gets out of try after $result = $facebook->api('/me/feed/','post',$attachment); statement, any idea whats broken.

$facebook = new Facebook(array(
  'appId'  => 'xxxxxxxxxxxx',
  'secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
));

// Get User ID


$user = $facebook->getUser();
if ($user) {
  try {
    // Get the user profile data you have permission to view
    $user_profile = $facebook->api('/me');
    $uid = $facebook->getUser();


      $url = $facebook->getLoginUrl(array(
    'canvas' => 1,
    'fbconnect' => 0,
    'req_perms' => 'email,publish_stream,status_update,user_birthday,user_location,user_work_history'));


    $attachment = array
 (
 'access_token'=>$facebook->getAccessToken(),
 'message' => 'I had a question: should I write a PHP facebook app that actually worked?',
 'name' => 'I Asked Bert',
 'caption' => 'Bert replied:',
 'link' => 'http://apps.facebook.com/askbert/',
 'description' => 'NO',
 'picture' => 'http://www.facebookanswers.co.uk/img/misc/question.jpg'
 );
 echo "Test 1"; 

$result = $facebook->api('/me/feed/','post',$attachment);

    echo "Test 2"; 
    $_SESSION['userID'] = $uid;


  } catch (FacebookApiException $e) {
    $user = null;
  }
} else {
  die('Somethign Strange just happened <script>top.location.href="'.$facebook->getLoginUrl().'";</script>');
}

Test 1 is printed but not Test 2.

Upvotes: 0

Views: 10198

Answers (3)

PUG
PUG

Reputation: 4472

below code surely works: even if you dont have permission it will try to get them

$facebook = new Facebook(array(
  'appId'  => 'xxxxxxxxxxxxxxxxxx',
  'secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
));
$user = $facebook->getUser();
$user_profile = $facebook->api('/me');


if( array_key_exists('publish_stream', $permissions['data'][0]) ) {
                // Permission is granted!
                // Do the related task
                //$post_id = $facebook->api('/me/feed', 'post', array('message'=>'Hello World!'));
                  $post_id = $facebook->api('/me/feed', 'post', $attachment);

            } else {
                // We don't have the permission
                // Alert the user or ask for the permission!
                echo "Click Below to Enter!";
                header( "Location: " . $facebook->getLoginUrl(array("scope" => "publish_stream")) );
            }

*Warning

as of sept 5 2011 this is working, but i saw on fb documentation they are changing method to poste on users wall and are discouraging use of publish stream. but its working for now

Upvotes: 0

phwd
phwd

Reputation: 19995

You said you were looking for updated documentation, did you check Facebook PHP-SDK FAQ?

Specifically,

  • How to authorize and have any of the following permissions?
  • How to post on a wall?

After you create an Application instance get your $user first

$user = $facebook->getUser();

From here, following the instructions from "How to authorize and have any of the following permissions?" using the scope

$par = array();
$par['scope'] = "publish_stream";

Check the user state to see which login/logout method is required passing the publish_stream permission

if ($user) {
        $logoutUrl = $facebook->getLogoutUrl();
} else {
        $loginUrl = $facebook->getLoginUrl($par);
}

Then place the attachment as explained in "How to post on a wall?"

if ($user) {
$attachment = array('message' => 'this is my message',
    'name' => 'This is my demo Facebook application!',
    'caption' => "Caption of the Post",
    'link' => 'http://mylink.com/ ',
    'description' => 'this is a description',
    'picture' => 'http://mysite.com/pic.gif ',
    'actions' => array(array('name' => 'Get Search',
    'link' => 'http://www.google.com/ '))
    );

    try {
    // Proceed knowing you have a user who is logged in and authenticated
    $result = $facebook->api('/me/feed/','post',$attachment);
    } catch (FacebookApiException $e) {
    error_log($e);
    $user = null;
    }

}

As explained in the example app, put in a try/catch block to see what data is available depending on whether the user is logged in or not when making API calls.

A call such as

$cocacola = $facebook->api('/cocacola');

Will always work since it is publicly available.

Upvotes: 5

ifaour
ifaour

Reputation: 38115

Here are a couple of notes:

  1. You are using the new PHP-SDK so don't use req_perms use scope instead
  2. Put your /me/feed post call inside the try
  3. You don't need to call getUser() twice, the user id is already in the $user
  4. The user id will be already in the session, with key that looks like: fb_XXXXXXX_user_id where XXXXXXX is your app id
  5. session already started..

Upvotes: 1

Related Questions