Facebook PHP SDK v5.0 - FacebookRequest not found Error
You should specifically define the namespace when creating the object:
$request = new Facebook\FacebookRequest(...
or keep with new FacebookRequest
but add use Facebook\FacebookRequset
at the beginning of your script.
Also pay attention that you're using the Facebook SDK v4 syntax instead of v5. See here for information about how to use FacebookRequest
with SDK v5.
Comments
-
gurung almost 2 years
I am using PHP-SDK-v5.0 package downloaded from here. I am trying to get post-feeds from a public group following an example provided by facebook. When I try to run the script, it throws an error -
Fatal error: Class 'FacebookRequest' not found
. Here is the example that throws an error.require_once __DIR__. '/Facebook/autoload.php'; $request = new FacebookRequest( $session, 'GET', '/{group-id}/feed', array( 'fields' => 'full_picture,attachments.limit(3){media,title,type,url},icon,message', 'limit' => '1' ) ); $response = $request->execute(); $graphObject = $response->getGraphObject(); var_dump($graphObject);
I have another simple facebook login script based on the example below, which shares the same directory and works perfectly. So definitely not a path issue.
session_start(); require_once __DIR__. '/Facebook/autoload.php'; $fb = new Facebook\Facebook([ 'app_id' => 'xxxxx', 'app_secret' => 'xxxxxxxxxxx', 'default_graph_version' => 'v2.2', ]); $helper = $fb->getRedirectLoginHelper(); $permissions = ['email', 'public_profile', 'user_friends']; // Optional permissions $loginUrl = $helper->getLoginUrl('http://domain.com/fb-app/login-callback.php', $permissions); echo '<a href="' . htmlspecialchars($loginUrl) . '">Log in with Facebook!</a>';
I suspect it is an issue related to namespace of which I am not very familiar. I tried using a solution to this question question, which suggests to use
use Facebook\FacebookRequest;
but does not work, neither does$request = new Facebook\FacebookRequest
. Also I feel that the script is incomplete and is supposed to incorporate an access token. I am still learning stuffs, please help me solve this. -
gurung over 8 yearsI had tried that earlier it throws this error -
Fatal error: Call to undefined method Facebook\FacebookRequest::execute()
-
uri2x over 8 yearsThat's a new subject :) That happens because you're probably reading a manual of an outdated SDK - v4 instead of v5. Look here for the latest manual on FacebookRequest.
-
gurung over 8 yearsSorry but as I said in my question I have already tried
use Facebook\FacebookRequest;
. it does not work and usingnew Facebook\FacebookRequest
throws the above-mentioned additional error. -
uri2x over 8 years@gurung, the undefined method is called because you're not using the SDK correctly. You're trying to use a method from Facebook SDK v4, while you've downloaded Facebook SDK v5. See here how to properly use this class (and don't forget to include autoload.php): developers.facebook.com/docs/php/FacebookRequest/5.0.0
-
gurung over 8 yearsI have the updated code in the bottom of my question. Please take a look, still throwing an error.
-
uri2x over 8 yearsThere's no
->execute()
at all on v5. I suggest you take one of the v5 examples as-is and start from there. -
Lee almost 8 yearsactually I'm getting the same thing, and these are in fact as described on the 5.0 docs...
-
Lee almost 8 years@uri2x developers.facebook.com/docs/graph-api/reference/page/ratings - these are the 5 docs.. ->execute() is there