Facebook Graph API PHP SDK v4 - 发布在页面上 [英] Facebook Graph API PHP SDK v4 - Post on Page
问题描述
所以我创建了一个简单的例子,我想让所有的网站作者可能发布他们的文章在我们的Facebook页面上,而不用给他们管理员访问。表单,作者输入的地址:URL,图像的URL,消息
在提交时,此表单将向facebook.php发送一个ajax请求,其中应该
第一个问题发生在require_once。
不可能要求所有4个文件没有错误。
如果我摆脱了facebook异常,那么除了请求本身之外,一切都起作用。
似乎有一个PHP错误,因为我根本没有ajax响应。
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');
使用Facebook\FacebookSession;
使用Facebook\FacebookRequest;
使用Facebook\GraphObject;
使用Facebook\FacebookRequestException;
$ message = safe($ _ POST [message]);
$ url = safe($ _ POST [url]);
$ image = safe($ _ POST [image]);
if($ message ==OR $ url ==OR $ image ==){
echoincomplete;
返回;
}
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发表于id:。 $响应 - >的getProperty( ID);
} catch(FacebookRequestException $ e){
echo异常发生,代码:。 $ E->引用代码();
echowith message:。 $ E->的getMessage();
}
} else {
echoNo Session available!;
}
更新: / strong> 2014年6月27日,SDK现在附带了一个内置的自动加载器为那些不能使用作曲家的人。
require __DIR__。 /path/to/facebook-php-sdk-v4/autoload.php;
如果没有为您找到路径,您可以使用 FACEBOOK_SDK_V4_SRC_DIR
。
define('FACEBOOK_SDK_V4_SRC_DIR','/ path / to / facebook-php -sdk-V4 / SRC /实/');
需要__DIR__。 /path/to/facebook-php-sdk-v4/autoload.php;
SDK的内部部件依赖于其他几个类你不包括。这就是为什么自动加载在这里非常重要。
使用Composer自动加载
最好的方式是< a href =https://getcomposer.org/ =nofollow noreferrer>安装作曲者。并将SDK添加到 composer.json
文件到您项目的根目录。
{
require:{
facebook / php-sdk-v4:4.0。*
}
}
然后从命令行运行 composer install
> composer.json 文件是。然后将自动加载器包含在脚本的顶部。
require_once __DIR__。 /vendor/autoload.php;
手动自动加载
自动加载这些文件是使用的这个解决方案来替换您的 require_once
的顶部rm-vanda :
function facebookLoader($ class){
require/ path / to / facebook-php-sdk-v4-master / src /。 str_replace(\\,/,$ class)。 .PHP;
}
spl_autoload_register(facebookLoader);
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.
So i created a simple form, where the author types in: URL, URL to image, message
On submit, this form will send a ajax request to facebook.php where the magic "should" happen.
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 Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\GraphObject;
use Facebook\FacebookRequestException;
$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!";
}
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';
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';
The internals of the SDK rely on several other classes that you're not including. That's why autoloading is really important here.
Autoloading With Composer
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.*"
}
}
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';
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屋!