Facebook Graph API PHP SDK v4 - 在页面上发布 [英] Facebook Graph API PHP SDK v4 - Post on Page

查看:19
本文介绍了Facebook Graph API PHP SDK v4 - 在页面上发布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让我所有的网站作者有机会在我们的 facebook 页面上发布他们的文章,而无需授予他们管理员访问权限.

I wanna give all my website authors a possiblity to post their articles on our facebook page without having go give them admin access to it.

所以我创建了一个简单的表单,作者在其中输入:URL、图像的 URL、消息

So i created a simple form, where the author types in: URL, URL to image, message

提交时,此表单将向 facebook.php 发送一个 ajax 请求,其中应该"发生魔法.

On submit, this form will send a ajax request to facebook.php where the magic "should" happen.

第一个问题发生在require_once".不可能在没有错误的情况下要求所有 4 个文件.如果我摆脱了 facebook 异常,那么除了请求本身之外,一切都正常.似乎有一个 PHP 错误,因为我根本没有收到 ajax 响应.

The first problem occurs at "require_once". It's not possible to require all 4 files without having an error. If i get rid of the facebook exception, then everything works except the request itself. There seems to be an PHP Error, because i get no ajax response at all.

session_start();

require_once($_SERVER['DOCUMENT_ROOT'].'/sys/facebook/FacebookSession.php');
require_once($_SERVER['DOCUMENT_ROOT'].'/sys/facebook/FacebookRequest.php');
require_once($_SERVER['DOCUMENT_ROOT'].'/sys/facebook/GraphObject.php');
require_once($_SERVER['DOCUMENT_ROOT'].'/sys/facebook/FacebookRequestException.php');

use FacebookFacebookSession;
use FacebookFacebookRequest;
use FacebookGraphObject;
use FacebookFacebookRequestException;

$message = safe($_POST["message"]);
$url = safe($_POST["url"]);
$image = safe($_POST["image"]);

if($message == "" OR $url == "" OR $image == ""){
    echo "incomplete";
    return;
}

FacebookSession::setDefaultApplication('{APP ID}','{APP SECRET}');
$session = new FacebookSession('{Page Access Token}');

if($session) {
    try {
        $response = (new FacebookRequest(
            $session, 'POST', '/{Page ID}/feed', array(
                'message'       => $message,
                'link'          => $url,
                'picture'       => $image
            )
        ))->execute()->getGraphObject();
        echo "Posted with id: " . $response->getProperty('id');
    } catch(FacebookRequestException $e) {
        echo "Exception occured, code: " . $e->getCode();
        echo " with message: " . $e->getMessage();
    }
} else {
    echo "No Session available!";
}

推荐答案

更新: 2014 年 6 月 27 日,SDK 现在带有 内置自动加载器,适用于无法使用 composer 的用户.

Update: June, 27 2014, The SDK now comes with a built-in autoloader for those who can't use composer.

require __DIR__ . '/path/to/facebook-php-sdk-v4/autoload.php';

如果这没有自动为您找到路径,您可以使用 FACEBOOK_SDK_V4_SRC_DIR 定义它.

If that doesn't automatically find the path for you, you can define it with FACEBOOK_SDK_V4_SRC_DIR.

define('FACEBOOK_SDK_V4_SRC_DIR', '/path/to/facebook-php-sdk-v4/src/Facebook/');
require __DIR__ . '/path/to/facebook-php-sdk-v4/autoload.php';

<小时>

SDK 的内部结构依赖于您未包括的其他几个类.这就是自动加载在这里非常重要的原因.


The internals of the SDK rely on several other classes that you're not including. That's why autoloading is really important here.

最好的方法是安装composer.并将 composer.json 文件中的 SDK 添加到项目的根目录.

The best way to do this is to install composer. And add the SDK in a composer.json file to the root of your project.

{
    "require" : {
        "facebook/php-sdk-v4" : "4.0.*"
    }
}

然后从 composer.json 文件所在的命令行运行 composer install.然后在脚本顶部包含自动加载器.

Then run composer install from the command line where the composer.json file is. Then include the autoloader at the top of your script.

require_once __DIR__ . '/vendor/autoload.php';

手动自动加载

自动加载这些文件的另一种方法是使用 require_once>rm-vanda:

Manual Autoloading

An alternative way to autoload these files is to replace your require_once's at the top with this solution from rm-vanda:

function facebookLoader($class) {
    require "/path/to/facebook-php-sdk-v4-master/src/" . str_replace("\", "/", $class) . ".php";
}

spl_autoload_register("facebookLoader");

这篇关于Facebook Graph API PHP SDK v4 - 在页面上发布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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