Posting on Facebook wall from Codeigniter app - facebook

I have a CI-based app that allows users to post an update stream similar to Facebook's wall.
Currently, users can authenticate into my app via Facebook using FB connect.
I would like to offer the possibility of a user -- when posting to my app's wall -- also be able to send the same post to his/her Facebook wall.
It seems clear the FB's Graph API support this but I'm having a hard time in finding a roadmap/ code/ library to help with this. The example on the above link is unhelpful and doesn't give me any idea how to implement this.
For example, how would a controller for this function look like?
I've found Elliot's FB CI library here, but am unsure if this is needed to accomplish what I want.
Any advice is greatly appreciated - thanks.

I would suggest you use Facebook PHP SDK
In your controller just include facebook php sdk eg: https://github.com/facebook/php-sdk/blob/master/examples/example.php
To make a wall post the following code should do the trick:
$wall_post = 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', $wall_post);

I ended up using a codeigniter helper function with curl to post to Facebook. Below is the code.
function facebook($data) {
$CI =& get_instance();
$CI->load->model('fk_model');
$token = $CI->fk_model->fk_cookie();
$attachment = array(
'access_token' => $token['access_token'],
'message' => $data['text'],
'link' => $data['link'],
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'https://graph.facebook.com/' . $token['id'] . '/feed');
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);
}

Related

Invalid access token facebook error for facebook graph api

I am using below curl code which is based on #unificationengine API to access facebook graph api and post message on facebook:
$post_msg = json_encode(
array(
'message' =>
array(
'receivers' =>
array(
array(
'name' => 'Me',
'address' => 'https://graph.facebook.com/v2.5/7/feed?access_token='.$request->access_token,
'Connector' => 'facebook'
),
),
'sender' =>
array('address' => 'sender address'),
'subject' => 'Hello',
'parts' =>
array(
array(
'id' => '1',
'contentType' => 'text/plain',
'data' => 'Hi welcome to UE',
'size' => 100,
'sort' => 0
),
),
),
)
);
$ch = curl_init('https://apiv2.unificationengine.com/v2/message/send');
curl_setopt($ch, CURLOPT_USERPWD, "ab33333222b-acb5-49a6-a766-80d991daff41:43433232-33cb-49f0-3333-3fe6c46acb5f");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_msg);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// execute!
$response = curl_exec($ch);
// close the connection, release resources used
curl_close($ch);
// do anything you want with your response
var_dump($response);
return ['label' =>$response];
I am getting invalid access token error with code 498. I referred various posts on this topic but couldn't figure out that what is missing.
How to check validity of facebook access token.
Referenced these questions:
SO question 1
SO question 2
The facebook access tokens have a lifetime of about two hours. For longer lived web apps, especially server side, need to generate long lived tokens. Long lived tokens generally lasts about 60 days.
UE has a capability to refresh facebook tokens. After adding connection using "apiv2.unificationengine.com/v2/connection/add"; api call, then you should call "apiv2.unificationengine.com/v2/connection/refresh"; api to make the short lived token to long lived.

upload photo to page timeline Facebook SDK

