简体   繁体   中英

Fatal Error stops execution in PHP

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?

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();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM