How to share a link on another user's wall - facebook

Using the Graph API Explorer, I tried posting the following: /[another_user_id]/links and a link field
but I am getting this error:
(#240) Requires a valid user is specified (either via the session or via the API parameter for specifying the user.
What am I missing here? Using /[another_user_id]/feed works but I want it to be a shared link.

The Following Code Posts In Users Friends wall.
try {
$newStatus = $facebook->api("/$FRIENDSUSERID/feed", 'POST',
array(
'link' => "https://apps.facebook.com/XXXXXXXXX",
'picture' => "https://XXXXXXXXX", //colour
'name' => "XXXXXXXXX",
'description' => "XXXXXXXXX",
'access_token' => $access_token
));
//echo '<pre>Post ID: ' . $newStatus['id'] . '</pre>';
} catch(FacebookApiException $e) {
error_log($e->getType());
error_log($e->getMessage());
//echo '<textarea style="width: 300px; height: 200px;">' . $e->getMessage() . '</textarea>';
}

Related

When sharing a link, can't specify custom picture, title, description fields

I've created a new app using version 2.9 of the Facebook PHP API. When trying to share a link, I can't force the post to contain the picture, title, and description fields I've specified.
Instead, Facebook uses Open Graph to dynamically pull an image URL from the shared page as well as its title and description. However, these are not what I want to appear on Facebook.
Here is some sample code:
$linkData = [
'message' => 'A new test post to Facebook',
'link' => 'https://www.example.com',
'picture' => 'https://www.example.com/image.jpg',
'name' => 'Test name',
'caption' => 'Test caption',
'description' => 'Test description'
];
try {
$response = $fb->post('/me/feed', $linkData, $pageAccessToken);
} catch(Facebook\Exceptions\FacebookResponseException $e) {
echo 'Graph returned an error: '.$e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
echo 'Facebook SDK returned an error: '.$e->getMessage();
exit;
}
$graphNode = $response->getGraphNode();
echo 'Posted with id: ' . $graphNode['id'];
Is there any way to force Facebook to use the custom parameters I've specified?

Facebook PHP SDK - An active access token must be used to query information about the current user

I have problem with Facebook PHP SDK. It always throws that exception. I tried many solutions listed here, but nothing works for me.
It seems that Facebook returns to me valid access token, because I tested it with Debug tool in dashboard of my application.
What's my scenario?
I want to post to publish simple content to user's wall by calling static function:
function social_publish($network, $title, $message, $link = '', $image = '') {
global $_config;
// Initialize Facebook SDK
$facebook = new Facebook(array(
'appId' => $_config['fb_app']['app_id'],
'secret' => $_config['fb_app']['app_security_key']
));
// Set data
$attachment = array(
'name' => $title,
'caption' => $title,
'message' => $message,
'link' => $link,
'picture' => $image,
'actions' => array('name' => 'Test', 'link' => 'Link')
);
try {
$access_token = $facebook->getAccessToken(); // returns valid access token
$uid = $facebook->getUser(); // always return 0
$result = $facebook->api( '/' . $_config['fb_profile'] . '/feed/', 'post', $attachment); // $_config['fb_profile'] procudes 'me' in this case
} catch (FacebookApiException $e) {
echo $e->getMessage();
}
}
Just to note: I am not working on local environment.
problem solve it as asked in scope request to Facebook for authentication, as I decided to use the JavaScript SDK-and then, here's the solution:
FB.getLoginStatus(function(response) {
if ( ! response.authResponse ) {
FB.login(function(response) {
// handle the response if needed
}, {scope: 'publish_actions, publish_stream'});
}
});
Thank you! :-)

How to Post on FB app users wall through API code

I have created a facebook app and need to post on all users FB WAll, who have authorized app to post on there behalf.
i.e, A new event organized somewhere, So through App, admin can post on every App Users FB Wall about the event.
I was using Graph API, however its not working.
Any Pointers/ Guidance will be highly appreciated.
Thanks
You have to do an api call with parameters to post on the users wall.
Edit.:
Here is the below linked example, with some explanation:
<?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'
);
?>
$result = $facebook->api('/me/feed/', 'post', $attachment);
The $attachment array contains the parameters of the wall post and the $facebook->api('/me/feed/','post',$attachment); make the call and returns the result into $result.
Here you can find the source: How do you post to the wall on a facebook page (not profile)
Publishing to user's wall in JS SDK:
var body = 'Status Test';
FB.api('/me/feed', 'post', { message: body }, function(response) {
if (!response || response.error) {
alert('Error occured');
} else {
alert('Post ID: ' + response.id);
}
});
[EDIT]
In Php
$ret_obj = $facebook->api('/me/feed', 'POST',
array(
'message' => 'Posting with the PHP SDK!'
));

facebook-php-sdk how to upload photo to wall or album?

