Ticket #348 (assigned Patch)
Add url::is_online(); to Helpers
| Reported by: | cecplex | Owned by: | Shadowhand |
|---|---|---|---|
| Priority: | minor | Milestone: | 2.3 |
| Component: | Helpers | Version: | SVN HEAD |
| Keywords: | url | Cc: |
Description
It's a bit of an unusual function as it's not like the other url functions; however, it's proven to be VERY helpful for me and might be for others too.
If you guys agree, maybe bring it into the Kohana core.
/*
* Method: is_online
* Checks if the provided URL is online.
*
* Parameters:
* uri - URL to check
*
* Returns:
* True or False depending on the status of the given URI
*/
public static function is_online($link) {
$url_parts = @parse_url($link);
if(empty($url_parts['host'])) {
return false;
}
if(!empty($url_parts['path'])) {
$documentpath = $url_parts['path'];
} else {
$documentpath = "/";
}
if(!empty($url_parts['query'])) {
$documentpath .= "?" . $url_parts['query'];
}
$host = $url_parts['host'];
$port = $url_parts['port'];
// Now (HTTP-)GET $documentpath at $host";
$port = empty($port) ? 80 : $port;
$socket = @fsockopen( $host, $port, $errno, $errstr, 30 );
if(!$socket){
return false;
} else {
fwrite ($socket, "HEAD ".$documentpath." HTTP/1.0\r\nHost: $host\r\n\r\n");
$http_response = fgets($socket, 22);
if(ereg("200 OK",$http_response,$regs)) {
return true;
fclose($socket);
} else {
return false;
}
}
}
Change History
Note: See
TracTickets for help on using
tickets.
