Browsing the blog archives for June, 2008.

Ewerl Function

Code

Ewerl is a new URL rewritting site with cool features.

Here’s a simple function to return the Ewerl url. It’s only being used for the main url, but could easily be adjusted to return an array. I just didn’t have a need for it in my case at this time.

/**
 * Returns an Ewerl url if available.
 * Otherwise the original that was passed in.
 * Returns false if..
 * 1. JSON extension isn't installed.
 * 2. No url was entered.
 * 3. No response from server.
 *
 * @todo Add URL validation.
 *
 * @param string $url
 * @return string URL, or false.
 * @author Casey Wilson 
 */
function getEwerl($url) {
	/**
	 * Verify JSON extension is loaded.
	 */
	if (!extension_loaded('JSON')) return false;

	/**
	 * Url wasn't passed
	 */
	if (!$url) return false;

	/**
	 * Assign / Prep vars.
	 */
	$postUrl = 'http://ewerl.com/api/v1/make/url=';
	$urlEncoded = base64_encode($url);

	$response = file_get_contents($postUrl . $urlEncoded);

	if (!$response) {
		return $url;
	}

	/**
	 * Should have returned a JSON object.
	 */
	$jsonObj = json_decode($response);

	/**
	 * If either Status not set, or status is false, return original url.
	 */
	if (!isset($jsonObj->status) || $jsonObj->status == 'FALSE') {
		return $url;
	}

	return $jsonObj->url_main;
}
No Comments

jQuery

jQuery

My expierence with jQuery thus far hasn’t been to bad. I’m starting to see the power and simplicity of it.

I’ve never been a JS guy, never been in a position to really need to do much with it since I’ve basically always been a backend coder. I need to really jump in there, get a good feel for both JS and jQuery. So far though, I can say I’m really starting to like it, only been at it a few days thus far so we’ll see here soon what the final say is.

Thanks to good friends, and long late night chats with people smarter than I.

The open source community as a whole rocks.

No Comments

Url to TinyUrl Function

Code

I needed to transform some long urls into tiny ones :p

Figured I’d post something up here that may be useful to someone else.

Code was updated.

/**
 * Get TinyUrl for $url
 *
 * @param string $url Must be a valid url
 */
function getTinyUrl($url) {
	if (! $url) {
		return false;
	}

	if (strlen ( $url ) < 30) {
		return $url;
	}

	$response = file_get_contents ( 'http://tinyurl.com/api-create.php?url=' . $url );

	return ($response) ? $response : $url;
}
No Comments