dsgnze6572 2012-02-22 13:06
浏览 22
已采纳

Facebook:通过php将图像和描述发布到墙上和页面相册中

I want users to post an image on a facebook page via a form on a website. When they have logged in via facebook on this website, they can select an image from their computer.

Once they have selected the image, I want it to be posted to the users wall, and in an album of the page where I'm one of the administrators.

I have created an app for this, but we can't seem to find a way to get the app to post on this facebook-page.

Do we need to set any permissions on this page or app?

  • 写回答

1条回答 默认 最新

  • dongtan8532 2012-02-22 13:39
    关注

    To upload images to a facebook page of which you're an admin you need to do the following:

    1.) Create a facebook application (the usual way), make sure you specify the Canvas URL

    2.) Navigate to the url below logged in as the admin of the page, and give the permissions (user_photos,manage_pages,offline_access,publish_stream)

    https://www.facebook.com/dialog/oauth?
        client_id=<application_id>
        &redirect_uri=<canvas_url>
        &response_type=token
        &scope=user_photos,manage_pages,offline_access,publish_stream
    

    3.) When you give the application the required permissions you'll be redirected to canvas_url#access_token=*access_token*, for example

    http://example.com/#access_token=awe12
    

    4.) Then navigate to

    https://graph.facebook.com/me/accounts?access_token=<access_token>
    

    (use the access token from #3). This will list the pages you administer; write down the access_token for the page(s) to which you want to upload the image

    I'm not 100% sure but I believe that using graph api you can upload images only to albums created via graph api; i.e. you need to first create an album via graph api. Here's sample code using curl:

    $uri = sprintf( 
        'https://graph.facebook.com/%1$s/albums?access_token=%2$s',
        $page_id, 
        $access_token
    );
    
    $post_fields = array(
        'name' => trim( $album_name )
    );
    
    $curl = curl_init( $uri );
    curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1 );  
    curl_setopt( $curl, CURLOPT_POST, TRUE );
    curl_setopt( $curl, CURLOPT_POSTFIELDS, $post_fields );  
    
    $raw_data = curl_exec( $curl );
    curl_close( $curl );
    
    $data = json_decode( $raw_data, $assoc = TRUE );
    

    The $data above will contain the album id, which you'll need to upload a photo:

    // prepare the curl post fields
    $batch = sprintf(
        '[{"method":"POST", "relative_url":"%1$s/photos", "attached_files":"file1"}]',
        $album_id
    );  
    
    $post_fields = array(
        'batch' => $batch,
        'access_token' => $access_token,
        'file1' => '@' . $image_abs_path
    );
    $uri = 'https://graph.facebook.com';
    
    $curl = curl_init( $uri );
    curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1 );  
    curl_setopt( $curl, CURLOPT_POST, TRUE );
    curl_setopt( $curl, CURLOPT_POSTFIELDS, $post_fields );  
    
    $raw_data = curl_exec( $curl );
    curl_close( $curl );
    
    $data = json_decode( $raw_data, $assoc = TRUE );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么