Ticket #348 (assigned Patch)

Opened 10 months ago

Last modified 6 weeks ago

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

Changed 10 months ago by gregmac

This seems a bit too specialized, imho. The rest of the url helper has to do with the local site, this doesn't really fit in and goes against the kohana goal of being lightweight.

At best, it probably belongs in a "web" or "http" library (which may not be part of core) along side other methods like fetch_page(), post(), etc.. (those can probably be combined, but i digress).

Changed 10 months ago by cecplex

I have to agree with you. I think the Kohana developers may want to implement an http helper as you mentioned though, I can see a big demand for functions like what you mentioned. Adding a http::post() function alone could replace a lot of repetitive cURL code in the Payment drivers.

Changed 10 months ago by Shadowhand

Changed 10 months ago by Shadowhand

http://kohana.pastebin.com/f5b77720b if the above link does not work.

Changed 10 months ago by Shadowhand

  • version set to SVN HEAD
  • milestone changed from 2.1 to 2.2

Changed 3 months ago by Shadowhand

See r3149.

Changed 6 weeks ago by Shadowhand

  • owner changed from - No owner - to Shadowhand
  • status changed from new to assigned
  • milestone changed from 2.2 to 2.3
Note: See TracTickets for help on using tickets.