Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I change the php curl timeout when curl attempts to resolve host (curl err_no 6)

I am performing a curl request to a page. I have set both 'CURLOPT_TIMEOUT' (to 6 seconds) and 'CURLOPT_CONNECTTIMEOUT' (to 4 seconds) - and both work perfectly for most URL's.

However, when a webpage does not respond (and finally causes CURL to through a 'Could not resolve host' error (err_no 6) ) - it seems that 'CURLOPT_TIMEOUT' has no effect and curl will wait, possibly indefinately or until some 'other' timeout expires.

With the URL I am trying to access, CURL always seems to take pretty much exactly 15 seconds before it returns (which has nothing to do with the 'CURLOPT_TIMEOUT' or the 'CURLOPT_CONNECTTIMEOUT'.

Can somebody tell me how I can limit the amount of time a CURL spends attempting to resolve a host?

like image 910
Tom Carnell Avatar asked Nov 19 '10 14:11

Tom Carnell


Video Answer


2 Answers

Well you've got 3 timeouts on your hands there.

  1. CURLOPT_TIMEOUT - how long the entire operation is taking
  2. CURLOPT_CONNECTTIMEOUT - how long cURL waits for the host to respond to its request
  3. DNS Lookup Timeout - what you are experiencing, the DNS query fails and takes a certain time to do this

cURL in PHP does not have a default method for setting this timeout, I would suggest resolving the hostname using another method with a timeout, and then passing the IP to cURL.

like image 164
Orbling Avatar answered Sep 28 '22 20:09

Orbling


change dns resolve timeout before use cURL

putenv('RES_OPTIONS=retrans:1 retry:1 timeout:1 attempts:1');
like image 32
user3665895 Avatar answered Sep 28 '22 21:09

user3665895