How can I upload photos to album using Facebook Graph API - php

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.

Related

Twitter chunked media upload with php

I was trying out a PHP framework called TwitterAPIExchange, and I did a test PHP to post to twitter, and it worked.
Now what I am trying to do is to upload a ‘chunked’ Media, such as a GIF, and response as a media ID. I layed out a simple Upload as media, which is not a chunked upload yet.
// files
$file = file_get_contents(__DIR__ . '/img.png');
$data = base64_encode($file);
// twitter api endpoint
$url = 'https://upload.twitter.com/1.1/media/upload.json';
// twitter api endpoint request type
$requestMethod = 'POST';
// twitter api endpoint data
$apiData = array(
'media_data' => $data
);
// create new twitter for api communication
$twitter = new TwitterAPIExchange( $settings );
// make our api call to twiiter
$twitter->buildOauth( $url, $requestMethod );
$twitter->setPostfields( $apiData );
$response = $twitter->performRequest( true, array( CURLOPT_SSL_VERIFYHOST => 0, CURLOPT_SSL_VERIFYPEER => 0 ) );
// display response from twitter
echo '<pre>';
print_r( json_decode( $response, true ) );
Is there a way I can call a INIT URL, and call on a APPEND request to upload the file and return the FINALIZE(d) media_id or rather media_id_str value?

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.

PHP + Facebook: how to upload photo on the wall?

The usual way as I post messages to my page wall is like:
$args = array(
'access_token' => $page_access_token,
'message' => $title,
'link' => $link,
'name' => 'This is title',
'description' => 'This is a testing of the posting on page',
//'picture' => 'http://www.example.com/directory/images/30.jpg'
);
$post_id = $facebook->api("/$pageId/feed","post",$args);
But how can I post an images to my wall - the alternative to: click to UPLOAD button, pick out an image -> and upload, image is on to wall.
I have on my FTP some images and these ones I would like to upload to my wall.
Thanks in advance
https://developers.facebook.com/blog/post/498/ this link can help you...
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.
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.
$facebook->setFileUploadSupport(true);
$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.
$facebook->setFileUploadSupport(true);
$args = array('message' => 'Photo Caption');
$args['image'] = '#' . realpath($FILE_PATH);
$data = $facebook->api('/'. $ALBUM_ID . '/photos', 'post', $args);
print_r($data);
I'm NOT using the Facebook API include, so i'm trying to integrate Facebook stuff using only curl requests directly to the Graph API URL.
So, to post images in the User's wall, the simplest way I found is just to indicate the Image URL.
<?php
$url = "https://graph.facebook.com/me/photos?access_token=" . $FacebookToken;
$url = $url . "&url=" . urlencode("http://www.url.to/the/image.jpg");
$url = $url . "&message=" . urlencode($Description);
$url = $url . "&method=POST";
$data = file_get_contents($url); // Can change to curl if
// file_get_contents is blocked on your host
?>
Here is a short example, recovered from my codes:
$fbPost = curl_init();
curl_setopt($fbPost, CURLOPT_RETURNTRANSFER, true);
curl_setopt($fbPost, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($fbPost, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($fbPost, CURLOPT_POST, true);
curl_setopt($fbPost, CURLOPT_ENCODING, 'gzip');
$photoInfo = array(
'access_token' => <USER_ACCESS_TOKEN>,
'name' => <IMAGE_DESCRIPTION>,
'url' => <ABSOLUTE_URL>/images/photo.jpg',
);
curl_setopt($fbPost, CURLOPT_URL, 'https://graph.facebook.com/<USER_ID>/photos');
curl_setopt($fbPost, CURLOPT_POSTFIELDS, $photoInfo);
$result = json_decode(curl_exec($fbPost), true);
curl_close($fbPost);

Get facebook Album URL using php-sdk

Using the facebook php-sdk, users can upload photos to facebook from our site. I can create the album and upload the photos. But can't get the URL to the newly created Album :(
Here's what I do ...
// create album
$albumDetails = array(
'name' => 'Fun images'
);
$album = $facebook->api('/me/albums', 'post', $albumDetails);
$albumID = $album['id'];
// upload photos
foreach ($images as $image) {
$file = FACEBOOK_IMAGES_DIR . $image->image_id . '.jpg';
$photoDetails = array(
'message'=> 'by choreboy'
);
$photoDetails['image'] = '#' . realpath($file);
$photoData = $facebook->api('/'.$albumID.'/photos', 'post', $photoDetails);
}
// Graph API says I can get link to the album:
// http://developers.facebook.com/docs/reference/api/album
// I thought I could get to the link data this way. But returns an empty array
$data=$facebook->api("/{$albumID}/photos?access_token={$session['access_token']}");
var_dump($data);
Maybe you are missing the first line in this code.
This URL redirects to your album:
$album_url = 'http://www.facebook.com/' . $albumID;

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