douan0729 2011-04-02 10:40
浏览 64

离线上传照片到Facebook与Web服务器的照片

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

  • 写回答

1条回答 默认 最新

  • dongxiezhi9564 2011-04-17 15:59
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大