Hi Guys i'm e newbie in php programin and facebook app, i found this code and it works very well for TEXT posts, but my requirements are that I have to UPLOAD a picture too, i tried by addin ['source' => '$image_source'] on the arrey but does not work...
How can I upload a PHOTO ?
<?php
include_once("config.php");
if($_POST)
{
//Post variables we received from user
$userPageId = $_POST["userpages"];
$userMessage = $_POST["message"];
$image_source = "http://www.example.com/image.jpg"
if(strlen($userMessage)<1)
{
//message is empty
$userMessage = 'No message was entered!';
}
//HTTP POST request to PAGE_ID/feed with the publish_stream
$post_url = '/'.$userPageId.'/feed';
//posts message on page statues
$msg_body = array(
'message' => $userMessage,
'source' => '$image_source'
);
if ($fbuser) {
try {
$postResult = $facebook->api($post_url, 'post', $msg_body );
} catch (FacebookApiException $e) {
echo $e->getMessage();
}
}else{
$loginUrl = $facebook->getLoginUrl(array('redirect_uri'=>$homeurl,'scope'=>$fbPermissions));
header('Location: ' . $loginUrl);
}
//Show sucess message
if($postResult)
{
echo '<html><head><title>Message Posted</title><link href="style.css" rel="stylesheet" type="text/css" /></head><body>';
echo '<div id="fbpageform" class="pageform" align="center">';
echo '<h1>Your message is posted on your facebook wall.</h1>';
echo '<a class="button" href="'.$homeurl.'">Back to Main Page</a> <a target="_blank" class="button" href="http://www.facebook.com/'.$userPageId.'">Visit Your Page</a>';
echo '</div>';
echo '</body></html>';
}
}
?>
You cannot use the feed endpoint to post photos. You should be using /me/photos with the publish_stream permission. To post to an album you need the /ALBUM_ID/photos endpoint
$msg_body = array(
'message' => $userMessage,
'source' => '#'.'$image_source'
);
$postResult = $facebook->api('/me/photos/','post', $msg_body);
For using urls not associated with your site you need to use url
$msg_body = array(
'message' => $userMessage,
'url' => 'http://somepage.com/img.png'
);

How to on Facebook wall using Graph API

I have developed an application for facebook. I want that whenever any user add my application by clicking on allow permission dialog, a message is automatically posted on the users wall only for the first time.
<?php
include_once 'fb_sdk_212/src/facebook.php';
include_once 'config.php';
$flag_post=0;
$facebook = new Facebook(array(
'appId' => FACEBOOK_APP_ID,
'secret' => FACEBOOK_SECRET_KEY,
'cookie' => true,
'domain' => 'xxxxx.in'
));
$session = $facebook->getSession();
if (!$session) {
$flag_post=1;
$url = $facebook->getLoginUrl(array(
'canvas' => 1,
'fbconnect' => 0,
'req_perms' => 'email,user_birthday,status_update,publish_stream'
));
echo "<script type='text/javascript'>top.location.href = '$url';</script>";
} else {
try {
$uid = $facebook->getUser();
$me = $facebook->api('/me');
$updated = date("l, F j, Y", strtotime($me['updated_time']));
echo "Hello " . $me['name'] . "<br />";
echo "You last updated your profile on " . $updated."<br/>";
if($flag_post == 1){
# let's check if the user has granted access to posting in the wall
$api_call = array(
'method' => 'users.hasAppPermission',
'uid' => $uid,
'ext_perm' => 'publish_stream'
);
$can_post = $facebook->api($api_call);
echo $can_post;
$attachment = array(
'name' => 'Tracer',
'description' => 'xxxx',
'caption' => 'xxxxx',
'picture' => 'images/mt75.jpg',
'link' => 'tracer/'
);
if($can_post){
# post it!
$facebook->api('/'.$uid.'/feed', 'post', $attachment );
echo 'posted';
} else {
die('Permissions required!');
}
}
} catch (FacebookApiException $e) {
echo "Error:" . print_r($e, true);
}
}
?>
Well you need to get publish_stream extended permission.
http://developers.facebook.com/docs/authentication/permissions
This can be done in the permission dialog
http://developers.facebook.com/docs/reference/javascript/FB.login
by adding publish_stream to the perms list.
After that you will be able to publish to the user's wall at any time.
I would do one of the following:
Add a field in a database table to indicate that it has been done. Then just check if it has been set. If cookies are enabled on the users PC you could use but using a database would be best.
Get the user to take an action to do the post. So it requires a user action. This has the disadvantage that some users may not do it.
I feel like you can handle this fairly easily without Facebook being involved in decision to publish to the users wall or not. In your database's user table you should have a column for first_fb_post. When a user account is created it should default to 0, then once you publish the wall post it should be updated to 1.
And your facebook publish function would be set like:
if($first_fb_post == 0)
{
//your FB publishing code here
//update your user table to mark this users first_fb_post to 1
}
You could hook into the first time the user "installs" your app. When they do this they are redirected to your site with a get parameter of 'code' set in the url:
if(isset($_GET['code'])){
// User Authorized app. Lets hook the install event.
if($can_post){
# post it!
$facebook->api('/'.$uid.'/feed', 'post', $attachment );
echo 'posted';
} else {
die('Permissions required!');
}
}