Question

I have following code in PHP

foreach($arr as $u){
    //code runs for users
    $u->fetchFriends();
}

function fetchFriends(){

$sparam = array('method' => 'fql.query', 
                'query' => $fql, 
                'callback' => '', 
                'access_token' => $fbtoken);

    try{
        $fqlResult = $facebook -> api($sparam);
        }
    catch(Exception $e) {
        echo 'There is issue with Token';
    }
}

The problem is that if the FB API throws an exception, then the process stops and next users in foreach loop doesn't get executed. I want that even if it throws an error, the foreach loop should run for all users. Is that possible?

Was it helpful?

Solution

You need to catch those exceptions using a Facebook Api Exception catch block:

function fetchFriends(){

$sparam = array('method' => 'fql.query', 
                'query' => $fql, 
                'callback' => '', 
                'access_token' => $fbtoken);

    try {
        $fqlResult = $facebook -> api($sparam);

    } catch (FacebookApiException $e) {//catch FB
        echo 'There is an issue with the Token';

    } catch(Exception $e) {//catch PHP
        echo $e->getMessage().' in '.$e->getFile().' on line '.$e->getLine();
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top