写真をアプリ独自のファンページにアップロードし、ファンページのタイムラインに大きく表示するPHPスクリプトを作成しようとしています。https://www.facebook.com/Instagramに似ています。
これは私が持っているコードですが、OpenGraph URLでpage_idを使用していますが、Pageアルバムではなく、個人の写真アルバムに投稿し続けます。
<?php
$app_id = "APP_ID";
$app_secret = "APP_SECRET";
$post_login_url = "LOGIN_URL";
$fan_page_id = "PAGE_ID";
$code = $_REQUEST["code"];
//Obtain the access_token with publish_stream,manage_pages permission
if(empty($code)){
$dialog_url= "http://www.facebook.com/dialog/oauth?"
. "client_id=" . $app_id
. "&redirect_uri=" . urlencode( $post_login_url)
. "&scope=publish_stream,manage_pages";
echo("<script>top.location.href='" . $dialog_url . "'</script>");
}
else {
$token_url="https://graph.facebook.com/oauth/access_token?"
. "client_id=" . $app_id
. "&redirect_uri=" . urlencode( $post_login_url)
. "&client_secret=" . $app_secret
. "&code=" . $code;
$response = file_get_contents($token_url);
$params = null;
parse_str($response, $params);
$access_token = $params['access_token'];
// Show photo upload form to user and post to the Graph URL
$graph_url= "https://graph.facebook.com/".$fan_page_id."/photos?access_token=" .$access_token;
echo '<html><body>';
echo '<form enctype="multipart/form-data" action="' .$graph_url .' "method="POST">';
echo 'Please choose a photo: ';
echo '<input name="source" type="file"><br/><br/>';
echo '<input type="submit" value="Upload"/><br/>';
echo '</form>';
echo '</body></html>';
}
?>
CURLから何も返さないコードサンプルv2
$fb_page_id = "fb_page_id";
$user_id = 'user_id';
$fb_access_token = 'fb_access_token';
$args = array(
'url' => 'http://www.mysite.com/savefiles/smalls/comps/1/comp_1_600.jpg',
'message' => 'SOME CAPTION TEXT'
);
//Get Page Access Token - (as opposed to your user access token - requires manage_pages permission)
$accounts = 'https://graph.facebook.com/'.$user_id.'/accounts?access_token='.$fb_access_token;
$accounts_data = file_get_contents($accounts,0,null,null);
$account_data = json_decode($accounts_data, true);
foreach ($account_data['data'] as $accountdata) {
$account_page_id = $accountdata['id'];
$page_access_token = $accountdata['access_token'];
if($account_page_id == $fb_page_id){
$my_page_access_token = $page_access_token;
//echo $my_page_access_token . '<br/>';
}
}
//Not Tested - Just in Case
if($my_page_access_token==''){
$my_page_access_token = $fb_access_token;
}
//Make Upload Call
$ch = curl_init();
$url = 'https://graph.facebook.com/'.$fb_page_id.'/photos?access_token='.$my_page_access_token;
// echo $url . '<br/>';
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);
$post_object = json_decode($data);
$post_data = get_object_vars($post_object);
$post_id = $post_data['post_id'];
curl_close($ch);