86

I have a blog site written in php and it posts new blog posts to twitter and a blog ping automatically under the hood using simple http post requests passed using php curl.

I have a facebook page for the blog site and want the updates to be posted to the wall on the page, is there a simple way to do this?

What I really want is a url and set of params to parcel up as an http post request.

Note that this is to post to the wall on a new style page not a profile.

starball
  • 20,030
  • 7
  • 43
  • 238
  • Have you tried the Facebook Developer Wiki at http://wiki.developers.facebook.com/index.php/Main_Page or elsewhere in their developer site http://developers.facebook.com/?ref=pf? – DOK Mar 27 '09 at 21:08
  • I'm not into facebook dev but I think you have to write a fb app first, to be authorized to do that. – sepehr Mar 28 '09 at 08:24

6 Answers6

65

Get PHP SDK from github and run the following code:

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

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

the above code will Post the message on to your wall... and if you want to post onto your friends or others wall then replace me with the Facebook User Id of that user..for further information look out the API Documentation.

Dour High Arch
  • 21,513
  • 29
  • 75
  • 90
Harish Kurup
  • 7,257
  • 19
  • 65
  • 94
  • that code works great!, but in the post there is no picture.. What am I missing here? THanks! – Diego Jun 20 '11 at 20:59
  • @Diego check the pics URL. in the above example its mysite.com/pic.gif. so it will fetch for the pic in the given url. so try setting it. please do check the fb documentation for any change in parameter's, cos the last time i worked in FB front was aug 2010.. – Harish Kurup Jun 21 '11 at 06:50
  • is $facebook a variable built into the php install? if not, how is the $facebook instance created? – sadmicrowave Sep 02 '11 at 19:14
  • @sadmicrowave the variable $facebook is the object of the class Facebook(might have changed the class name so please read the docs after getting the idea). You need to instantiate the object by the following code "$facebook = new Facebook(array( 'appId' => APPID, 'secret' => SECRETKEY, 'cookie' => true ));" – Harish Kurup Sep 03 '11 at 11:14
  • You also need to have the PHP SDK installed in your app to have access to the Facebook class – mitchellhislop Oct 11 '11 at 16:07
  • @JaySmoke sorry, you need to check the API Docs as i have not been working on this front for a long time. As you can see the 'picture' index in $attachment array, some key will be there to post the Video or audio file. I am not sure with this, pls dont consider this as a solution. do check the API Docs and also post the solution for that. Thank you. – Harish Kurup Dec 23 '11 at 06:59
  • where can I find that???? Cause there's nothing here http://developers.facebook.com/docs/reference/api/ – Jay Smoke Dec 23 '11 at 12:46
  • The original question is about pages, not a user's wall. Why I think this is important is because of a very small but essential point: you need to use $facebook->setAccessToken() before calling the API function. – Parham Doustdar Aug 04 '13 at 04:57
9

This works for me:

try {
       $statusUpdate = $facebook->api('/me/feed', 'post',
                 array('name'=>'My APP on Facebook','message'=> 'I am here working',
                 'privacy'=> array('value'=>'CUSTOM','friends'=>'SELF'),
                 'description'=>'testing my description',
                 'picture'=>'https://fbcdn-photos-a.akamaihd.net/mypicture.gif',
                 'caption'=>'apps.facebook.com/myapp','link'=>'http://apps.facebook.com/myapp'));
 } catch (FacebookApiException $e) {
      d($e);
}
Nick Cox
  • 35,529
  • 6
  • 31
  • 47
Helton Uchoa
  • 91
  • 1
  • 1
8

Harish has the answer here - except you need to request manage_pages permission when authenticating and then using the page-id instead of me when posting....

$result = $facebook->api('page-id/feed/','post',$attachment);
Phill Price
  • 191
  • 1
  • 2
  • 12
5

You can not post to Facebook walls automatically without creating an application and using the templated feed publisher as Frank pointed out.

The only thing you can do is use the 'share' widgets that they provide, which require user interaction.

Darryl E. Clarke
  • 7,537
  • 3
  • 26
  • 34
3

If your blog outputs an RSS feed you can use Facebook's "RSS Graffiti" application to post that feed to your wall in Facebook. There are other RSS Facebook apps as well; just search "Facebook for RSS apps"...

alexg
  • 3,015
  • 3
  • 23
  • 36
Ricky
  • 31
  • 1
0

You can make api calls by choosing the HTTP method and setting optional parameters:

$facebook->api('/me/feed/', 'post', array(
    'message' => 'I want to display this message on my wall'
));

Submit Post to Facebook Wall :

Include the fbConfig.php file to connect Facebook API and get the access token.

Post message, name, link, description, and the picture will be submitted to Facebook wall. Post submission status will be shown.

If FB access token ($accessToken) is not available, the Facebook Login URL will be generated and the user would be redirected to the FB login page.

Post to facebook wall php sdk

<?php
//Include FB config file
require_once 'fbConfig.php';

if(isset($accessToken)){
    if(isset($_SESSION['facebook_access_token'])){
        $fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
    }else{
        // Put short-lived access token in session
        $_SESSION['facebook_access_token'] = (string) $accessToken;

        // OAuth 2.0 client handler helps to manage access tokens
        $oAuth2Client = $fb->getOAuth2Client();

        // Exchanges a short-lived access token for a long-lived one
        $longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($_SESSION['facebook_access_token']);
        $_SESSION['facebook_access_token'] = (string) $longLivedAccessToken;

        // Set default access token to be used in script
        $fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
    }

    //FB post content
    $message = 'Test message from CodexWorld.com website';
    $title = 'Post From Website';
    $link = 'http://www.codexworld.com/';
    $description = 'CodexWorld is a programming blog.';
    $picture = 'http://www.codexworld.com/wp-content/uploads/2015/12/www-codexworld-com-programming-blog.png';

    $attachment = array(
        'message' => $message,
        'name' => $title,
        'link' => $link,
        'description' => $description,
        'picture'=>$picture,
    );

    try{
        //Post to Facebook
        $fb->post('/me/feed', $attachment, $accessToken);

        //Display post submission status
        echo 'The post was submitted successfully to Facebook timeline.';
    }catch(FacebookResponseException $e){
        echo 'Graph returned an error: ' . $e->getMessage();
        exit;
    }catch(FacebookSDKException $e){
        echo 'Facebook SDK returned an error: ' . $e->getMessage();
        exit;
    }
}else{
    //Get FB login URL
    $fbLoginURL = $helper->getLoginUrl($redirectURL, $fbPermissions);

    //Redirect to FB login
    header("Location:".$fbLoginURL);
}

Refrences:

https://github.com/facebookarchive/facebook-php-sdk

https://developers.facebook.com/docs/pages/publishing/

https://developers.facebook.com/docs/php/gettingstarted

http://www.pontikis.net/blog/auto_post_on_facebook_with_php

https://www.codexworld.com/post-to-facebook-wall-from-website-php-sdk/

Farhad
  • 4,119
  • 8
  • 43
  • 66
  • Thanks. Have you tried the code though? ***Custom Link Preview Snippets*** are now obsolete, so that code will not post to a page ( https://developers.facebook.com/blog/post/2017/06/27/API-Change-Log-Modifying-Link-Previews/ ) – tinonetic Oct 08 '17 at 13:30