我想调用shareFB()
方法在我管理的 Facebook页面上自动分享内容。也许可以传递数据shareFB($link, $message)
目前获得Parse error: syntax error, unexpected 'use' (T_USE)
。
如何设置此功能?
public function shareFB() {
require_once( ''.dirname(__DIR__).'/includes/facebook-php-sdk-v4-4.0-dev/src/Facebook/FacebookRequest.php' );
use Facebook\FacebookRequest;
try {
$response = (new FacebookRequest(
$session, 'POST', '/me/feed', array(
'link' => 'https://www.youtube.com/watch?v=C2FETG7tCF0',
'message' => 'The Sun Full HD 1080p, Amazing Documentary'
)
))->execute()->getGraphObject();
echo "Posted with id: " . $response->getProperty('id');
} catch(FacebookRequestException $e) {
echo "Exception occured, code: " . $e->getCode();
echo " with message: " . $e->getMessage();
}
}
编辑***
require_once 'Db.class.php';
require_once( ''.dirname(__DIR__).'/includes/facebook-php-sdk-v4-4.0-dev/src/Facebook/FacebookRequest.php' );
use Facebook\FacebookRequest;
$app_id = 'xxx'; //Facebook App ID
$app_secret = 'xxx'; //Facebook App Secret
$redirect_url = 'http://example.com/com/login'; //FB redirects to this page with a code
//initialize Facebook
FacebookSession::setDefaultApplication($app_id, $app_secret);
$helper = new FAcebookRedirectLoginHelper($redirect_url);
try {
$session = $helper->getSessionFromRedirect();
} catch(FacebookRequestException $ex) {
// When Facebook returns an error
die(" Error : " . $ex->getMessage());
} catch(\Exception $ex) {
// When validation fails or other local issues
die(" Error : " . $ex->getMessage());
}
class HelperClass {
public function shareFB() {
try {
$response = (new FacebookRequest(
$session, 'POST', '/me/feed', array(
'link' => 'https://www.youtube.com/watch?v=C2FETG7tCF0',
'message' => 'The Sun Full HD 1080p, Amazing Documentary'
)
))->execute()->getGraphObject();
echo "Posted with id: " . $response->getProperty('id');
} catch(FacebookRequestException $e) {
echo "Exception occured, code: " . $e->getCode();
echo " with message: " . $e->getMessage();
}
}
}
答案 0 :(得分:1)
http://php.net/manual/en/language.namespaces.importing.php
use关键字必须在文件的最外层范围(全局范围)或命名空间内声明中声明。这是因为导入是在编译时完成的,而不是运行时,所以它不能是块作用域。
将use
声明移到shareFB
功能之外。
答案 1 :(得分:0)
试试这个:
require_once(dirname(__DIR__) . '/includes/facebook-php-sdk-v4-4.0-dev/src/Facebook/FacebookRequest.php');
use Facebook\FacebookRequest;
public function shareFB() {
...
要发布到网页,您需要publish_pages
和manage_pages
,并且您需要使用网页令牌发布“作为网页”。但在这种情况下这不是问题 - 在您修复手头的问题之后,它可能只是一个问题,所以我添加它以防万一;)
关于一般使用PHP SDK:
您的代码中缺少很多东西。您需要使用正确的权限进行授权(请参阅上文),并且需要生成页面令牌。确保你知道所有这些。
以下是有关令牌的其他一些链接: