1

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

2 Answers 2

3

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
}
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.