0

OK so the cod below basically works fine, WHEN this line is like this:

$post_url = '/'.$userPageId.'/feed';

This successfully posts to the Facebook Page Timeline. But I want to upload a photo, and when I change this line to

$post_url = '/'.$userPageId.'/photos';

It posts the photo... but it posts to the User Feed, instead of AS the Facebook Page.

I want this to post the photo to the Facebook Page Timeline

[config.php]

<?php
include_once("inc/facebook.php");

##################################
//Call Facebook API

// Required facebook permissions
$fbPermissions = 'publish_stream,manage_pages,photo_upload';

$facebook = new Facebook(array(
  'appId'  => $appId,
  'secret' => $appSecret,
'fileUpload' => true,
'cookie' => true  
));

$fbuser = $facebook->getUser();
?>

[process.php]

<?php
include_once("config.php");

if($_POST)
{
    //Post variables we received from user
    $userPageId         = $_POST["userpages"];
    $userMessage         = $_POST["message"];

    if(strlen($userMessage)<1) 
    {
            //message is empty
            $userMessage = 'No message was entered!';
    }

    //HTTP POST request to PAGE_ID/feed with the publish_stream
    $post_url = '/'.$userPageId.'/photos';

    //posts message on page statues 
    $msg_body = array(
        'source' => '@' . 'test.jpg',                   
        'message' => "yo yo yo",
    );

    if ($fbuser) {
        try {
            $postResult = $facebook->api($post_url, 'post', $msg_body );
        } catch (FacebookApiException $e) {
            echo $e->getMessage();
        }
    } else {
        $loginUrl = $facebook->getLoginUrl(
            array('redirect_uri'=>$homeurl,'scope'=>$fbPermissions)
        );
        header('Location: ' . $loginUrl);
    }

    //Show sucess message
    if($postResult)
    {

    }
}
?>

[index.php] (for logging in)

<?php
include_once("config.php");
if ($fbuser)
{
    try {
        $user_profile = $facebook->api('/me');
        //Get user pages details using Facebook Query Language (FQL)
        $fql_query = 'SELECT page_id, name, page_url FROM page '
            .'WHERE page_id IN (SELECT page_id FROM page_admin WHERE uid='
            .$fbuser.')';
        $postResults = $facebook->api(
            array( 'method' => 'fql.query', 'query' => $fql_query )
        );
    } catch (FacebookApiException $e) {
        echo $e->getMessage();
        $fbuser = null;
    }
} else {
    //Show login button for guest users
    $loginUrl = $facebook->getLoginUrl(
        array('redirect_uri'=>$homeurl,'scope'=>$fbPermissions)
    );
    echo '<a href="'.$loginUrl.
        '"><img src="images/facebook-login.png" border="0"></a>';
    $fbuser = null;
}

if($fbuser && empty($postResults))
{
    /*
    if user is logged in but FQL is not returning any pages, we need to make
    sure user does have a page OR "manage_pages" permissions isn't granted yet
    by the user. Let's give user an option to grant permission again.
    */
    $loginUrl = $facebook->getLoginUrl(
        array('redirect_uri'=>$homeurl,'scope'=>$fbPermissions)
    );
    echo '<br />Could not get your page details!';
    echo '<br /><a href="'.$loginUrl.'">Click here to try again!</a>'; 
}
elseif ($fbuser && !empty($postResults))
{
    //Everything looks good, show message form.
}
?>
Owen Blacker
  • 4,117
  • 2
  • 33
  • 70

1 Answers1

4

If you want to act as the Facebook Page and upload a photo as such, you'll need:

  1. To be an Admin on the Facebook Page
  2. At least the following scopes when getting authorizations: 'manage_pages', 'photo_upload'
  3. The access token as the Facebook Page, NOT as the User who is the Admin

You can fetch the access tokens of the Facebook Pages you are Admin of by calling the Open Graph API:

$accounts = $facebook->api('/me/accounts','GET');

In order to get the access token and any more information, you need the 'manage_pages' permission.

Once you have the access token, you'll need to use it and re-initialize a client with it. Then you can essentially act as it, and post a photo as it. Hopefully this will be enough to get you started. Your code looks set up to be able to upload the photo to where you want it to be. It's all a matter of the access token.

Edit:

You can set the access token with the PHP SDK with setAccessToken:

// Set a new access token, by first getting it via means other than the SDK
$facebook->setAccessToken($new_access_token);
JayNCoke
  • 1,091
  • 2
  • 11
  • 17
  • Thank you very much for the first ray of light into this issue for me :) What you're saying makes sense... however... I am wondering about the access tokens... Where do I set an access token? – Arron Rasmussen May 15 '13 at 23:08
  • Ok so I used, $access_token = $facebook->getAccessToken(); and that is generating a token... however, I don't know which of my Facebook Pages it is generating for (or if that matters) I tried adding: $new_access_token = "mytokenhere"; $facebook->setAccessToken($new_access_token); to the process.php code, but there was no change... so far... guessing i may need to fetch a page specific token code? but not sure... thank you! – Arron Rasmussen May 15 '13 at 23:55
  • Calling $facebook->getAccessToken() gets the token for your user. You need to make an Open Graph API call that I mentioned and fetch the pages that your user is admin for. Make sure you have authorized your app with the appropriate scopes so you can get the data. Once you get the data, you can pretty much pluck the access token from there. – JayNCoke May 16 '13 at 00:07
  • Edited my answer for you to get the Facebook Page information which will have the access token you need. – JayNCoke May 16 '13 at 00:13
  • Hallelujah! You're my savior man. That worked like a peach. Thank you brother, really, thank you... that was 10 hours and counting of scouring the web for an answer... thank you!!! – Arron Rasmussen May 16 '13 at 00:32
  • Thank you, you are my savior! – Skyzer Jun 20 '13 at 00:59