0

I am using a the following code to see if a website is down or not.

<?php
    if (isset($_POST['submit'])) {
        $url = $_POST['url'];
        $curl = curl_init($url);
        curl_setopt($curl, CURLOPT_NOBODY, true);
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
        curl_exec($curl);
        $code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
        if ($code == 200) {
            echo "<h3>Up!</h3>";
        } else {
            echo "<h3>Down!</h3>";
        }
    }
?>

<form action="" method="post">
    Url: <input type="text" name="url" /><br />
    <input type="submit" name="submit" />
</form>

The issue I am having is when I check facebook.com it says its down but it isn't...

Why is this doing this? I orginally thought it could be the https but google works fine.

Jenz
  • 8,280
  • 7
  • 44
  • 77
Rick Skeels
  • 513
  • 1
  • 11
  • 30
  • What kind of http method are you using? Does the nobody part mean it will send a HEAD? Facebook probably has a problem with some part of your call. – gpgekko Feb 06 '14 at 10:01
  • 1
    Well, it's quite easy to say the http code facebook returns is not 200. Take a look at http://en.wikipedia.org/wiki/List_of_HTTP_status_codes to learn about http status codes. – Kei Feb 06 '14 at 10:01
  • not to sure i searched the web for this and i have tried a few i found but all the same – Rick Skeels Feb 06 '14 at 10:03
  • @Kei your right i amended my code to echo the the code given its 301 – Rick Skeels Feb 06 '14 at 10:05
  • 1
    @RickNash The correct url to use for facebook would be `https://www.facebook.com`, it's the only thing it will respond to. On the other hand, 301 answers mean the site is up so you really should expand your code to accept more than 200's. – gpgekko Feb 06 '14 at 10:08
  • yes thx i thought i was going mad! i will add more if statements to the error codes i need :) – Rick Skeels Feb 06 '14 at 10:13

2 Answers2

1

I believe your curl isn't following HTTP redirects, this means you'll always receive a 301 response from Facebook.

Here's a thread on how to make your curl follow redirects:

Make curl follow redirects?

Community
  • 1
  • 1
Tristan Godfrey
  • 349
  • 4
  • 14
  • thank you for your answer, i have read and amended the code to follow redirects and still im getting 301 but you have taught me a lot from providing the link thanks. – Rick Skeels Feb 06 '14 at 10:15
  • Could you post your updated code? Another good practice is for your if statement to check for status codes indicating that the site is down, rather than the infinite amount of status codes that indicate that the site is up. – Tristan Godfrey Feb 06 '14 at 10:20
  • yes im going to answer just in-case others need the same – Rick Skeels Feb 06 '14 at 10:23
1

I decided to post my updated code incase others are having same issue

<?php
    if (isset($_POST['submit'])) {
        $url = $_POST['url'];
        $curl = curl_init($url);
        curl_setopt($curl, CURLOPT_NOBODY, true);
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
        curl_exec($curl);
        $code = curl_getinfo($curl, CURLINFO_HTTP_CODE);

        echo "<h3>$code</h3>";



        if ($code == 200) {
            echo "<h3>Up! with a 200 code</h3>";
        } 
        if ($code == 301) {
            echo "<h3>Up! with a 301 code</h3>";
        } 
        else {
            echo "<h3>Down!</h3>";
        }
    }
?>

<form action="" method="post">
    Url: <input type="text" name="url" /><br />
    <input type="submit" name="submit" />
</form>

Above you can see i added the following

if ($code == 301) {
            echo "<h3>Up! with a 301 code</h3>";
        } 

There are alot of offline codes so i decided i would just use the online codes

as Kei posted in a comment you can see the list of return codes here

i also added the line echo "<h3>$code</h3>"; so you can see the responce from the form ie 200, 301

Rick Skeels
  • 513
  • 1
  • 11
  • 30