I would like to call the shareFB()
method to auto share something on a Facebook Page which I manage. And maybe pass it data shareFB($link, $message)
Currently getting Parse error: syntax error, unexpected 'use' (T_USE)
.
How can I set this up to work?
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();
}
}
EDIT ***
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();
}
}
}