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

Facebook: post image and description to wall and in page album via php(Facebook:通过 php 将图像和描述发布到墙和页面相册中)
本文介绍了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 将图像和描述发布到墙和页面相册中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)