使用 facebook API 上传图片 [英] Upload image with facebook API

查看:33
本文介绍了使用 facebook API 上传图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:如何通过 FB API 将图像从我的网络服务器上传到 Facebook?

Question: How to upload an image from my webserver to facebook via FB API?

我正在编写一个应用程序,它从用户的相册中检索图像,进行一些修改(例如添加水印),然后将其发送回相册.

I'm writing an application that retrieves images from the user's photo album, makes some modifications (e.g. adding a watermark) then send it back to photo album.

我用来上传照片的代码如下

The code I use to upload the photo is as follows

<?php
include_once("api/facebook.php");
include_once("config.php");
include_once("utils.php");
include_once("bemyfans.php");
$facebook=new Facebook($api_key,$app_secret);
$facebook->require_frame();
$user=$facebook->require_login();
echo "<p>Hello <fb:name useyou='false' uid=\"$user\"/></p>";
$args = array(
  'api_key' => $api_key,
  'call_id'=>microtime(true),
  'v'=>'1.0',
  'format' => 'JSON'
);
$args['Lenna.png']="@/home/thoai/htdocs/apps/bemyfans/Lenna.png";
signRequest($args,$secret);
$ch = curl_init();
$url = 'http://api.facebook.com/restserver.php?method=photos.upload';
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);
if ($data = curl_exec($ch)) echo "done";
echo $data;

function signRequest(&$args, $secret){
  ksort($args);
  $sig = '';
  foreach($args as $k => $v){
    $sig .= $k . '=' . $v;
  }
  $sig .= $secret;
  $args['sig'] = md5($sig);
}
?>

这根本行不通.更具体地说,我不断收到签名不正确"的消息.

It just doesn't work. More specifically, I keep getting an "Incorrect signature" message.

代码有什么问题???

推荐答案

此问题中的代码使用了过时的 REST API,即将停止使用.

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

现在正确的方法是:

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

记住$fbk->setFileUploadSupport(true);

这篇关于使用 facebook API 上传图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