I want to create post with web site preview - facebook

I want to create a post with web site preview. It must be similar as on screenshot.
How do I create a post with web preview? I want to add it on pages.
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:#"Post with web preview", #"message", nil];
[FBRequestConnection startWithGraphPath:[NSString stringWithFormat:#"%#/feed", myself.pageID]
parameters:dict HTTPMethod:#"POST" completionHandler:nil];

More info about posting links (what you call "post with web site preview") here:
http://developers.facebook.com/docs/reference/api/link/
I asumme you have your Facebook APP with your permissions set, and that you're using PHP-sdk classes. Anyway, the proccess is the same for any language, just changing the way you write it. Also, you need the user Access Token with the proper permission set.
Using the PHP-sdk from Facebook, process would be:
// Load FB class, and init it
require_once("facebook.php");
$config = array();
$config['appId'] = 'YOUR_APP_ID';
$config['secret'] = 'YOUR_APP_SECRET';
$facebook = new Facebook($config);
// You set the User Access Token (you need to have it previously to this: Because you
// requested before, or because you obtain it from Facebook)
$facebook->setAccessToken( $user_access_token );
try {
// Set the link params:
if ( isset($config['link']) ) {
$args = array(
'link' => $link_url //Url to be linked
, 'name' => $link_titulo //Box title
, 'description' => $link_descripcion //Box Description
, 'picture' => $link_foto //Photo to be posted
, 'message' => $link_message //Message over the "link box"
);
// Post in the User/FB_Page wall
$facebook->api('/me/feed', 'post', $args);
}
} catch (FacebookApiException $e) {
$fbError = $e->getResult();
$result = array(
'tipo' => 'error'
, 'code' => $fbError['error']['code']
, 'text' => $fbError['error']['message']
);
print_r($result);
}
Update
After reading your answer to my comment, I think you're looking just for the CSS and the layout. You can find something to achive the post formatting here:
http://jsfiddle.net/5NYD5/3/

This can be done using multiple ways,
If you are using facebook API then its easy, just like on FB(image will be automatically fetched)
Dim fb As FacebookClient = New FacebookClient('access_token')
Dim args As Dictionary(Of String, Object) = New Dictionary(Of String, Object)()
args("message") = "Your Message to be posted"
args("link") = "http://www.example.com"
fb.Post("/me/feed", args)
I hope this will do,
else if you are not using FB API then its comparatively long way to grab image or create snapshot, of site manually. Just like search engine crawlers crawl after entering website. Its long way but yes possible.

Related

Post to facebook page from facebook page tab app

I want to post activities from page tab app to my own facebook page. I referred many documents to post message to app.
My php code :
$GLOBALS["facebook"] = new \Facebook_Facebook(array('appId' => $facebook_app_id, 'secret' => $facebook_secret,));
$page_info = $GLOBALS["facebook"]->api("/pageid?fields=access_token");
print_r($page_info);die;
if (!empty($page_info['access_token'])) {
$args = array(
'access_token' => $page_info['access_token'],
'message' => 'TEST'
);
$postId = $facebook->api("/pageid/feed", "post", $args);
But pageinfo variable has only id. I'm not getting access_token. Any idea what else need to be done?
We need manage_pages permission which we can get it using https://www.facebook.com/dialog/oauth?client_id=client_app_id&client_secret=9ea3e2eff7c65b1fcf1a633da&redirect_uri=YOUR_REDIRECT_URL&scope=read_stream,publish_stream,offline_access,manage_pages.
After that using above code we will get access token which can be used to publish to our page

posted images in wall disappear

i'm posting some images using the following command:
$url_facebook = "https://api.facebook.com/method/stream.publish?message=".$messaggio."&attachment={\"name\":\"".$title."\",\"href\":\"".$action_link."\",\"caption\":\"".$caption."\",\"description\":\"".$description."\",\"media\":[{\"type\":\"image\",\"src\":\"".$image."\",\"href\":\"".$action_link."\"}]}&action_links=[{\"text\":\"".$link_name."\",\"href\":\"".$action_link."\"}]&target_id=".$target_uid."&uid=".$source_uid."&".$site_token."";
simplexml_load_file("".$url_facebook."");
the image i post in the wall (a group) is generated on the fly from PHP.
this is what happens:
i generate an image and save it to a folder
i post it with the code upon
i exit from the script
the next time a run the script (each hour) i delete all the old
created images , than repeat wath sayid upon
i noticed that the images just when published, are present and existing on the FB wall
BUT
the next time i launch the script (and so the images are deleted from my site) they disappear also from facebook
i understood that the images where uploaded to facebook, so they were not needed anymore on my space....
isn't so ?
i've this doubt also because, if i see on the red x on facebook (in the place of the images)
and i right click "proprerties", i can read :
"https://s-platform.ak.fbcdn.net/www/app_full_proxy.php?app=164286063695472&v=1&size=z&cksum=17f253305254403324260843c7a6989a&src=http%3A%2F%2Fwww.graficisismici.it%2FV6_00%2Ffacebook%2Ffacebook_link_285790.jpg%3F285790"
so it's seem that it's really a link to the original image
can you solve my doubt ?
meanwhile I modified the script to use the new Graph Api
<?php
$user = null;
$facebook = new Facebook(array(
'appId' => FACEBOOK_APP_ID,
'secret' => FACEBOOK_SECRET,
'cookie' => true
));
$user = $facebook->getUser(); // Get the UID of the connected user, or 0 if the Facebook user is not connected.
if($user == 0) {
/**
* Get a Login URL for use with redirects. By default, full page redirect is
* assumed. If you are using the generated URL with a window.open() call in
* JavaScript, you can pass in display=popup as part of the $params.
*
* The parameters:
* - redirect_uri: the url to go to after a successful login
* - scope: comma separated list of requested extended perms
*/
$login_url = $facebook->getLoginUrl($params = array('scope' => "publish_stream"));
echo ("<script> top.location.href='".$login_url."'</script>");
} else {
$user_message = "Message";
$titolo = "Titolo";
$caption = "Caption";
$link_name = "Link Name";
$action_link = "Action Link";
$testo = "Text";
$image = "Image Link";
$target_uid = "XXXXXXXX"; // ID where to publish
//HTTP POST request to PAGE_ID/feed with the publish_stream
$post_url = '/'.$target_uid.'/feed';
try {
$params = array(
'message' => $user_message,
'name' => $titolo,
'caption' => $caption,
'link' => $action_link,
'description' => $link_name,
'picture' => $image,
'actions' => array(
array(
'name' => $testo,
'link' => $action_link
)
)
);
$post = $facebook->api($post_url,"POST",$params);
}
catch (FacebookApiException $e) {
$result = $e->getResult();
}
}
?>
I just would like to have a clarification about the image question.
Does the image have to still exist ?
Or once posted, I can delete them, as they still live on Fb ?
My questione born from the fact that i see (while using the old script in the top of this post) on the error log of my site a lof of row of 404 error (related to the missed images searched from Fb)
i understood that the images where uploaded to facebook, so they were not needed anymore on my space....
This is not the case. Facebook simply caches your content in their CDN. If you delete the source photos, when the content falls out of their cache, their attempt to refresh / re-fill the cache will fail and your users will get a broken image icon.
Does the image have to still exist ? Or once posted, I can delete them, as they still live on Fb ?
You should not delete them.

CodeIgniter and Facebook Connect

I am trying to integrate my Codeigniter website with the Facebook PHP SDK. What I want to do is let a user share an article from my site on their facebook wall, if they are logged into a facebook account. My library appears to load correctly, but everytime I try to do something, I get some kind of error... primarily with the auth. getUser does not appear to return the correct results. I set up my facebook application and set the config vars for my library, but no luck. It says I am not logged into facebook. When I click on the "login" anchor, the link takes me to the same page, but with the facebook url, and doesn't ask me to login with the app. Here's my code:
function facebook($article_id){
$config = array(
'appId' => '276870792431073',
'secret' => '8d49eee575413fb9a8063d22f65dbf6a'
);
$this->load->library('facebook', $config);
$user = $this->facebook->getUser();
if($user){
try {
$user_profile = $this->facebook->api('/me');
} catch (FacebookApiException $e){
error_log($e);
$user = null;
}
}
if($user){
$article = $this->article->fetch_article($article_id);
$config = array(
'message' => 'I just read an '.anchor('articles/'.url_title($article['title']).'/'.$article_id, 'article').' on '.anchor('', 'TrackTheOutbreak.com').'!',
);
$this->facebook->api('/me/feed', 'post', $config);
} else {
$data['MESSAGE_TITLE'] = 'Authentication Error';
$data['MESSAGE_TEXT'] = 'You must be logged into an existing Facebook account to use this feature. Click '.anchor($this->facebook->getLoginUrl(), 'here').' to login.';
$this->parser->parse('error_body.tpl', $data);
}
}
In order to access a users information and post anything to their wall you first need to get a access token from them. To do that you need to make sure that you have gained their permission through Facebook's FB_login (and then Open Graph). I would double check with this guide and make sure that you have everything set up properly to post to their timeline.
https://developers.facebook.com/docs/reference/javascript/FB.login/
I hope this helps

How to present an Unlike link for a Facebook page

I have a Facebook app which is interested in whether users like or unlike specific pages. I want to offer a link to unlike a page, as so far as I know there is no way to unlike a page programmatically using the Graph API.
Facebook provide a like button, but (again, so far as I know) no unlike button. How can I present an option to unlike a page without saying "Click here to go to the page, scroll down to the miniscule unlike link and click it"?
You can issue a DELETE request to the page_id
suppose the page id is 1234567890123
place the link as on the a page as:
Unlike
Now collect the GET parameter in the unlike.php file and fire a DELETE request through the graph api:
<?php
$fb = new Facebook(array(
'appId' => FB_APP_ID,
'secret' => FB_APP_SECRET,
'cookie' => true
));
$session = $fb->getSession();
if (empty($session))
{
$loginUrl = $fb->getLoginUrl();
echo "<script>window.top.location='{$loginUrl}';</script>";
exit;
}
$fb_liked_page = $_GET['d']; //COLLECT THE PAGE ID HERE
try
{
$status = $fb->api("/{$fb_liked_page}/likes", 'DELETE');
if ($status)
{
echo "Success, Unliked!";
}
else
{
echo "Could not unlike this page.";
}
}
catch (Exception $e)
{
var_dump($e);
}

Facebook iFrame app - how to fetch Preload FQL result using PHP SDK?

since few years I have an FBML app (a small Flash game) which I'm now trying to convert to an iFrame app. Unfortunately there aren't many docs for Facebook iFrame apps yet.
For my game I need the user's first name, picture, gender and the city.
In my old version I had this preload FQL (created once by a PHP script):
$fql = array('info' => array('pattern' => 'facebook',
'query' => 'SELECT first_name, sex, pic_big, current_location
FROM user WHERE uid={*user*}'));
$fb->api_client->admin_setAppProperties(
array('preload_fql' => json_encode($fql)));
and then my FBML app script had been as simple as:
<?php
require_once('facebook.php');
define('FB_API_ID', 'XXX');
define('FB_AUTH_SECRET', 'YYY');
$fb = new Facebook(FB_API_ID, FB_AUTH_SECRET);
$viewer_id = $fb->require_login();
$data = json_decode($fb->fb_params['info'], true);
$first_name = $data[0][0];
$last_name = $data[0][2];
$female = ($data[0][3] != 'male');
$avatar = $data[0][3];
$city = $data[0][4]['city'];
# and then I'd just construct flashvars attribute
# for the <fb:swf ...> tag and print it
?>
Does anybody please have hints on how to recreate the same script for the iFrame version - i.e. how can I fetch the result of Preload FQL by my iFrame app?
According to an older Facebook blog entry Preload FQL should be accessible by the iFrame apps.
Thank you!
Alex
My own answer after long searching is that Preload FQL results aren't sent to iframe Facebook apps.
That is why Facebook performance doc says:
"Preload FQL Query and Multiquery.
This section applies to FBML canvas pages, but not to websites or IFrame canvas pages."
As Facebook said for Preload FQL
"Facebook will send the result of these FQL queries as JSON-encoded POST parameters to your Canvas URL"
print_r your $_POST and see which variable is the "json-encoded results". You convert json into php object using json_decode
JSON looks like this: {"var":"val","var":"val"}
Also, Facebook already has great docs for iframes. Then you might have missed these great docs:
Facebook Docs Home
http://developers.facebook.com/docs/
Authentication
http://developers.facebook.com/docs/authentication/
Signed Request
http://developers.facebook.com/docs/authentication/signed_request/
iFrame Canvas Apps
http://developers.facebook.com/docs/guides/canvas/
PHP SDK
https://github.com/facebook/php-sdk
Graph API
http://developers.facebook.com/docs/reference/api/
You don't need to call any FQL for the information you are getting. For iFrame you just need to do following steps
Download the PHP SDK of graph api https://github.com/facebook/php-sdk/
Create the object and authorize the app from user
$fbconfig['appid' ] = "your application id";
$fbconfig['api' ] = "your application api key";
$fbconfig['secret'] = "your application secret key";
try{
include_once "facebook.php";
}
catch(Exception $o){
echo '<pre>';
print_r($o);
echo '</pre>';
}
// Create our Application instance.
$facebook = new Facebook(array(
'appId' => $fbconfig['appid'],
'secret' => $fbconfig['secret'],
'cookie' => true,
));
// User location extended permission allow you to get user's current location
$loginparams = array('canvas' => 1,'fbconnect' => 0,'req_perms' => 'user_location');
$loginUrl = $facebook->getLoginUrl($loginparams);
// We may or may not have this data based on a $_GET or $_COOKIE based session.
// If we get a session here, it means we found a correctly signed session using
// the Application Secret only Facebook and the Application know. We dont know
// if it is still valid until we make an API call using the session. A session
// can become invalid if it has already expired (should not be getting the
// session back in this case) or if the user logged out of Facebook.
$session = $facebook->getSession();
$fbme = null;
// Session based graph API call.
if ($session) {
try {
$uid = $facebook->getUser();
$fbme = $facebook->api('/me');
} catch (FacebookApiException $e) {
d($e);
}
}
function d($d){
echo '<pre>';
print_r($d);
echo '</pre>';
}
// You can found all the data in this array.
print_r($fbme);
For more detail you can follow this tutorial http://thinkdiff.net/facebook/php-sdk-graph-api-base-facebook-connect-tutorial/
Hope it works for you