Post a picture fia Facebook API - php

I'm trying to post a message with picture (from a public URL, not from my albums) to group's wall:
$response = $facebook->api("/$group_id/feed", "POST", array(
'access_token=' => $access_token,
'message' => 'This is a test message',
'picture' => 'http://d24w6bsrhbeh9d.cloudfront.net/photo/agydwb6_460s.jpg',
)
);
Everything is fine, but I can't see a picture itself:
What am I doing wrong? Thanks in advance!

While posting a feed, you must specify a link along with a picture if want to add a picture to the feed; else its not a feed. Logical right?
Code:
'access_token=' => $access_token,
'message' => 'This is a test message',
'link' => '{link-to-share}',
'picture' => 'http://d24w6bsrhbeh9d.cloudfront.net/photo/agydwb6_460s.jpg',
If you dont want link, but just a picture, then you should simply upload the picture instead- by using /photos
Code:
$response = $facebook->api(
"/$group_id/photos",
"POST",
array (
'url' => '{image-url}',
)
);

Related

Create link with "#" symbol in facebook status with facebook api

I have problem with facebook api and can't find any information.
I want create link (example #Username) in status, with Graph API.
My code:
$response = $facebook->api("/" . $data['pageId'] . "/feed", 'POST',
[
'message' => $messageContainer['message'],
'link' => 'https://example.com/reviews',
'name' => $messageContainer['subject'],
'description' => $messageContainer['description'],
'picture' => 'https://example.com/uploads/dealer/' . $messageContainer['logo'],
]
);
$messageContainer['message'] = 'Some text #Username';
but text wasn't created as link in status only as string.
May be it's impossible?

facebook post article with user token

Hello i'm trying to post on a user's wall a big foto article like this one
I have tried this code
$post = array(
'access_token' => $row['user_token'],
'message' => 'Hello world ',
'link' => $row['url'],
//'name' => "This is my title",
//'caption' => "My Caption",
//'description' => "Some Description...",
'picture' => $imgurl
//'image' => $row['img']
);
$res = $facebook->api("/" . $row['facebook_id']. "/feed", 'POST', $post);
and it produces a small picture like this one
And this code produces nothing
/* make the API call */
$response = $facebook->api(
"/me/objects/article",//or /$user_id/objects/article
"POST",
array (
'fb:app_id' => '302184056577324',//change with stuff
'og:type' => 'article',
'og:url' => 'Put your own URL to the object here',
'og:title' => 'Sample Article',
'og:image' => 'https://s-static.ak.fbcdn.net/images/devsite/attachment_blank.png',
)
);
/* handle the result */
Can someone provide working code or explain like you would to a 7 years old ?
If you want the image to be displayed at the larger size, the source image has to be at least 1200x630px as described at https://developers.facebook.com/docs/opengraph/howtos/maximizing-distribution-media-content#images and already pointed out by CBroe...

Facebook post wall data when facebook offline ( access_token = staticValue )

I am trying to post dynamic data to facebook wall.
I downloaded facebook api for php.
First of all, as for testing, I use below code.
<?php
// Remember to copy files from the SDK's src/ directory to a
// directory in your application on the server, such as php-sdk/
require '../src/facebook.php';
$config = array(
'appId' => 'myAPPID',
'secret' => 'MySecretCode'
);
//'access_token' => $access_token = $facebook->getAccessToken(),
$facebook = new Facebook($config);
$attachment = array(
'access_token' => 'my_tokenkey',
'message' => 'Test Message offline test message',
'name' => 'Name Test offline test...',
'description' => 'here goes description and links http:anotherfeed.com | http://facebook.com/anotherfeed',
'caption' => 'this is caption for action link',
'picture' => 'http://www.google.com/tv/images/slivetv.png',
'link' => 'www.yahoo.com',
'properties' => array(
array('text' => 'test link 1', 'href' => 'http://anotherfeed.com/?ref=1'),
array('text' => 'test link 1', 'href' => 'http://anotherfeed.com/?ref=1'),
),
);
try {
$facebook->api('/me/feed', 'POST', $attachment);
} catch(FacebookApiException $e) {
echo $e;
# handling facebook api exception if something went wrong
}
?>
As for my program requirement, I want my program to post data to facebook's wall whether facebook is online or offline.
But when I try this code, I get error.
OAuthException: An active access token must be used to query information about the current user.
Could anyone please give me suggestions?

Facebook PHP image

I've serached a little but I didn't find anything that could solve my problem.
I've managed to post on page as page admin, link is added correctly, but image isn't attached. My code:
Blahblah authentication, etc.
$attachment = array(
'message' => 'text',
'name' => 'Name',
'link' => 'http://somelink.com',
'description' => '',
'access_token' => $ACCESS_TOKEN
);
if(something) $attachment['media'] = array(array('type'=>'image', 'src'=>'http://'.$_SERVER['HTTP_HOST']."/media/file/image_by_id/".$data['thumb_file_tree_id'].'/?w=400&h=500', 'href'=>'http://somelink.com'));
try {
if($facebook->api('/XXX/feed', 'post', $attachment))
{
echo 'Hooray, ok';
}
} catch (FacebookApiException $e) {
echo 'Damn';
}
Result: Correctly posted news with link ('Hooray, ok'), but without image. Can it be due to PHP generated image, and not png/jpg/gif extension? I've added some additional headers like etag, accepted-ranges, last modified, etc. What am I doing wrong (again)?
'Something' is true, checked with print_r whole attachment array.
E: Again removed [0]
To just attach an image, you can use the picture key in your attachment.
$attachment = array(
'message' => 'text',
'name' => 'Name',
'link' => 'http://somelink.com',
'description' => '',
'access_token' => $ACCESS_TOKEN,
'picture' => 'http://example.com/example.jpg'
);

Facebook graph API & PHP SDK questions

I have a couple of problems using facebook graph api.
1).
When i try to upload a photo i get the following error "(#324) Requires upload file".
$attachement = array(
'access_token'=> (...)',
'name' => 'uploaded foto',
'source' => 'C:\Documents and Settings\Username\Desktop\1.jpg'
);
$fb_foto = $fb->api('me/photos','POST',$attachement);
I am sure that source is correct. I have tried with a photo from the internet and not from mu local PC also.
2). How can i delete an object from facebook?. (a wall message for example).
I have tried this:
$fb->api('/post_id','POST',array('method'=> 'delete'));
But i get "unsupported POST request" error.
Any help would be appreciated.
EDIT: Here is the complete solution i found for upload photos to Facebook. You need to have the version 2.1.1 or above of PHP SDK
$fb = new Facebook(array(
'appId' => ...,
'secret' => ...,
'cookie' => true,
));
$fb->setFileUploadSupport(true);
$attachement = array(
'access_token'=> '...',
'name' => 'uploaded foto',
'source' => '#absolute_path_to_the_file'
);
$fb_foto = $fb->api('me/photos','POST',$attachement);
Concerning your 2nd problem, I remember reading somewhere about a DELETE request, instead of POST. See:
http://developers.facebook.com/docs/api#deleting
Read the documentation
curl -F 'access_token=...' \
-F 'source=#file.png' \
-F 'message=Caption for the photo' \
https://graph.facebook.com/me/photos
See how the source parameter is formatted?
$fb_foto = $fb->api('me/photos','POST',array(
'access_token' => (...)
, 'message' => 'Caption'
, 'source' => '#' . realpath( 'path/to/file' )
));
To delete photos, again the documentation has your answer: Issue a DELETE request
$fb->api( '/PHOTO_ID', 'DELETE' );
I'm using this method with the new php-sdk (v2.1.1, facebook-php-sdk-v2.1.1-0-g08909f3.zip) to upload a photo for a new event. The php code below creates the event.
$fname="/tmp/foo.jpg";
$attachment = array(
'access_token' => $facebook->getAccessToken(),
'name' => substr(event_name),
'description' => my_description,
'start_time' => my_start_time,
'link' => my_link,
'source'=> '#'.$fname
);
$result = $facebook->api('/me/events', 'POST', $attachment);

Categories