我正在尝试创建一个 PHP 脚本来将照片上传到应用程序自己的粉丝页面,并让它们在粉丝页面时间轴中显得很大。类似于https://www.facebook.com/Instagram。
这是我拥有的代码,但尽管它在 OpenGraph URL 中使用 page_id,但它会一直发布到我的个人相册,而不是页面相册。
<?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);