Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
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: Select all
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;
}
}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
feyd | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]