2

Is there any good tutorial that teaches how to post on user's wall with API? This is the flow:

  1. The user will come on my website and click on post to facebook button at the end of article.

  2. He is shown sign in dialog from facebook, after sign in he will give permission to my application to post on his wall on his behalf.

  3. After authorization, his shared link will be posted on his wall.

  4. Upon future shares, he will not be asked for permissions since he has already given permission to post on his behalf. So in future when he clicks 'Post to Facebook' button under the article then that item will be posted to his wall without opening facebook login dialog.

I have searched a lot on tutorials but have not found any that meets my requirement.

I am very new to facebook API and have not worked with it before.

Any suggestions or link to tutorials? Thank You!

user3603267
  • 23
  • 1
  • 3

3 Answers3

1

I've code to help you to post status with an image to an user's timeline.

After user has given permission to your app , you might have received a query string parameter called 'code' , using this code , we can post to user's timeline.

$code = @$_REQUEST["code"];

      if(!empty($code))
      {
          // Start : Get live user access token ------------------------------------------ //
          $token_url= "https://graph.facebook.com/oauth/"
          . "access_token?"
          . "client_id=" .  FACEBOOK_APP_ID
          . "&redirect_uri=" . urlencode( FACEBOOK_POST_LOGIN_URL)
          . "&client_secret=" . FACEBOOK_APP_SECRECT
          . "&code=" . $code;

          $token = $this->get_app_access_token(FACEBOOK_APP_ID,FACEBOOK_APP_SECRECT,$token_url);
          $access_token = $token;

          // End : Get live user access token ------------------------------------------- //

          // Start : Create album ------------------------------------------------------ //

          $graph_url = "https://graph.facebook.com/me/albums?"
          . "access_token=". $access_token;

          $uri = 'https://graph.facebook.com/albums?access_token='.$access_token;

          $post_fields = array('name' => trim( FACEBOOK_ALBUM_NAME ));

          $curl = curl_init( $uri );
          curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1 );
          curl_setopt( $curl, CURLOPT_POST, TRUE );
          curl_setopt( $curl, CURLOPT_POSTFIELDS, $post_fields );

          $raw_data = curl_exec($curl);
          curl_close( $curl );

          $created_album_id = json_decode( $raw_data, $assoc = TRUE );

          // End : Create album ---------------------------------------------------------- //   

          $facebook_share_image_url = FACEBOOK_SHARE_IMAGE_PATH;

          $facebook_status_text = 'The status text';

          $graph_url= "https://graph.facebook.com/me/photos";

          $postData = "url=" . urlencode($facebook_share_image_url)
          . "&message=" . urlencode($facebook_status_text)
          . "&access_token=" .$access_token;

          $ch = curl_init();
          curl_setopt($ch, CURLOPT_URL, $graph_url);
          curl_setopt($ch, CURLOPT_HEADER, 0);
          curl_setopt($ch, CURLOPT_POST, 1);
          curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
          curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
          $output = curl_exec($ch);
          curl_close($ch);

          // End : Add photos ------------------------------------------------------------- //
    }

and the function to get app access token

function get_app_access_token($app_id, $secret,$token_url)
{
    $url = 'https://graph.facebook.com/oauth/access_token';
    $token_params = array(
            "type" => "client_cred",
            "client_id" => FACEBOOK_APP_ID,
            "redirect_uri" => urlencode(FACEBOOK_POST_LOGIN_URL),
            "client_secret" => FACEBOOK_APP_SECRECT
    );

    $a1 = $this->file_get_contents_curl($token_params,$token_url);
    $a2 =  str_replace("access_token=","",$a1);
    $a3 = explode("&expires",$a2);

    return $a3[0];
}

The other function access graph url

function file_get_contents_curl($params,$url)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
    curl_setopt($ch, CURLOPT_FRESH_CONNECT, TRUE);
    $headers = array(
            "Cache-Control: no-cache",
    );
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    $ret = curl_exec($ch);
    curl_close($ch);
    return $ret;
}

Hope it helps..!!

0

You can use simple facebook sharer URL:

https://www.facebook.com/sharer/sharer.php?u=YOURPAGEURL

Where YOURPAGEURL is the page URL which you want to share.. Hope this helps.

Rahul Kaushik
  • 1,454
  • 8
  • 16
  • 1
    This is not what I want. I want when user clicks on "Post to facebook" button then that link will be posted on user's wall without opening any dialog box from facebook. Obviously the dialog will open when the user has not given the permission, but once user has given the permission I want to save his tokens in db and next time check those tokens against user id and post to his profile without opening facebook dialog. – user3603267 May 05 '14 at 07:11
0

you have to permission pubish_stream on your app and then try this using curl:- Also you need a user access token then send access token with curl call

$attachment =  array(
        'access_token' => $access_token,
        'message' => 'i m success to using graph api for post wall',
        'name' => 'Wall Post using graph api',
        'link' => 'www.mysite.com',
        'description' => 'Using the Graph API, any Facebook users Wall feed may be accessed by using this URL:',
        'picture'=>'http://example.com/images/noimage.png'
 );
$url = "https://graph.facebook.com/$facebook_id/feed";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  //to suppress the curl output
$result = curl_exec($ch);
curl_close ($ch);
print_r($result)

For more :- Graph API new feed post object-Attachment not showing

Post to a Facebook user's wall with cURL PHP

Community
  • 1
  • 1
Rakesh Sharma
  • 13,680
  • 5
  • 37
  • 44