画像をアップロードするための HTML フォーム
<form enctype="multipart/form-data" action="upload.php" method="POST">
Please choose a photo:
<input name="photo" type="file"><br/><br/>
Say something about this photo:
<input name="message" type="text" value=""><br/><br/>
<input type="submit" value="Upload"/><br/>
</form>
HTMLフォームはupload.phpにリダイレクトされます
upload.php ファイルのコード ..
<?php
//upload.php
if(isset($_FILES['photo']) && isset($_POST['message'])){
$uploadfile ='./uploads/'.basename($_FILES['photo']['name']);
$iStats=getimagesize($_FILES['photo']['tmp_name']);
if (isset($iStats['mime']) && $iStats[0]>0) {
move_uploaded_file($_FILES['photo']['tmp_name'], $uploadfile);
require_once('sdk/src/facebook.php');
require_once('utils.php');
require_once('AppInfo.php');
try{
//this one is used to find the APPID and secret of the app
$facebook = new Facebook(array(
'appId' => AppInfo::appID(),
'secret' => AppInfo::appSecret(),
'sharedSession' => true,
'trustForwarded' => true,
));
$album_details = array(
'message'=> 'Album desc',
'name'=> 'Album name'
);
$uid = $facebook->getUser(); //used to get the user id
$me = $facebook->api('/me');
$token = $facebook->getAccessToken(); //here I get the token from the $session array
$create_album = $facebook->api('/me/albums', 'post', $album_details);
$album_id = $create_album['id'];
$facebook->setFileUploadSupport(true);
$args['image'] = '@' . realpath($uploadfile);
$data = $facebook->api('/'. $album_id . '/photos?access_token='. $token, 'post', $args['image']);
// $data = $facebook->api('/'.$album_id.'/photos', 'post', $args['image']);
} catch(FacebookApiException $e){
echo "Error:" .$e;
}
unlink($uploadfile);
echo "Success!\n";
} else {
echo "Wrong file type!\n";
}
}
?>
次のエラーが表示されます
Error:OAuthException: (#324) アップロード ファイルが必要です