gpt4 book ai didi

php - 将图片上传到 Facebook

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:40:06 26 4
gpt4 key购买 nike

我正在尝试将图片上传到 Facebook 粉丝页面上的图库,这是我目前的代码,

$ch = curl_init();

$data = array('type' => 'client_cred', 'client_id' => 'app_id','client_secret'=>'secret_key',' redirect_uri'=>'http://apps.facebook.com/my-application/'); // your connect url here

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, 'https://graph.facebook.com/oauth/access_token');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

$rs = curl_exec($ch);
curl_close($ch);

$ch = curl_init();
$data = explode("=",$rs);
$token = $data[1];

$album_id = '1234';
$file= 'http://my.website.com/my-application/sketch.jpg';
$data = array(basename($file) => "@".realpath($file),
//filename, where $row['file_location'] is a file hosted on my server
"caption" => "test",
"aid" => $album_id, //valid aid, as demonstrated above
"access_token" => $token
);

$ch2 = curl_init();
$url = "https://graph.facebook.com/".$album_id."/photos";
curl_setopt($ch2, CURLOPT_URL, $url);
curl_setopt($ch2, CURLOPT_HEADER, false);
curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, false);
curl_setopt($ch2, CURLOPT_POST, true);
curl_setopt($ch2, CURLOPT_POSTFIELDS, $data);
$op = curl_exec($ch2);

当我回显 $token 时,我似乎得到了 token ,但是当我运行脚本时,我得到这个错误,{"error":{"type":"OAuthAccessTokenException","message":"An access请求此资源需要 token 。”},我不知道为什么要这样做!

基本上我在该代码中所做的是使用 curl 获取访问 token ,然后也通过 curl 将照片上传到我的相册,但我一直收到该错误!

如有任何帮助,我们将不胜感激,谢谢!

最佳答案

好的,开始工作了,这是代码,假设您有一个有效的 session 。

    $token = $session['access_token'];

//upload photo
$file= 'photo.jpg';
$args = array(
'message' => 'Photo from application',
);
$args[basename($file)] = '@' . realpath($file);

$ch = curl_init();
$url = 'https://graph.facebook.com/me/photos?access_token='.$token;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
$data = curl_exec($ch);

这会将指定的图像上传到 session 持有者图库,如果没有图像,它将创建一个相册,您也可以通过 session 数组访问 token ,如上所示。希望这可以帮助某人。

关于php - 将图片上传到 Facebook,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3019501/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com