使用网络服务器上的照片离线将照片上传到 Facebook

发布于 2024-10-29 23:41:24 字数 1428 浏览 0 评论 0原文

我到处搜索,但没有运气,找到了很多在用户处于会话中时将照片上传到 Facebook 的示例(即用户实际坐在计算机前并访问网页)。我已经尝试过这些示例,并且它们有效。

我注意到去年关于同一问题的这个未回答的问题 Stackoverflow 问题

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

我真的很难找到将照片发布到用户墙上的方法。阅读 Facebook 文档,我认为您只能使用以下方式上传照片多部分/表单数据!?!?

如果用户不在他们的计算机前,这将不起作用。您可以上传存储在我的服务器目录中的照片吗?

到目前为止,这是我的代码。请记住,这不使用 facebook 会话,因为 access_code 已预先授予并存储。正如我所提到的,在用户墙上发布信息已经可以使用这种方法。

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

编辑:$结果返回错误...忘记添加。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

地狱即天堂 2024-11-05 23:41:24

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

$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,则可以发布到特定相册。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文