Facebook graph API & PHP SDK questions - php

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);

Related

publicating using php facebook api creates only permalink insted of post on facewall

I'm using PHP facebook API 3.2.3 to connect to facebook and publicate messages from my page.
Until recently it was working find, but now messages are not publicated.
When i copy returned by FB message ID to create link, it is there, but not on my facewall.
I can accesss to my message by link like this:
https://www.facebook.com/permalink.php?story_fbid=800134770033938&id=659754124150164
It says, that i've publicated link on my fanpage, but it is not visible on wall.
Ofcourse there are all necessary permissions.
Code looks like this:
$fb_fanpage_name = $fb['FBFanpageName'];
$fb_access_token = $fb['FBAccessToken'];
$fb_app_id = $fb['FBApp'];
$fb_secret = $fb['FBSecret'];
$facebook = new Facebook(array(
'appId' => $fb_app_id,
'secret' => $fb_secret,
'cookie' => TRUE,
));
$post = array(
'access_token' => $fb_access_token,
'link' => $fb_url
);
try
{
$res = $facebook->api('/' . $fb_fanpage_name . '/links', 'post', $post);
} catch (Exception $e)
{
$this->zp($e->getMessage());
}
According to FB API on page https://developers.facebook.com/docs/graph-api/reference/v2.3/page/links/ - you can't posting links like this.
Apparently You want post message to feed.
So to do this try :
$post = array(
'access_token' => $fb_access_token,
'link' => $fb_url,
'message' => 'try me!'
);
$facebook->api('/'. $fb_fanpage_name .'/feed', 'POST', $post);
Update : to find this look at page https://developers.facebook.com/docs/graph-api/reference/v2.3/page/feed

Facebook PHP SDK V3 login token - Fatal error: Uncaught OAuthException:

The code below works fine with the PHP SDK v3. However, I had to use a java sdk login at first and now I don't know how to get a token with PHP or check if the user is logged in or not with PHP. I GET THE ERROR BELOW.
I can not use the SDK 4 yet as my hosting will not allow it. Long story. I have tried SEVERAL sites and tutorials for the past few hours to no avail. Can someone show me a simple way to do this? With code examples? What do I need to change the canvas url to on the Facebook app? etc..
Here is an error I get without the login.
Fatal error: Uncaught OAuthException: An active access token must be used to query information about the current user. thrown in
require 'Facebook3/src/facebook.php';
$facebook = new Facebook(array(
'appId' => 'myID',
'secret' => 'MySecret',
));
$attachment = array(
'message' => 'COOL',
'name' => 'Some Info',
'caption' => "more info",
'link' => 'http://aLink.com',
'description' => 'View Trailer Here!',
'picture' => 'http://Picture.com/images/aPic.png',
'actions' => array(
array(
'name' => 'Get Search',
'link' => 'http://www.google.com',
)
)
);
$result = $facebook->api('/me/feed/', 'post', $attachment);

tweet post Image with {"errors":[{"code":189,"message":"Error creating status."}]}

I'm trying to publish tweet on twitter with image and a link to my site
I get the following error:
{"errors": [{"code": 189, "message": ". Error creating status"}]}
I tried with twitter api:
with update.json
the tweet is published but no picture.
and
update_with_media.json
I get error
this is the code I use:
require '../tmhOAuth.php';
require '../tmhUtilities.php';
$tmhOAuth = new tmhOAuth(array(
'consumer_key' => 'xxxxxxxxxx',
'consumer_secret' => 'xxxxxxxxxxxxx',
'user_token' => 'xxxxxxxxxxxxxxxxxxxx',
'user_secret' => 'xxxxxxxxxxxxxxxxxx',
));
// we're using a hardcoded image path here. You can easily replace this with an uploaded image-see images.php example)
// 'image = "#{$_FILES['image']['tmp_name']};type={$_FILES['image'] ['type']};filename={$_FILES['image']['name']}",
$picimg="img.jpg";
$code = $tmhOAuth->request('POST', 'https://api.twitter.com/1.1/statuses/update_with_media.json',
array(
'media[]' => "#{$picimg}",
'status' => "status"
),
true, // use auth
true // multipart
);
if ($code == 200) {
tmhUtilities::pr(json_decode($tmhOAuth->response['response']));
} else {
tmhUtilities::pr($tmhOAuth->response['response']);
}
I searched a lot on google but not served me anything.
I do not understand what's going on
It was poorly cast the image url and more stuff.
I have made an example of different api in my local.
I used APIs
https://github.com/themattharris/tmhOAuth
I've also installed composer for dependencies.
installing dependencies twitter api.
Well the most important thing is:
this piece of code:
$picimg="img.jpg";
array(
'media[]' => "#{$picimg}",
'status' => "status"
)
this is wrong.
but this above is good
$image = __DIR__.DIRECTORY_SEPARATOR .'..' . DIRECTORY_SEPARATOR . 'fotos'. DIRECTORY_SEPARATOR . 'foto1.jpg';
DIRECTORY_SEPARATOR is used instead of backslash, and this is very important if not the error response would be 189
$name = basename($image);
$status = "Picture time";
$media = "#{$image};type=image/jpg;filename={$name}";
media must be represented well in this format, not only url string.
// we set the type and filename are set here as well
$params = array(
'media[]' => $media,
'status' => $status
);
This is working AMIGOS

Post a picture fia Facebook API

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}',
)
);

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?

Categories