問(wèn)題描述
我想讓我所有的網(wǎng)站作者有機(jī)會(huì)在我們的 facebook 頁(yè)面上發(fā)布他們的文章,而無(wú)需授予他們管理員訪問(wèn)權(quán)限.
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.
所以我創(chuàng)建了一個(gè)簡(jiǎn)單的表單,作者在其中輸入:URL、圖像的 URL、消息
So i created a simple form, where the author types in: URL, URL to image, message
提交時(shí),此表單將向 facebook.php 發(fā)送一個(gè) ajax 請(qǐng)求,其中應(yīng)該"發(fā)生魔法.
On submit, this form will send a ajax request to facebook.php where the magic "should" happen.
第一個(gè)問(wèn)題發(fā)生在require_once".不可能在沒(méi)有錯(cuò)誤的情況下要求所有 4 個(gè)文件.如果我擺脫了 facebook 異常,那么除了請(qǐng)求本身之外,一切都正常.似乎有一個(gè) PHP 錯(cuò)誤,因?yàn)槲腋緵](méi)有收到 ajax 響應(yīng).
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 現(xiàn)在帶有 內(nèi)置自動(dòng)加載器,適用于無(wú)法使用 composer 的用戶(hù).
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';
如果這沒(méi)有自動(dòng)為您找到路徑,您可以使用 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';
<小時(shí)>
SDK 的內(nèi)部結(jié)構(gòu)依賴(lài)于您未包括的其他幾個(gè)類(lèi).這就是自動(dòng)加載在這里非常重要的原因.
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 添加到項(xiàng)目的根目錄.
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
文件所在的命令行運(yùn)行 composer install
.然后在腳本頂部包含自動(dòng)加載器.
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';
手動(dòng)自動(dòng)加載
自動(dòng)加載這些文件的另一種方法是使用 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");
這篇關(guān)于Facebook Graph API PHP SDK v4 - 在頁(yè)面上發(fā)布的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!