如何使用Facebook Graph API将图像上传到Apps自己的页面?

时间:2013-02-21 16:59:33

标签: facebook facebook-graph-api

我正在尝试创建一个PHP脚本,将照片上传到应用程序自己的粉丝页面,并使它们在粉丝页面时间轴中显得很大。与https://www.facebook.com/Instagram相似。

这是我的代码,但是虽然它在OpenGraph网址中使用了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>';
    }
?>

代码示例v2,它不会从CURL返回任何内容

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

1 个答案:

答案 0 :(得分:1)

您应该能够将照片上传到您的应用页面,就像您将其他任何页面一样。首先设置您的上传参数以传递给Facebook:

$args = array(
    'url' => 'URL-OF-IMAGE' ,
    'message' => 'SOME CAPTION TEXT'
    );

在接下来的步骤中,请确保您已拥有用户ID,页面ID和有效的访问令牌:

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

这会将您的照片上传到您网页上与您的应用名称相同的相册。如果此相册尚不存在,则会在首次上传时自动创建。此照片还会显示在您的时间轴上,带有您的标题。只要您具有适当的身份验证,可变数据(如页面ID,用户ID和访问令牌)以及您的参数,上面的大多数代码都会经过测试并按原样运行。