1

I've read many other topics about this subject but I didn't find something helpful.

What I want to do

When I add an article on my website, I'd like to post an update on Twitter and Facebook. It's working for Twitter, but I have an issue with Facebook.

I downloaded the facebook.php which uses OAuth.

My issue

When I post a simple text, it works fine, it's displayed as posted by the page as wanted. But when I want to post a text with a thumbnail, a link, a caption and a description, it's posted as if my personal account was posting this update onto my page's wall.

Here is my code for the simple text (I requested the acces_token above):

$post = array('access_token' => $token, 'message' => 'My message');
try{  
$res = $facebook->api('/mypage/feed','POST',$post);  
print_r($res);  

} catch (Exception $e){  

    echo $e->getMessage();  
}

Here is the wrong code:

$post = array('access_token' => $token,
                'message' => 'My message',
                'picture' => 'http://www.website.com/picture.jpg',
                'link' => 'http://www.website.com',
                'caption' => 'test caption',
                'description' => 'test description', 
                'from' => array('name' =>'Page name', 'id' => 'page id'), 
                );  


try{  
$res = $facebook->api('/mypage/feed','POST',$post);  
print_r($res);  

} catch (Exception $e){  

    echo $e->getMessage();  
}

The Facebook API is not well documented but I've searched everywhere not to ask you this question .. But I don't find any solution.

Thanks a lot for helping me.

Benjamin

Benjamin
  • 13
  • 1
  • 3

3 Answers3

1

Hoping you have the following permissions (publis_stream,manage_pages, offline_access) and access_token, try the following code

                <?php
            /**
             * Edit the Page ID you are targeting
             * And the message for your fans!
             */
            $page_id = 'PAGE_ID';
            $message = "I'm a Page!";


            /**
             * This code is just a snippet of the example.php script
             * from the PHP-SDK <http://github.com/facebook/php-sdk/blob/master/examples/example.php>
             */
            require '../src/facebook.php';

            // Create our Application instance (replace this with your appId and secret).
            $facebook = new Facebook(array(
              'appId'  => 'app_id',
              'secret' => 'app_secret',
            ));

            // Get User ID
            // $user supposed to be page admin
            $user = $facebook->getUser();

            if ($user) {
              try {
                $page_info = $facebook->api("/$page_id?fields=access_token");
                if( !empty($page_info['access_token']) ) {
                    $args = array(
                        'access_token'  => $page_info['access_token'],
                        'message'       => $message ,
                        'name'         => 'My Wall Post Header/Title Here',
                        'caption'      => 'Small caption here',
                        'link'         => 'http://www.mywebsite.org',
                        'description'  => 'Wall Post Details Here',
                        'picture'      => "http://www.mywebsite.org/images/logo.gif",           
                    );
                    $post_id = $facebook->api("/$page_id/feed","post",$args);
                }
              } catch (FacebookApiException $e) {
                error_log($e);
                $user = null;
              }
            }

            // Login or logout url will be needed depending on current user state.
            if ($user) {
              $logoutUrl = $facebook->getLogoutUrl();
            } else {
              $loginUrl = $facebook->getLoginUrl(array('scope'=>'manage_pages,publish_stream'));
            }
            ?>
Team Webgalli
  • 730
  • 4
  • 13
  • It's working, thanks a lot, I'll analyze the differences between our codes to figure out where I'm wrong ! Thanks again Edit : it's working when my code is alone in a page but when I put all the code in a function and call it, it's not working anymore. Anyway, I'm sure I'll figure this out ! – Benjamin Jun 11 '12 at 12:51
  • Edit : it worked 1 time only but doesn't do anything since without changing the code hmhm .. – Benjamin Jun 11 '12 at 14:19
0

You need to implement some Open Graph og:tags on your page so that Facebook knows what images, descriptions, titles to take.

https://developers.facebook.com/docs/opengraphprotocol/

They look something like this -

<meta property="og:title" content="The Rock"/>
<meta property="og:type" content="movie"/>
<meta property="og:url" content="http://www.imdb.com/title/tt0117500/"/>
<meta property="og:image" content="http://ia.media-imdb.com/rock.jpg"/>
<meta property="og:site_name" content="IMDb"/>
<meta property="fb:admins" content="USER_ID"/>
<meta property="og:description"
      content="A group of U.S. Marines, under command of
               a renegade general, take over Alcatraz and
               threaten San Francisco Bay with biological
               weapons."/>

Once you have implemented the required meta tags, you can test your work using the handy-dandy Facebook URL Debugger. It will tell you if there are problems, exactly what they are, and how to fix them.

Lix
  • 47,311
  • 12
  • 103
  • 131
  • Hi, thanks for your answer. Yes there is open graph on my pages, but when the message is only the url, it didn't parse the page to get the og markups. When it posts as me on my page's wall with the options I gave, everything is ok. – Benjamin Jun 11 '12 at 11:36
  • Yeah, it's not a data issue, but a php sdk issue ! – Benjamin Jun 11 '12 at 13:04
  • This issue is not with the SDK but how you implement it :) – Lix Jun 11 '12 at 13:11
  • Ok so I tested the pages I want to auto post on Facebook on the Facebook debugger and .. there are weird errors. It tells me that my og tags are in the body but in my code, and when I look at the page source, it's in the head. But in the Facebook scraped page, it's in the body within

    . How can this interfer with the FB SDK ?
    – Benjamin Jun 11 '12 at 18:50
0

This probably won't be it but, ensure you are not using picture URLs that resolve to an internal domain name (i.e. through updating of your local hosts file). I found after a lot of testing (albeit it using the FB.UI call in JS) that if the domain name is pointing internally, even if it is also available externally (this is the bit that threw me), you might get errors. This only seemed to matter for the Picture argument.

Good luck!

Snouto
  • 1,039
  • 1
  • 7
  • 21