Undermine2k
Undermine2k

Reputation: 1491

catch curl error with PHP request library

I'm using https://github.com/rmccue/Requests

I'm making a request using a random Proxy sometimes the proxy will work sometimes it will fail: with different errors: Uncaught exception 'Requests_Exception' with message 'cURL error 28: connect() timed out!' My question is: How can I catch this CURL error, and try another another proxy in a loop?

I tried var_dump($request->status_code); it outputs 200 reguardless of success or fail.

include('../library/Requests.php');

// Next, make sure Requests can load internal classes
Requests::register_autoloader();

// Now let's make a request via a proxy.
$options = array(
    'proxy' => '$randomProxyIP:$randomProxyPort')
);
$request = Requests::get('http://httpbin.org/ip', array(), $options );


var_dump($request->status_code); 

Upvotes: 1

Views: 789

Answers (2)

Kasyx
Kasyx

Reputation: 3200

In documentation you can find lots of exceptions. Also your message means that you have exception Requests_Exception to catch. All of them are subclases from Requests_Exception (here) so in basics way you should:

try {
    $request = Requests::get('http://httpbin.org/ip', array(), $options );
} catch (Requests_Exception $e) {
    //something goes wrong
}

Upvotes: 3

SeanCannon
SeanCannon

Reputation: 77966

try {
    $request = Requests::get('http://httpbin.org/ip', array(), $options );
} catch (Exception $e) {
    var_dump($e->getMessage());
}

Upvotes: 1

Related Questions