简体   繁体   中英

post as page on facebook page wall

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

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'));
            }
            ?>

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.

This probably won't be it but, ensure you are not using picture URLs that resolve to an internal domain name (ie 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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM