离线上传照片到Facebook与照片从web服务器

问题描述:

我没有运气搜索到世界各地,发现很多例子,当用户在会话中(即用户实际坐在计算机并访问网页)时,将照片上传到Facebook。我已经尝试了样本,他们工作。

I've searched 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 * Question

我目前的应用程序允许用户授权离线更新,并存储access_token,user_id等,并且我可以在离线时成功地发布到用户墙。

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.

我真的很难找到工作,向用户墙发布照片。阅读 Facebook文档,我认为您只能使用multipart / form-data!?!?

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?

这是我的代码到目前为止。记住,这并不使用Facebook会话,因为access_code已经被预先授予和存储。正如我所说,发布到用户墙已经使用这种方法。

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);

编辑:$ result返回false ...忘记添加。

$result comes back false... forgot to add that.

任何帮助将不胜感激。
非常感谢,
Dean

Any help would be appreciated. Many thanks, Dean

您应该使用Facebook SDK。使用Facebook SDK,您可以使用:

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 );

这将把照片发布到他们的默认相册。如果您将user_id替换为album_id,您可以发布到特定的相册。

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.