Upload image with facebook API

徘徊边缘 提交于 2019-11-29 08:52:43

The code in this question use the outdated REST APIs, that will soon be discontinued.

The correct way now is:

$fbk = new Facebook(/* conf */);
$fbk->setFileUploadSupport(true);

//If you are executing this in a script, and not in a web page with the user logged in:
$fbk->setAccessToken(/* access token from other sources */);

//To add to an album:
$fbk->api("/$albumId/photos", "POST", 
          array('source' => '@'. realpath($myPhoto), 'message'  => "Nice photo"));

//To upload a photo directly (the album will be created automatically):
$fbk->api("/me/photos", "POST", 
          array('source' => '@'. realpath($myPhoto), 'message'  => "Nice photo"));

Remember the $fbk->setFileUploadSupport(true);

Judging from the error, your request is not being signed properly.

I took a quick look at the facebook API docs, and your signRequest() function looks about right to me.

So I'll suggest you check the obvious: that your $secret is correct.

EDIT: Your answer to the questions in comments make me see it now. I don't know how the facebook servers deal with binary data and signatures, but your signature is based on that @-prefixed pathname. Facebook never sees that pathname, since cURL sends the file contents. So there's no way that facebook can reconstruct the signature and verify it.

Check the facebook docs for the call you're making very carefully. You might need to omit the file from your signature-generation, or base64 encode it, or who knows what.

But it's clear that your code is sending a signature that facebook cannot verify, since it never sees anything remotely resembling "@/home/thoai/htdocs/apps/bemyfans/Lenna.png" in your request.

You are signing your requesting using all the args including the file argument. To get a correct signature you need to sign request without the file argument in the args.

I think you are doing it all wrong Look at my code

$appapikey = 'keykeykey';
$appsecret = 'secretcesret';
$facebook = new Facebook($appapikey, $appsecret);
$user=$facebook->require_login()

$facebook->api_client->photos_upload($file, null, "A test Photo", $user);

This works for me

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!