php 使用 Facebook 的 Graph API 将照片上传到相册

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2718610/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:28:21  来源:igfitidea点击:

Upload Photo To Album with Facebook's Graph API

phpfacebook

提问by st4ck0v3rfl0w

I'm trying to familiarize myself with Facebook's new Graph API and so far I can fetch and write some data pretty easily.

我正在尝试熟悉 Facebook 的新 Graph API,到目前为止我可以很容易地获取和写入一些数据。

Something I'm struggling to find decent documentation on is uploading images to an album.

我正在努力寻找体面的文档是将图像上传到相册。

According to http://developers.facebook.com/docs/api#publishingyou need to supply the messageargument. But I'm not quite sure how to construct it.

根据http://developers.facebook.com/docs/api#publishing,您需要提供message参数。但我不太确定如何构建它。

Older resources I've read are:

我读过的旧资源是:

If someone has more information or could help me tackle uploading photos to an album using Facebook Graph API please reply!

如果有人有更多信息或可以帮助我解决使用 Facebook Graph API 将照片上传到相册的问题,请回复!

回答by Brody Robertson

Here are some various ways to upload photos using the PHP Facebook Graph API. The examples assume you've instantiated the $facebook object and have a valid session.

以下是使用 PHP Facebook Graph API 上传照片的多种方法。这些示例假设您已经实例化了 $facebook 对象并且有一个有效的会话。

1 - Upload to Default Application Album of Current User

1 - 上传到当前用户的默认应用相册

This example will upload the photo to your default application album of the current user. If the album does not yet exist it will be created.

此示例将照片上传到当前用户的默认应用程序相册。如果相册尚不存在,则会创建它。

$facebook->setFileUploadSupport(true);
$args = array('message' => 'Photo Caption');
$args['image'] = '@' . realpath($FILE_PATH);

$data = $facebook->api('/me/photos', 'post', $args);
print_r($data);

2 - Upload to Target Album

2 - 上传到目标专辑

This example will upload the photo to a specific album.

此示例将照片上传到特定相册。

$facebook->setFileUploadSupport(true);
$args = array('message' => 'Photo Caption');
$args['image'] = '@' . realpath($FILE_PATH);

$data = $facebook->api('/'. $ALBUM_ID . '/photos', 'post', $args);
print_r($data);

回答by AhDang

Here is the code that worked for me:

这是对我有用的代码:

//upload photo
$file= '/path/filename.jpg';
$args = array(
   'message' => 'Photo from application',
);
$args[basename($file)] = '@' . realpath($file);
$ch = curl_init();
$url = 'http://graph.facebook.com/'.$album_id.'/photos?access_token='.$access_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);
//returns the photo id
print_r(json_decode($data,true));

Link to documentation: http://developers.facebook.com/docs/reference/api/photo

文档链接:http: //developers.facebook.com/docs/reference/api/photo

回答by Alex Grande

You have to do a few things to get the graph api to work with php. This code works. Notice the fileUpload => true...

您必须做一些事情才能使图形 api 与 php 一起使用。此代码有效。注意 fileUpload => true ...

I also was never able to get it to work with javascript so I'm doing ajax to this:

我也永远无法让它与 javascript 一起工作,所以我正在为此做 ajax:

require './facebook.php';

require './facebook.php';

$facebook = new Facebook(array(  
  'appId'  => 'ID',  
  'secret' => 'SECRET',  
  'fileUpload' => true,  
  'cookie' => true // enable optional cookie support  
));  


$facebook->setFileUploadSupport(true);  

# File is relative to the PHP doc  
$file = "@".realpath("../../_images/stuff/greatness.jpg");  

$args = array(  
    'message' => 'Photo Caption',  
    "access_token" => "urtoken",  
    "image" => $file  
);  


$data = $facebook->api('/ALBUMID_GOES_HERE/photos', 'post', $args);
if ($data) print_r("success");
$facebook = new Facebook(array(  
  'appId'  => 'ID',  
  'secret' => 'SECRET',  
  'fileUpload' => true,  
  'cookie' => true // enable optional cookie support  
));  


$facebook->setFileUploadSupport(true);  

# File is relative to the PHP doc  
$file = "@".realpath("../../_images/stuff/greatness.jpg");  

$args = array(  
    'message' => 'Photo Caption',  
    "access_token" => "urtoken",  
    "image" => $file  
);  


$data = $facebook->api('/ALBUMID_GOES_HERE/photos', 'post', $args);
if ($data) print_r("success");