Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP's cURL suddenly failing to get an IP address, is file_get_contents a good replacement?

Tags:

php

dns

We have a page on our site that uses cURL to get XML data from a remote domain. A few days ago it randomly started failing (perhaps 1/3 of requests fail). After debugging with our host and with the remote site's operators, we found that the curl error is 'name lookup timed out', indicating a DNS problem. Our CURLOPT_CONNECTTIMEOUT was set to 5. When I changed that to 30, it worked every time.

But this is a live page, I can't have visitors hanging for 30 seconds while waiting for a response. Plus, the increased timeout doesn't answer the question of why this started failing in the first place. The system had been in place for years prior and the 5 second timeout was always fine.

Furthermore I found that if I do a dns_get_record(), it works every time and I quickly get a valid IP address. So I modified the script to first do a dns_get_record(), then I cURL to the IP it returns, which gets around the name lookup on cURL's end. It works fine but it's silly.

So first question, does anyone have any suggestions as to how or why the cURL may be failing. Our host and the remote site's host both agree that it's a DNS server somewhere, but neither agrees on who's DNS server is responsible, because both say that their own servers are good, and our host says they can ping the remote domain without a problem.

Second question, is file_get_contents() a sufficient replacement for dns_get_record() + cURL? Or should I stick with dns_get_record() + cURL instead?

like image 396
Mike Willis Avatar asked Jan 23 '26 10:01

Mike Willis


1 Answers

Under the hood, both curl_exec and file_get_contents perform nearly identical operations; they both use libresolv to:

  1. connect to a name server
  2. issue a dns request
  3. process the dns response

To further debug this, you can use curl_getinfo() to get detailed statistics about your requests; you can use this to get an idea of how long each part took using:

  • CURLINFO_NAMELOOKUP_TIME
  • CURLINFO_CONNECT_TIME
  • CURLINFO_PRETRANSFER_TIME
  • ...
like image 156
Ja͢ck Avatar answered Jan 26 '26 00:01

Ja͢ck



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!