Today all sites try to be social as they know that it generates more visitors and increase brand value. Now suppose we need to transfer all our flickr photos to facebook album. Normal step will be downloading photos from flickr first, then uploading it to facebook albums. This was the flow even using facebook apis till mid of 2011. Later facebook started supporting direct uploading of images from a given url directly to facebook profile or albums using api. In an earlier post, I discussed about uploading an image from local hard disk. Today we are going to discuss how to upload an image directly from an image url using facebook graph api.
This particular demo needs authentication. If you visit this page directly, you can handle the authentication part using my post, Facebook Authentication Using PHP SDK. Now once authentication is ready, we need to add publish_stream to config.php file. You can find everything in download zip file.
publish_stream is a standard name for one of the permissions set by facebook. For important and highly secure actions to be done by an app, the app has to first request permission from the user. In our case our app has to upload or publish a photo on user’s wall. For that we need publish_stream permission.
If you observe, the contents of download zip file, You can find a folder named ‘upload-photos-to-facebook-directly-from-url’. That folder contains the needed files for the demo. Now we need a simple form for the user to input image url and image caption. For that 2 textboxes are placed.
<form enctype="multipart/form-data" action=" " method="POST">
Please choose a photo:
<input name="source" type="file"><br/><br/>
Say something about this photo:
<input name="message"
type="text" value=""><br/><br/>
<input type="submit" value="Upload" class="btn btn-primary"/><br/>
</form>
Now the action attribute of the form is left blank. So when user clicks submit button, the page posts back and following code runs if post variables are set.
if(isset($_FILES["source"]["name"]))
{
try {
$facebook->setFileUploadSupport(true);
$response = $facebook->api(
'/me/photos/',
'post',
array(
'message' => $_POST['message'],
'source' => '@'.$_FILES["source"]["tmp_name"]
)
);
}
catch (FacebookApiException $e) {
error_log('Could not post image to Facebook.');
}
}
Here we are not using the api() function from $facebook object to run the graph api. It was not working when i tried api() function here. Instead we extract the access token from facebook object using getAccessToken(). Then this access token is used to create the graph api url. Since the photo has to be uploaded to logged in user’s wall the graph api connection will be /me/photos. In that case our app will create an album with name of our app and upload the photo to that album. Now if you know the album id, in that case the connection will be /[album_id]/photos. Now url field accepts the url of image, message string takes the image caption.
Our prepared graph url is requested using php file_get_contents() and response is converted to a json object using json_decode function.




Hi Joby Joseph.
This post is really awesome.
I got the solution for my problem only after reading your post here….
sir could you help me in redirecting to another page. I am using server side login authentication for the user using state parameter as specified in facebook.when i perform an action it is again redirecting to the same page
this code still works for you? here always returns the error:
[message] => (#1) An unknown error occurred
[type] => OAuthException
[/code] => 1
need to check.
This is great! But how can I change the name of the album that is created?