Reputation: 1491
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
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
Reputation: 77966
try {
$request = Requests::get('http://httpbin.org/ip', array(), $options );
} catch (Exception $e) {
var_dump($e->getMessage());
}
Upvotes: 1