我开始学习API以查看可用的内容。
有没有办法让用户上传图片并将它们放在脸书和网站上?
例如:如果我创建一个观鸟网站www.birdwatch.com,人们有没有办法发布图片,以便它们同时出现在他们的个人资料和网站上?
当人们对图片发表评论时,它会出现在网站和Facebook上吗?
答案 0 :(得分:1)
是的,但它相当复杂。你需要获得一个FB开发人员令牌,编写一些HTML和PHP的东西等等。
以下是一些PHP代码,我用它来获取已经上传到我的服务器的文件,然后将其中的5个投放到Facebook上,然后是第6张照片,该图片是“单击相册说明中的链接以查看更多”。
以下代码非常来自Botchit& Scarper Web Dev大学,但它应该强调如何让事情顺利进行:)
[代码]
// Facebook Shite
$fb_app_id = 'xxxxxxxxxxxxxxxxxxxxxxxxxx';
$fb_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$fb_app_url = 'http://apps.facebook.com/your-fb-page/canvas-name-here';
require './facebook.php';
//Create facebook application instance.
$facebook = new Facebook(array(
'appId' => $fb_app_id,
'secret' => $fb_secret
));
/////////////////////////////////////////////// ////
$facebookalbumname = stripslashes($_POST['facebookalbumname']);
$facebookalbumurl = stripslashes($_POST['facebookalbumurl']);
$facebookphoto1 = stripslashes($_POST['facebookphoto1']);
$facebookphoto2 = stripslashes($_POST['facebookphoto2']);
$facebookphoto3 = stripslashes($_POST['facebookphoto3']);
$facebookphoto4 = stripslashes($_POST['facebookphoto4']);
$facebookphoto5 = stripslashes($_POST['facebookphoto5']);
/////////////////////////////////////////////// ////
//At the time of writing it is necessary to enable upload support in the Facebook SDK, you do this with the line:
$facebook->setFileUploadSupport(true);
//Create an album
$album_details = array(
'access_token'=> 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
'message'=> "See ALL the photos in this gallery at $facebookalbumurl ." ,
'name'=> $facebookalbumname
);
$create_album = $facebook->api('/me/albums', 'post', $album_details);
//Get album ID of the album you've just created
$album_uid = $create_album['id'];
///////////////////////
$current = file_get_contents($facebookphoto1); file_put_contents("facebookphoto1.jpg", $current);
$current = file_get_contents($facebookphoto2); file_put_contents("facebookphoto2.jpg", $current);
$current = file_get_contents($facebookphoto3); file_put_contents("facebookphoto3.jpg", $current);
$current = file_get_contents($facebookphoto4); file_put_contents("facebookphoto4.jpg", $current);
$current = file_get_contents($facebookphoto5); file_put_contents("facebookphoto5.jpg", $current);
///
//Upload a photo to album of ID...
$photo_details = array(
'access_token'=> 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
'message'=> "See ALL the photos in this gallery at $facebookalbumurl 1"
);
$photo_details['image'] = '@' . realpath('facebookphoto1.jpg');
$upload_photo = $facebook->api('/'.$album_uid.'/photos', 'post', $photo_details);
///
//Upload a photo to album of ID...
$photo_details = array(
'access_token'=> 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
'message'=> "See ALL the photos in this gallery at $facebookalbumurl 2"
);
$photo_details['image'] = '@' . realpath('facebookphoto2.jpg');
$upload_photo = $facebook->api('/'.$album_uid.'/photos', 'post', $photo_details);
///
//Upload a photo to album of ID...
$photo_details = array(
'access_token'=> 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
'message'=> "See ALL the photos in this gallery at $facebookalbumurl 3"
);
$photo_details['image'] = '@' . realpath('facebookphoto3.jpg');
$upload_photo = $facebook->api('/'.$album_uid.'/photos', 'post', $photo_details);
///
//Upload a photo to album of ID...
$photo_details = array(
'access_token'=> 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
'message'=> "See ALL the photos in this gallery at $facebookalbumurl 4"
);
$photo_details['image'] = '@' . realpath('facebookphoto4.jpg');
$upload_photo = $facebook->api('/'.$album_uid.'/photos', 'post', $photo_details);
///
//Upload a photo to album of ID...
$photo_details = array(
'access_token'=> 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
'message'=> "See ALL the photos in this gallery at $facebookalbumurl 5"
);
$photo_details['image'] = '@' . realpath('facebookphoto5.jpg');
$upload_photo = $facebook->api('/'.$album_uid.'/photos', 'post', $photo_details);
///
//Upload a photo to album of ID...
$photo_details = array(
'access_token'=> 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
'message'=> "See ALL the photos in this gallery at $facebookalbumurl 6"
);
$photo_details['image'] = '@' . realpath('seeallthephotos.jpg');
$upload_photo = $facebook->api('/'.$album_uid.'/photos', 'post', $photo_details);
&GT?; [/代码]