Offline upload photo to Facebook with photo from web server
I've searche开发者_JS百科d around everywhere without luck and found lots of examples to upload a photo to facebook when the user is in a session (i.e. the user is physically sitting at the computer and accessing the web page). I've tried the samples, and they work.
I noticed this unanswered question from last year on the same issue Stackoverflow Question
My current app lets the user authorise off-line updates and I store the access_token, user_id, etc. and I can succesfully post to a users wall when they are offline.
I'm really struggling getting something to work with posting a photo to the users wall. Reading the Facebook documentation, I'm thinking you can only upload photos using multipart/form-data!?!?
That wouldn't work if the user isn't at their computer. Can you upload photos that are stored on a directory on my server?
Here's my code so far. Remember, this doesn't use a facebook session as the access_code has already been granted and stored beforehand. As I mentioned, posting to a users wall already works with this approach.
$filename= "@/myphotodir/filename.jpg");
$url = "https://graph.facebook.com/".$uid."/photos"; //$uid is fb user id
$ch = curl_init($url);
$attachment = array('access_token' => $access_token,
'app_id' => $app_id,
'name' => "A photo from me...",
'fileUpload' => true,
'message' => "my message",
'image' => $filename,
);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result= curl_exec($ch);
curl_close ($ch);
Edit: $result comes back false... forgot to add that.
Any help would be appreciated. Many thanks, Dean
You should use the Facebook SDK. With the Facebook SDK, you can use:
$facebook->setFileUploadSupport( true );
$parameters = array(
'access_token' => 'ACCESS_TOKEN_HERE',
'message' => 'PHOTO_CAPTION',
'image' => '@' . realpath( '/path_to_file.jpg' ) // Notice the @ sign
);
$facebook->api( '/user_id/photos', 'post', $parameters );
This will post the photo to their default album. If you replace user_id with an album_id you can post to a specific album.
精彩评论