Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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?

  • CURLOPT_TIMEOUT - how long the entire operation is taking
  • CURLOPT_CONNECTTIMEOUT - how long cURL waits for the host to respond to its request
  • 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.

    It's not going to be particularly easy, as DNS is a synchronous affair as a rule, which blocks the process. There is 'adns' ( chiark.greenend.org.uk/~ian/adns ) - which provides asynchronous lookups, it's old but functional. Probably easy enough to call dig or nslookup externally and kill the process if it takes too long, but that introduces overhead. If you can use pcntl_fork(), then you could start a new thread to do that, or use pcntl_alarm() to get out of the lockup early. Orbling Nov 19, 2010 at 14:59 In fact I have it set to 10 seconds and resolving timeout after 2.5 seconds happens. Tested in php 7.2 John Jan 16, 2019 at 2:42

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question . Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers .