Facebook graph API upload photos to albums - php

I am using this code to upload photos to existing album with access_token
$FILE_PATH='C:\\wamp\\www\\photo\\photo.jpg';
$args = array('message' => 'Photo Caption');
$args['image'] = '#' . realpath($FILE_PATH);
$data = $facebook->api('/'. $aid . '/photos?access_token='. $user_access_token, 'post', $args);
print_r($data);
I have a valid access_token with publish_stream permission. I have checked i have a valid access_token, but here is an error comming.
> Fatal error: Uncaught OAuthException: An access token is required to request this resource. thrown in C:\wamp\www\photo\facebook-php-sdk\src\base_facebook.php on line 970

You need to set the upload support to true. The following code works for me:
$facebook->setFileUploadSupport(true);
$facebook->api('/'.$aid.'/photos', 'POST', array('image'=> '#' . realpath($FILE_PATH), 'message'=> 'Photo Caption'));
and furthermore, you will probably need the user_photos, photo_upload, and the publish_stream permissions.

The problem is that we need to set the access-token in the args,and then call to upload album, this was a fix that applied and it worked. thanks for your comments and answer,

Related

Facebook api does not upload image

My issue is already discussed here
couldn't open file "" error in Facebook PHP API
and my php script is
//variables we are going to post to facebook
$fbPermissions = 'publish_stream,user_photos'; //Required facebook permissions
$PicLocation = 'fb_cover_images/cover7.jpg';
$msg_body = array(
'message' => 'I liked this pic from '. $homeurl .
' it is perfect for my cover photo.',
'url' => "http://my_site.com/$PicLocation"
);
if ($fbuser){ //user is logged in to facebook, post our image
try {
$uploadPhoto = $facebook->api('/me/photos', 'post', $msg_body );
}
catch (FacebookApiException $e) {
echo $e->getMessage(); //output any error
}
}
else{
$loginUrl = $facebook->getLoginUrl(
array('scope'=>$fbPermissions,'return_url'=>$return_url));
header('Location: ' . $loginUrl);
}
If I set 'url' => "http://my_site.com/$PicLocation" then getting
(#200) Permissions error
and If I set 'url' => "#http://my_site.com/$PicLocation" then getting
couldn't open file "http://my_site.com/fb_cover_images/cover7.jpg"
message though if I hit "http://my_site.com/fb_cover_images/cover7.jpg", I can view image and permission to folder+image is set to 777.
Please guide what I am doing wrong....
I have searched but fail.
I also tried [PHP + Facebook: how to upload photo on the wall? but same rubbish message (#200) Permissions error
Also Upload Photo To Album with Facebook's Graph API
I don't know what the hell is going on with FB API.
Try adding the following line to the top of your code, after setting up Facebook:
$facebook->setFileUploadSupport( true );
Then, change your $msg_body to the following:
$msg_body = array(
'name' => 'I liked this pic from '. $homeurl . ' it is perfect for my cover photo.',
'source' => "http://my_site.com/$PicLocation"
);
The name is the caption for the image, and the source is the URL to the file. You only need to prefix # to the source if you are referencing a local file.

Unable to tag users to a fb photo uploaded via graph api

I just testing something and found out by script not working . It actually upload a pic and then use it pic id to tag some random peoples from the friend lists . My app have user_photos,publish_stream permissions . I can successfully upload the photo but i am getting error on making tags .
Here is The code:
$f1 = $facebook->api('me/friends?limit=19');
$img = $_REQUEST['imgl'];
$access_token = $facebook->getAccessToken();
$args = array(
'message' => $_REQUEST['m_Config']['appTitle'],
'source' => '#' . $img,
'access_token' => $access_token,
);
$photo = $facebook->api ( $user . '/photos', 'post', $args );
foreach($f1['data'] as $fbu){
$tagx = array('tag_uid' => $fbu['id'],'x' => rand(100,350),'y' => rand(100,350));
$ftags[] = $tagx;
}
$args = array (
'tags' => json_encode($ftags),
'access_token' => $access_token,
);
$result = $facebook->api('/' . $photo['id'] . '/tags', 'post', $args);
print_r($result);
Now the Error which , I am getting is Fatal error: Uncaught OAuthException: (#100) Invalid parameter thrown in C:\xampp\htdocs\fb\base_facebook.php on line 1254
My question is where , My code is wrong . WHy I am getting error , I also tried searching but cant get it fixed .
Thanks
You seem to be assigning the tags at random locations for random friends, which is completely wrong.
The user should be selecting where in the photo their friends are - anything else would be against policy and likely to be shut down as spam
As for your code problem, this isn't working because tag_uid isn't a listed parameter name in the API - the parameter to use for specifying which user to tag is to
See the Photo object's Tag connection documentation for more details and some example code.

Facebook: publish image to page album with app access token

How do I give a Facebook App permission to post images to a Facebook Page album?
This is the code I use:
$args = array(
'image' => '#' . $img,
'aid' => $album_id,
'no_story' => 1,
'access_token' => $app_access_token
);
$photo = $facebook->api($album_id . '/photos', 'post', $args);
This is the error I get:
Uncaught OAuthException: A user access token is required to request this resource.
Here is the code I use to publish a photo to an album on Facebook. I hope it'll help.
<?php
include '../src/facebook.php';
$config['appId']='app_id';
$config['secret']='API_KEY';
$config['fileUpload'] = true;
$param['redirect_uri']='your_redirect_url';
$param['scope']=',publish_stream,user_photos';
$facebook=new Facebook($config);
$userid=$facebook->getUser();
if($userid){
try{
$args = array(
'image' => '#' . $img,
'message'=>$message,
);
$photo = $facebook->api('/'.$album_id . '/photos', 'post', $args);
}
catch(FacebookApiException $e){
echo $e->getMessage();//Get OAuth Error
}
else{
$loginUrl=$facebook->getLoginUrl($param);
echo "Please <a href='$loginUrl'>Click Here</a> To Login";
}
?>
Learn more Here
Just in case any user stumbles upon this post , to resolve this error
1> $facebook->setaccesstoken() to facebook page's access token . The facebook page access token is available by issuing $facebook->api('/user_id/accounts'), here user id is the id of page's admin .
2> After setting access token you can use the facebook->api to post the photo to album using album id
$facebook->api('album_id/photos', 'post', array( 'source' => '#'.$photo ));

How can I upload photos to album using Facebook Graph API

Here is the code:
$file= 'bbbb.jpg';
$data = array(
basename($file) => "#".realpath($file),
"caption" => "Uploaded using graph api",
"aid" => '13595',
"access_token" => $accessToken,
'method' => 'photos.upload'
);
$sds =$facebook->api($data);
This is the error
Uncaught CurlException: 26: failed creating formpost data
What to do?
Here are some various ways to upload photos using the Graph API. The examples assume you've instantiated the $facebook object and have a valid session for the current user.
1 - Default Application Album of Current User
This example will upload the photo to your default application album of the current user. If the album does not yet exist it will be created.
$args = array('message' => 'Photo Caption');
$args['image'] = '#' . realpath($FILE_PATH);
$data = $facebook->api('/me/photos', 'post', $args);
print_r($data);
2 - Target Album
This example will upload the photo to a specific album.
$args = array('message' => 'Photo Caption');
$args['image'] = '#' . realpath($FILE_PATH);
$data = $facebook->api('/'. $ALBUM_ID . '/photos', 'post', $args);
print_r($data);
3 - Target Album with Access Token
This example will upload a photo to a specific album which requires an access token.
$args = array('message' => 'Photo Caption');
$args['image'] = '#' . realpath($FILE_PATH);
$data = $facebook->api('/'. $ALBUM_ID . '/photos?access_token='. $ACCESS_TOKEN, 'post', $args);
print_r($data);
your $data array should have "message" instead of "caption",
also, remove "aid", "method", and "access_token"
your $data has to have the file data and "message", that is it.
$sds =$facebook->api('/me/13595/photos', 'POST', $data);
where instead of 13595 just use the variable with the album aid
also, if needed, access_token is best appended to api uri like this:
$sds =$facebook->api('/me/13595/photos?access_token='.$access_token, 'POST', $data);
also, if the php sdk doesn't work for you, I have successfully used cURL instead if your php installation supports it. in that case see cURL example at Upload Photo To Album with Facebook's Graph API
The latest version of the Facebook PHP SDK wont work with the above examples without the following update to the code.
class Facebook {
...
*Line #539*
protected function makeRequest($url, $params, $ch=null) {
if (!$ch) {
$ch = curl_init();
}
if( isset($params['doMultiPart']) ) {
$doMultiPart= true;
unset($params['doMultiPart']);
} else {
$doMultiPart= false;
}
$opts = self::$CURL_OPTS;
$opts[CURLOPT_POSTFIELDS] = $doMultiPart ? $params : http_build_query($params, null, '&');
...
Basically the problem is that the PHP SDK uses "curl_setopt_array" which if you pass it a url encoded string as the option value it will pass the data as application/x-www-form-urlencoded when what you really want is multipart/form-data; to do this we simply switch to passing in the array of options if we have a param of doMultiPart in the params array.
This was a quick hack I put together to get something working, probably need to review the code to make sure it doesnt break anything else you are doing. Otherwise enjoy.

Facebook API + php, photos.upload() not working?

I've set up a Facebook application, I've requested the extended permissions and now I'm trying to upload a photo but it doesn't work!
I've tried everything, from
$facebook->api_client->photos_upload('photo/789165784.jpg');
To
$facebook->api_client->photos_upload('photo/789165784.jpg', NULL, 'My photo', 100000287894654);
I'm beginning to suspect that I need to set up some extensions for php. I'm using WAMP and since the server is currently offline, I can't test it on production until tomorrow (I think..).
Thanks!
Here are some various ways to upload photos using the Graph API. The examples assume you've instantiated the $facebook object and have a valid session for the current user.
1 - Default Application Album of Current User
This example will upload the photo to your default application album of the current user. If the album does not yet exist it will be created.
$args = array('message' => 'Photo Caption');
$args['image'] = '#' . realpath($FILE_PATH);
$data = $facebook->api('/me/photos', 'post', $args);
print_r($data);
2 - Target Album
This example will upload the photo to a specific album.
$args = array('message' => 'Photo Caption');
$args['image'] = '#' . realpath($FILE_PATH);
$data = $facebook->api('/'. $ALBUM_ID . '/photos', 'post', $args);
print_r($data);
3 - Target Album with Access Token
This example will upload a photo to a specific album which requires an access token.
$args = array('message' => 'Photo Caption');
$args['image'] = '#' . realpath($FILE_PATH);
$data = $facebook->api('/'. $ALBUM_ID . '/photos?access_token='. $ACCESS_TOKEN, 'post', $args);
print_r($data);
I don't know too much about this, I'm afraid, but the following link suggests that other people have had the same problem. Maybe this forum will be helpful:
http://forum.developers.facebook.com/viewtopic.php?pid=93450
It also links to this page, which seems like it might help:
http://wiki.auzigog.com/Facebook_Photo_Uploads
Ben

Categories