Upload Image to Facebook and Tag them Using PHP

Our aim is very simple but really worthy for commercial applications. We have seen several applications that create some top friends list as an image and upload them on our wall with several tags. This enables the application to send a notification to tagged members, thereby getting more users.
Uploading an image from hard disk is straight forward. But what if I wish to upload an image from some other website URL and later tag them.
Demo Download
We are going to use facebook php sdk. Once the initialization are done and if you have a valid user object, place the following code:
$facebook->setFileUploadSupport(true);
$args = array('message' => 'by http://www.lookmywebpage.com/api');
copy('http://demo.lookmywebpage.com/facebook-upload-photos/penguin.png', 'tmp/file.jpeg');
$args['image'] = '@' . realpath('tmp/file.jpeg');
$data = $facebook->api('/me/photos', 'post', $args);
unlink('tmp/file.jpeg');
//assigning users to tag and cordinates
$argstag = array('to' => $user);
$argstag['x'] = 40;
$argstag['y'] = 40;
$datatag = $facebook->api('/' . $data['id'] . '/tags', 'post', $argstag);
echo 'Success! Check your facebook wall now';
Here first line enables facebook to support uploads. ‘message’ and ‘image’ parameters of $args are representing photo caption and photo path respectively. Copy() function copies the image to our temporary folder tmp with file name ‘file.jpeg’. After uploading to wall using api() function, the temporary file is deleted using php unlink() function. $argstag prepares the attributes for tagging the image. Information about image is passed with $data[‘id’] variable. After uploading the image facebook returns the id of uploaded image in json format and we are storing it in $data.
$facebook->setFileUploadSupport(true);
$args = array('message' => 'by http://www.lookmywebpage.com/api');
copy('http://demo.lookmywebpage.com/facebook-upload-photos/penguin.png', 'tmp/file.jpeg');
$args['image'] = '@' . realpath('tmp/file.jpeg');
$data = $facebook->api('/me/photos', 'post', $args);
unlink('tmp/file.jpeg');
//assigning users to tag and cordinates
$argstag = array('to' => $user);
$argstag['x'] = 40;
$argstag['y'] = 40;
$datatag = $facebook->api('/' . $data['id'] . '/tags', 'post', $argstag);
echo 'Success! Check your facebook wall now';
0 comments: