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

时间:2022-11-29
本文介绍了Facebook:通过 php 将图像和描述发布到墙和页面相册中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

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.

我为此创建了一个应用程序,但我们似乎无法找到让应用程序在此 facebook 页面上发布的方法.

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?

推荐答案

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

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

1.) 创建一个 facebook 应用程序(通常的方式),确保你指定了 Canvas URL

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

2.) 导航到以下 url 以页面管理员身份登录,并授予权限 (user_photos,manage_pages,offline_access,publish_stream)

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.) 当您授予应用程序所需的权限时,您将被重定向到 canvas_url#access_token=*access_token*,例如

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.) 然后导航到

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

(使用 #3 中的访问令牌).这将列出您管理的页面;记下要上传图片的页面的 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

我不是 100% 确定,但我相信使用图形 API 您只能将图像上传到通过图形 API 创建的相册;即您需要首先通过图形 api 创建一个专辑.下面是使用 curl 的示例代码:

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

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

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 将图像和描述发布到墙和页面相册中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:如何以编程方式“按下"通过 Facebook 应用程序的“喜欢"按钮? 下一篇:必须使用主动访问令牌来查询有关当前 user-Graph api 异常的信息

相关文章

最新文章