Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

curl and ping - how to check whether a website is either up or down?

I want to check whether a website is up or down at a particular instance using PHP. I came to know that curl will fetch the contents of the file but I don't want to read the content of the website. I just want to check the status of the website. Is there any way to check the status of the site? Can we use ping to check the status? It is sufficient for me to get the status signals like (404, 403, etc) from the server. A small snippet of code might help me a lot.

like image 842
brainless Avatar asked Jan 05 '11 18:01

brainless


People also ask

How do I test my website using curl command?

The syntax for the curl command is as follows: curl [options] [URL...] In its simplest form, when invoked without any option, curl displays the specified resource to the standard output. The command will print the source code of the example.com homepage in your terminal window.

How does curl use data to get from a website?

To make a GET request using Curl, run the curl command followed by the target URL. Curl automatically selects the HTTP GET request method unless you use the -X, --request, or -d command-line option.

Can Ping can't curl?

Check your firewall rules to ensure this port is open. Ping does not connect to a port (it uses ICMP), so that is why pinging works without opened ports. On a side note, your ping and curl commands show you're trying to connect to "db-machine-02".


1 Answers

something like this should work

    $url = 'yoururl';     $ch = curl_init($url);     curl_setopt($ch, CURLOPT_NOBODY, true);     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);     curl_exec($ch);     $retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);     curl_close($ch);     if (200==$retcode) {         // All's well     } else {         // not so much     } 
like image 66
Jeff Busby Avatar answered Oct 11 '22 17:10

Jeff Busby