How To: Upload A Photo To A User’s Profile Using Facebook Graph API
Uploading a photo to the user’s profile is an easy task using the Facebook Graph API.
As mentioned in the Facebook blog post:
In order to publish a photo to a user’s album, you must have the
publish_streampermission. With that granted, you can upload a photo by issuing an HTTP POST request with the photo content and an optional description to one these to Graph API connections:
https://graph.facebook.com/USER_ID/photos– The photo will be published to an album created for your app. We automatically create an album for your app if it does not already exist. All photos uploaded this way will then be added to this same album.https://graph.facebook.com/ALBUM_ID/photos– The photo will be published to a specific, existing photo album, represented by theALBUM_ID.
So basically, you need the publish_stream permission and using one of the following approaches:
Without using the PHP-SDK:
<?php
error_reporting(E_ALL & ~E_NOTICE);
$app_id = "APP_ID";
$app_secret = "APP_SECRET";
$my_url = "REDIRECT_URI"; // mainly, redirect to this page
$perms_str = "publish_stream";
$code = $_REQUEST["code"];
if(empty($code)) {
$auth_url = "http://www.facebook.com/dialog/oauth?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url)
. "&scope=" . $perms_str;
echo("<script>top.location.href='" . $auth_url . "'</script>");
}
$token_url = "https://graph.facebook.com/oauth/access_token?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url)
. "&client_secret=" . $app_secret
. "&code=" . $code;
$response = file_get_contents($token_url);
$p = null;
parse_str($response, $p);
$access_token = $p['access_token'];
$graph_url= "https://graph.facebook.com/me/photos?"
. "access_token=" .$access_token;
if (!empty($_FILES)) {
$params = array();
if( isset($_POST['message']) ) {
$params['message'] = trim($_POST['message']);
}
$uploaddir = './uploads/'; // Upload folder
$uploadfile = $uploaddir . basename($_FILES['source']['name']);
if (move_uploaded_file($_FILES['source']['tmp_name'], $uploadfile)) {
$params['source'] = "@" . realpath($uploadfile);
}
// Start the Graph API call
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$graph_url);
/*
Next option is only used for
user from a local (WAMP)
machine. This should be removed
when used on a live server!
refer:https://github.com/facebook/php-sdk/issues/7
*/
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
$result = curl_exec($ch);
$decoded = json_decode($result, true);
curl_close($ch);
if(is_array($decoded) && isset($decoded['id'])) {
/*
Picture is uploaded successfully:
1) show success message
2) optionally, delete image from our server
*/
$msg = "Image uploaded successfully: {$decoded['id']}";
}
}
?>
<!doctype html>
<html>
<head>
<title>Upload</title>
<style>
label {float: left; width: 100px;}
input[type=text],textarea {width: 210px;}
#msg {border: 1px solid #000; padding: 5px; color: red;}
</style>
</head>
<body>
<?php if( isset($msg) ) { ?>
<p id="msg"><?php echo $msg; ?></p>
<?php } ?>
<form enctype="multipart/form-data" action="" method="post">
<p><label for="name">Caption</label><input type="text" name="message" value="" /></p>
<p><label for="source">Photo</label><input type="file" name="source" /></p>
<p><input type="submit" value="Upload" /></p>
</form>
</body>
</html>Please note that file upload snippet is for demonstration purpose only, you should NOT use it on live websites!
Using the Facebook PHP-SDK
$file = FCPATH . "assets/img/small1.jpg";
$post_data = array(
"message" => "My photo caption",
"source" => '@' . realpath($file)
);
$data['photo'] = $this->facebook->api("/me/photos", 'post', $post_data); Code was taken from my answer here.
Back to top- http://www.facebook.com/eliwedel Eli Wedel
- http://www.facebook.com/profile.php?id=654770068 Muhammadh Amin
- http://www.facebook.com/KelliDandelake Kelli Dandelake
- Mani
- http://www.facebook.com/profile.php?id=654770068 Muhammadh Amin
- Mchprod
- Mozack
- brijkishor rajput
- http://www.facebook.com/profile.php?id=708266359 Daniel Ireson
- Shehzad Mo
- http://www.facebook.com/gianluca.suarato Gianluca Suarato
- http://www.facebook.com/nikz99 Nikz Kamal
- Joby Joseph
- Vanessah2478
- http://www.facebook.com/breitlingmusicgroup Tee Mo MuLa
- Jay Bhojwani
- Jay Bhojwani
- Jay Bhojwani
Advertisment
Recent Tutorials
- How To: Create A User Photo Albums Browser Using Facebook Graph API
- How To: Upload A Photo To A User’s Profile Using Facebook Graph API
- How To: Check Status And RSVP To Facebook Events Using Graph API & FQL
- Facebook Javascript SDK Best Practices
- How To: Create Facebook Events Using Graph API – Advanced







