Reliable way to determine an active domain name using cURL?
I'd like to run checks to see if a given URL is registered or not. As a simple proxy for testing if it's registered, I was going to check HTML response codes. 200, 300 probably == registered, 4xx probably == not registered.
I understand that there might be holes in this logic. My question is if cURL has a way to return the HTML response code neatly? I currently can print out the entire header, and am wondering if I need to parse out the bit I want on my own, or if it can return the whole header as an array.
A direc开发者_运维问答t answer or a great alternative would both be welcome.
The biggest hole in this logic is that just because a domain is registered doesn't mean it has a web server running at it.
The best way to tell if a domain is registered is to do a whois check. In PHP there's the PEAR::Net_Whois library for that.
The second biggest hole in your logic is that if a domain is responding to requests--whether 200, 300, or 404, it's definitely registered. But, if you do just want to check the response code, the correct way is to do an HTTP HEAD request, which tells the server you don't want a complete response, just the headers. You can do with PHP's cURL library by setting CURLOPT_CUSTOMREQUEST
to 'HEAD'
:
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'HEAD');
Take a look at checkdnsrr()
.
Searches DNS for records of type type corresponding to host.
bool checkdnsrr ( string $host [, string $type = "MX" ] )
host
may either be the IP address in dotted-quad notation or the host name.
type
may be any one of: A, MX, NS, SOA, PTR, CNAME, AAAA, A6, SRV, NAPTR, TXT or ANY.
精彩评论