I made this small app that basically asks a user to login and post a photo to a FB page.
the code I have works great for posting on your own wall, but when it comes to posting to a page I am having some difficulties.
require_once('../src/facebook.php');
$config = array(
'appId' => 'XXXXXXXXXXXXX',
'secret' => 'XXXXXXXXXXXXXXX',
'fileUpload' => true,
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
$photo = realpath("mypic.png"); // Path to the photo on the local filesystem
$message = 'Photo upload via the PHP SDK!';
if($user_id) {
try {
$ret_obj = $facebook->api('/PAGE_ID_HERE_??/photos', 'POST', array(
'source' => '#' . $photo,
'message' => $message,));
if i use feed instead of photos, like here
$facebook->api('/PAGE_ID_HERE_??/FEED',
it works, but only posts the message.
i have all permissions needed:
user_photos user_videos publish_action
manage_pages publish_stream
To interact with the page you need the access token of the user. The user needs to be the 'manager' or 'content creator' of the page. And those tokens are per user per page.
I have a PHP example where this is handled using a cURL call. I think it gives the idea.
$ch = curl_init();
$url = "https://graph.facebook.com/" . $album_id . "/photos?access_token=" . $access_token;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
$retdata = curl_exec($ch);
echo($retdata);
In this one the $album_id is the album id of the page and the $access_token is the access token for the user for the page.
Also here's an example by Facebook for a personal album. You can chage the album ID and the token and use that approach.

How to post to my facebook wall from my own adminpages(CMS)?

How can I post a ordinary post to my facebook wall from my administration pages where I upload content to my webpage?
So I upload content to my webpage from my CMS and next to where I display my uploaded content in my adminpages I would like to have a button that can publish that post to my facebook wall. As an ordinary post and not like a LIKE post or Comment post!
First you need to create an facebook app.
Then you will get an app id and a secret key.
Using this details you can do post into ur wall using facebook php library
or u can use the following function
<?php
function doWallPost($postName='',$postMessage='',$postLink='',$postCaption='',$postDescription='')
{
$FB_APP_ID='xxxxxxxxxxxxxxxxxxxxxxxx';
$FB_APP_SECRET='xxxxxxxxxxxxxxxxxxxxxxxxxxx';
$APP_RETURN_URL=((substr($_SERVER['SERVER_PROTOCOL'],0,4)=="HTTP")?"http://":"https://").$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'];
$code = $_REQUEST["code"];
if(empty($code))
{
$dialog_url = "http://www.facebook.com/dialog/oauth?client_id=".$FB_APP_ID."&redirect_uri=".$APP_RETURN_URL."&scope=publish_stream";
header("Location:$dialog_url");
}
$token_url = "https://graph.facebook.com/oauth/access_token?client_id=".$FB_APP_ID."&redirect_uri=".urlencode($APP_RETURN_URL)."&client_secret=".$FB_APP_SECRET."&code=".$code;
$access_token = file_get_contents($token_url);
$param1=explode("&",$access_token);
$param2=explode("=",$param1[0]);
$FB_ACCESS_TOKEN=$param2[1];
$url = "https://graph.facebook.com/me/feed";
$attachment = array( 'access_token' => $FB_ACCESS_TOKEN,
'name' => $postName,
'link' => $postLink,
'description' => $postDescription,
'message' => $postMessage,
'caption' => $postCaption,
);
$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_CONNECTTIMEOUT,2);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
$result=curl_exec($ch);
header('Content-type:text/html');
curl_close($ch);
return $result
}
?>
For details
follow How to post wall in facebook using API in PHP?
function postonwall(){
// showLoader(true);
FB.api('/me/feed', 'post',
{
message : "testtext.",
link : 'http://www.mydomain.se',
picture : 'http://www.mydomain.se/image.jpg',
name : 'iOS Apps & Games',
description : 'Checkout iOS apps and games from iThinkdiff.net. I found some of them are just awesome!'
},
function(response) {
// showLoader(false);
if (!response || response.error) {
alert('Error occured');
} else {
//alert('Post ID: ' + response.id);
alert('Success: Content Published');
}
});
}

Facebook Graph API - Photo Upload

I have an application that uses the old Facebook API but now I'm migrating it. The application works good until I try to upload a photo.
I knew how to do it in the old way, but now... I'm in troubles.
This is the way I used to do it:
$args = array
(
'method' => 'photos.upload',
'v' => $ver,
'api_key' => $key,
'uid' => $uid,
'call_id' => $cid,
'format' => 'XML',
'caption' => $caption
);
signRequest($args, $sec);
$args[basename($file)] = '#' . realpath($file);
$ch = curl_init();
$url = 'http://api.facebook.com/restserver.php?method=photos.upload';
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
$data = curl_exec($ch);
Any ideas??
Thanks
The API url starts with https:// not http://. That could be the issue.
I found the solution here:
Uploading a picture to facebook
There is shown how to use the new Facebook Graph API with the PHP Curl function and a valid session token.

revokeApplication Using Facebook's Graph API

I'm in the final stages of converting our site over to Graph API from the Rest API.
The last piece I'm missing is the old "revokeApplication" call used for when a user chooses to "remove connection" from our site.
Despite my desires to completely remove the Rest API, I thought I might just fire it up for this, but it requires a session key -- something no longer stored in the Graph API.
Anybody have any ideas?
I figured it out. I'll leave it here for those that need to know...
The old rest api (including the revokeApplication api) can still be accessed, now with the new OAuth access_token. Just use this url: https://api.facebook.com/method/METHODNAME
For this particular call, it's a POST:
$ch = curl_init();
curl_setopt($ch, CURLOPT_POSTFIELDS, 'access_token='.$users_access_token);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL, 'https://api.facebook.com/method/auth.revokeAuthorization');
$output = curl_exec($ch);
curl_close($ch);
More info here:
http://developers.facebook.com/docs/reference/rest/
You can do it with the new graph API :
$facebook = new Facebook(array(
'appId' => $fbconfig['appid'],
'secret' => $fbconfig['secret'],
'cookie' => true
));
$revoked = $facebook->api("/me/permissions", "DELETE");
$revoked is a boolean.