Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
754 views
in Technique[技术] by (71.8m points)

php - Upload image with facebook API

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.

What's wrong with the code???

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...