J7mbo / twitter-api-php

The simplest PHP Wrapper for Twitter API v1.1 calls
MIT License
1.82k stars 802 forks source link

Catching Exception from performRequest method #224

Closed ghost closed 7 years ago

ghost commented 7 years ago

I'm trying to catch an exception that is thrown during the performRequest (the if statement with the conditional: (($error = curl_error($feed)) !== '')) but can't seem to get it to function correctly. Any ideas?

Here's what I'm trying to do:

$settings = array(
    'oauth_access_token' => OAUTH_ACCESS_TOKEN,
    'oauth_access_token_secret' => OAUTH_ACCESS_TOKEN_SECRET,
    'consumer_key' => CONSUMER_KEY,
    'consumer_secret' => CONSUMER_SECRET
);
for($abc = 0; $abc <= 2; $abc++){
    try{
        $twitterGet = new TwitterAPIExchange($settings);
        $twitterPost = new TwitterAPIExchange($settings);
    }catch(Exception $e){
        echo 'Exception Caught';
        if($abc === 2){
            exit("Failed to Connect to Twitter.");
        }
    }

    if(!isset($e) || $e==='' || $e===null){
        break;
    }
}

Doing the above I still get an exception coming through and giving me a Fatal Error and Exception notice when I lose an internet connection from the system running the script.

EwenH commented 7 years ago

Have you tried CURL debug? A nice explanation can be found here https://blog.kettle.io/debugging-curl-requests-in-php/ and that may narrow it down for you. I find that CURLOPT_SSL_VERIFYPEER is normally the biggest culprit.

Best of luck

ghost commented 7 years ago

I don't actually have a connection issue.... Things work fine. What I'm trying to do is catch the thrown exception, to output my own error message when in the rare circumstance that my system temporarily loses an internet connection. Right now I'm purposefully disabling my internet connection.

Here in this code the API throws an exception:

 if (($error = curl_error($feed)) !== '')
        {
            curl_close($feed);
            throw new \Exception($error);
        }

What I'm trying to do is customize the output that the exception displays by catching it with this:

for($abc = 0; $abc <= 2; $abc++){
    try{
        $twitterGet = new TwitterAPIExchange($settings);
        $twitterPost = new TwitterAPIExchange($settings);
    }catch(Exception $e){
        echo 'Exception Caught';
        if($abc === 2){
            exit("Failed to Connect to Twitter.");
        }
    }

    if(!isset($e) || $e==='' || $e===null){
        break;
    }
}

The issue is the above is NOT catching the exception.

EwenH commented 7 years ago

@AndrewMcCarrick , This is a bit outside my comfort zone but hopefully I can't lead you astray twice! You may want to look at curl_getinfo() and checking the header as this should tell give you a 404 or another timeout error... e.g. I'm not sure if this is done already in this Twitter API but I can't think of anywhere else you would check connectivity.

curl_exec($ch);
$result = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($result != 200) {
    die('Request failed: HTTP status code: ' . $result);
}