1

我正在尝试创建一个 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);
4

1 回答 1

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 和访问令牌)以及您的参数,上面的大部分代码都经过测试并且可以正常工作。

于 2013-02-21T19:42:20.670 回答