I'm trying to familiarize myself with Facebook's new Graph API and so far I can fetch and write some data pretty easily.
Something I'm struggling to find 开发者_StackOverflowdecent documentation on is uploading images to an album.
According to http://developers.facebook.com/docs/api#publishing you need to supply the message argument. But I'm not quite sure how to construct it.
Older resources I've read are:
http://wiki.auzigog.com/Facebook_Photo_Uploads
https://developers.facebook.com/docs/reference/rest/photos.upload/
If someone has more information or could help me tackle uploading photos to an album using Facebook Graph API please reply!
Here are some various ways to upload photos using the PHP Facebook Graph API. The examples assume you've instantiated the $facebook object and have a valid session.
1 - Upload to 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 - Upload to 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);
Here is the code that worked for me:
//upload photo
$file= '/path/filename.jpg';
$args = array(
'message' => 'Photo from application',
);
$args[basename($file)] = '@' . realpath($file);
$ch = curl_init();
$url = 'http://graph.facebook.com/'.$album_id.'/photos?access_token='.$access_token;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
$data = curl_exec($ch);
//returns the photo id
print_r(json_decode($data,true));
Link to documentation: http://developers.facebook.com/docs/reference/api/photo
You have to do a few things to get the graph api to work with php. This code works. Notice the fileUpload => true...
I also was never able to get it to work with javascript so I'm doing ajax to this:
require './facebook.php';
$facebook = new Facebook(array(
'appId' => 'ID',
'secret' => 'SECRET',
'fileUpload' => true,
'cookie' => true // enable optional cookie support
));
$facebook->setFileUploadSupport(true);
# File is relative to the PHP doc
$file = "@".realpath("../../_images/stuff/greatness.jpg");
$args = array(
'message' => 'Photo Caption',
"access_token" => "urtoken",
"image" => $file
);
$data = $facebook->api('/ALBUMID_GOES_HERE/photos', 'post', $args);
if ($data) print_r("success");
精彩评论