Facebook:通过php将图像和描述贴到墙壁和页面相册中 [英] Facebook: post image and description to wall and in page album via php

查看:85
本文介绍了Facebook:通过php将图像和描述贴到墙壁和页面相册中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望用户通过网站上的表单在Facebook页面上发布图像。
当他们通过本网站上的Facebook登录时,他们可以从他们的电脑中选择一个图像。



一旦他们选择了图像,我希望它被发布到用户墙,以及我在其中一个管理员页面的相册中。



我已经创建了一个应用程序,但我们可以' t / p>

我们需要在此页面或应用上设置任何权限吗?

解决方案

要将图像上传到您是管理员的Facebook页面,您需要执行以下操作:



1。)创建一个Facebook应用程序(通常的方式),确保指定Canvas URL



2。)导航到下面的URL以页面的管理员身份登录,并授予权限(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。)当您向应用程序发送所需的权限时,您将被重定向到canvas_url#access_token = * access_token *,例如

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

4。)然后导航到

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

(使用#3中的访问令牌)。这将列出您管理的页面;写下您要上传图片的页面的access_token



我不是100%肯定但我相信使用图形API可以上传图片只能通过图形API创建的相册;即您需要先通过图形API创建一个相册。以下是使用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);

上面的 $ data 将包含相册id,您需要上传照片:

  //准备卷曲帖子字段
$ 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);


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?

解决方案

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

这篇关于Facebook:通过php将图像和描述贴到墙壁和页面相册中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