Jump to content

checking if a HTTPS server is available


poizn

Recommended Posts

Hi everybody

I need to check if a URL is up, so i searched the net and people suggested that you send a HEAD request to the server and check the result (code to follow)

[code]
  function url_exists($url) {
//could have used urlencode - but this works better ;)
    $url = preg_replace("# #" , "%20" , $url);
    $a_url = parse_url($url);
//break the url into scheme, host, post ect...
    if(!isset($a_url["port"])) {
      if(strtolower($a_url["scheme"]) == "https")
        $a_url["port"] = 443;
      else
        $a_url["port"] = 80;
    }
    if(isset($a_url["host"]) && $a_url["host"] != gethostbyname($a_url["host"])) {
//open socket to host X on port Y
      if(!$fid = fsockopen($a_url["host"] , $a_url["port"]))
        return false;
      $page = isset($a_url["path"])?$a_url["path"]:"/";
      $page .= isset($a_url["query"])?"?".$a_url["query"]:"";
//send request to server
      fputs($fid , "HEAD $page HTTP/1.0\r\nHost: ".$a_url["host"]."\r\n\r\n");
      $head = fread($fid , 4096);
      fclose($fid);
//return true or false based on what the server returns
      return preg_match("#^HTTP/.*\s+[200|302]+\s#i" , $head);
    } else {
      return false;
    }
  }
[/code]

This function works pretty cool, except for when you try a HTTPS server.
This is what i get if i print $head on some HTTPS url's

[server_response]
Your browser sent a request that this server could not understand.
Reason: You're speaking plain HTTP to an SSL-enabled server port.
Instead use the HTTPS scheme to access this URL, please.
[/server_respone]

Does anyone know "how to speak HTTPS"?

Any help will be appreciated

Thanks in advance
Link to comment
Share on other sites

to everybody looking at this post now, i finally got the answer from another forum

[quote]
You have to put ssl:// before the hostname for it to work properly. Here is an example using pfsockopen, but it should work with fsockopen.

http://www.php.net/manual/en/functi...kopen.php#67395
[/quote]

thanks
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.