FaceBookRequest-> execute()使我的ajax调用返回状态为500

FaceBookRequest-> execute()使我的ajax调用返回状态为500

问题描述:

I'm trying to make a php script which receives an ajax call from a Facebook application to check if the id sent by the user is his own. I get the user id and access token in the next piece of code. The function validateMe() puts the id, the letter z as a separator and the access token together, then makes an ajax call

FB.getLoginStatus(function(response) {
    if (response.status === 'connected') {
        monLogin.uid=response.authResponse.userID;
        monLogin.token=response.authResponse.accessToken;
        validateMe();
    } 
}

the php script which receives the ajax call comes next. It returns to the ajax call with status 500 when the line $response=request->execute(); is not commented. When I stop the code before this line by commenting it, it returns to the ajax call with status 200. So, I believe there is something that makes this line not working as expected. I tried changing the name $faceb to $session as in some examples, but that's not it. Using the Facebook = new Facebook(array) object instead of the method setDefaultApplication doesn't do the trick either because the Facebook object is deprecated.

<?php
session_start();
include 'vendor/autoload.php';
use Facebook\FacebookSession;
use Facebook\GraphObject;
use Facebook\GraphUser;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookAuthorizationException;

if (isset($GLOBALS['HTTP_RAW_POST_DATA'])){
    $appid='123456';  //not the true appid
    $psswd='123456abcdef';  //not the true password
    FacebookSession::setDefaultApplication($appid, $psswd);
    $infosJoueur = $GLOBALS['HTTP_RAW_POST_DATA'];
    $arrayData = explode('z', $infosJoueur, 2);

    // arrayData[1] is the accessToken from the javascript
    $session = new FacebookSession($arrayData[1]);
    $request = new FacebookRequest($session, 'GET', '/me');
    $response = $request->execute();

    /*$graph = $response->getGraphObject(GraphUser::className());
    $id = $graph->getId();
    if (($arryData[0] = $id)) {
        echo 'yes';
        $_SESSION['uid']=$id;
    }
    else {echo 'no';}*/
}
?>

as proposed in the comments, i checked the logs of the server and found that the mbstring method didn't exist because I had to add it in the composer.json file. in the require, i added "ext-mbstring": "*". I still get an error 500 with a different log:

PHP Fatal error: Uncaught exception 'Facebook\FacebookAuthorizationException' with message 'The access token could not be decrypted'

thank you for pointing that more details on the problem could be in the server logs: mb_substr is not there by default for php, we have to tell php were to find it.

Call to undefined function Facebook\HttpClients\mb_substr() in /app/vendor/facebook/php-sdk-v4/src/Facebook/HttpClients/FacebookCurlHttpClient.‌​php on line 254

The answer can be found there Facebook PHP SDK error mb_substr

(then i had a few bugs that i added while tweaking the code in search of a solution like removing the 2 in the explode)