How to use Facebook SDK 4.0 without composer with

2019-09-09 22:15发布

问题:

I want to send a notification to a Facebook user.

I have the following setup example, but can't seem to get anywhere, Keeps giving an error that "Facebook Class not found".

<?php 
session_start();
require_once 'autoload.php';
require_once( 'src/Facebook/FacebookSession.php' );
require_once( 'src/Facebook/FacebookRequest.php' );

use Facebook\FacebookSession;
use Facebook\FacebookRequest;
$facebook = new Facebook();

$app_id = '654346742464654654';

$app_secret = '--my-secret--';

$app_access_token = $app_id . '|' . $app_secret;

$response = $facebook->api( '/65785757987982452465465445465/notifications', 'POST', array(

                'template' => 'You have received a new message.',

                'href' => 'RELATIVE URL',

                'access_token' => $app_access_token
            ) );    

print_r($response);
?>

Thanks.

回答1:

The key classes you need to do basic requests to Facebook API are FacebookSession and FacebookRequest. The following example tries to post at user's wall.

//First of all, initialize with your Facebook App id and secret

FacebookSession::setDefaultApplication("YOUR_APP_ID", "YOUR_APP_SECRET");

//Now its time to make a new FacebookSession instance

$session = new FacebookSession($accessToken);

//Sending the API request for posting
$response = (new FacebookRequest($session,"POST","/me/feed",array(
                "message" => "I'm a cool guy"
            )))->execute();

$responseObject = $response->getGraphObject();

//do something with the result

Additionally you have to make sure your Facebook application has enough permissions to do the action you are requesting (send a notification in this case)

For further details about Facebook API permissions review please check: https://developers.facebook.com/docs/apps/review

And for details about Facebook PHP Api: https://developers.facebook.com/docs/reference/php/4.0.0

Hope it helps



回答2:

This is what I do for notifications (without all the other code for my app). In my case a user has posted a new reply to a forum topic. In other places I've already verified its a logged in user making this request so I can initiate the call to post a new notification. I got kind of lazy when it came to the use statements and I normally just throw every single one on there just in case, but I only left the ones I thought you might need here. Make sure to add error handling! There's also more than one way of wording the request.

<?php 

session_start();

require_once 'autoload.php';

use Facebook\FacebookRequest;
use Facebook\FacebookRequestException;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookServerException;
use Facebook\FacebookSession;

use Facebook\GraphObject;

$app_id="APP_ID";
$app_secret="APP_SECRET";

$access_token=$app_id."|".$app_secret;

FacebookSession::setDefaultApplication($app_id,$app_secret);

$session=new FacebookSession($access_token);

try
{
  $request=(new FacebookRequest($session,'POST','/'.$user_id.'/notifications',array('href'=>$url,'template'=>$message,'access_token'=>$access_token)));
  $response=$request->execute();
}
catch(FacebookRequestException $ex)
{
  //Handle error here.
}
catch(\Exception $ex)
{
  //Handle error here.
}

?>