Tag Archives: API

PHP Weirdness

Beware: this post is definitely not for the feint of heart. It includes a lot of code. You have been warned.

I wrote an application some time ago for my company that looks up the longitude and latitude of an address for use in our geocoding initiative. It relied on yahoo_geo(), a function written by PHP creator Rasmus Lerdorf and the Yahoo Maps API. It was largely dependent on this function:

function yahoo_geo($location) {
	$q = 'http://api.local.yahoo.com/MapsService/V1/geocode?appid=rlerdorf&location='
	$tmp = '/tmp/yws_geo_'.md5($q);
	request_cache($q, $tmp, 43200);
	$xml = simplexml_load_file($tmp);
	$ret['precision'] = (string)$xml->Result['precision'];
	foreach($xml->Result->children() as $key=>$val) {
		if(strlen($val)) $ret[(string)$key] = (string)$val;
	return $ret;

This function worked for over two years for us with no problems at all. Then suddenly, in the last month, it started getting spotty. I fixed things by commenting out the caching parts of the function and forcing each execution to run again. Then I got errors about the libxml_use_internal_errors() function, so I commented that out. But today, the function just flat out failed, every single time returning the same error:

Warning: file_get_contents(http://XXXXXXXXXX/XXX) [function.file-get-contents]: failed to open stream: HTTP request failed! in /home/intranet/html/fetch.php on line X

What the heck? This code is all over the web. I’ve tried a million permutations of this function, including using fopen() and ob_get_contents(), and none have worked. And most frustratingly, I could load the URL successfully in Lynx and eLinks, so the machine could quickly and easily fetch the URL.

So I ventured into a sandbox I’ve never really played before: cURL. cURL is an interesting animal. But the interesting thing is, once I got it working, it worked faster than ever! So, without further ado, here is the new and improved yahoo_geo() function:

function yahoo_geo($location) {
	$q = 'http://api.local.yahoo.com/MapsService/V1/geocode?appid=rlerdorf&location='.urlencode(trim($location));
	$ch = curl_init($q);
	curl_setopt($ch, CURLOPT_HEADER, 0);
	$stream = ob_get_contents();
	if($stream) {
		$xml = simplexml_load_string($stream);
		$ret['precision'] = (string)$xml->Result['precision'];
		if($xml) {
			foreach($xml->Result->children() as $key=>$val) {
				if(strlen($val)) $ret[(string)$key] =  (string)$val;
		return $ret;
	} else {
		return FALSE;

Note: If you’re reproducing these functions elsewhere, be careful – WordPress may have converted the quotes into smart quotes that will need to be fixed before this script will run properly.

Tagged , , , ,

Flickr Feeds

Who knew that Flickr supported so many types of feeds? RSS, RDF, Atom, SQL, PHP, Serialized PHP, YAML, CSV, JSON, CDF… that’s incredible.

Tagged , ,

My Dream App More Like A Nightmare

So, awhile back, someone came up with this idea – invite users to dream up an application, then hold a contest and actually develop the best ones. I mean – how cool is that, right? ANYTHING you can think of! What a great opportunity to see some incredible ideas come to life.

Let me cut to the chase: it appears the vote has been hacked. Clearly, something fishy is going on here!

The winners are – get this – a cookbook, a sync manager, and a thing that makes your desktop look like the weather. I cannot believe it. I’m stunned. THIS is what Mac users wanted? THIS is the best we could come up with?

One guy dreamt big: you hum into your computer and it pitch corrects and allows you to create a song. Whoa! Cool!

One girl had an interesting idea: you take pictures of your clothes and then can keep a “virtual closet” where you can look at your clothes together and design outfits.” Holy crap – NOTHING I know of does this, even if I don’t have much of a need for it.

But you see, these are REAL apps that are the first of their kind. Who needs a new sync manager? And who but chefs will really use Cookbook? And I couldn’t care less about my desktop wallpaper matching the weather, being as though I can LOOK OUTSIDE if I want to know!

What a letdown.

Read More | Digg Story

received via the firsttube.com API

Tagged , , , , ,

firsttube.com Now Has An API

Well, something I’ve LONG considered implementing is an API for my website. Yes, there’s VERY little to ever use an API for me, however, it’s always been one of those “out there” challenges. It’s always been breaking new ground to parse an XML request and return XML to it. I’ve tried probably 10 times before, and each time I’ve given up. This time, I had to figure it out.

So I slammed the pedal to the metal and did it. I now have a working API. It currently supports only 1 method reliably – blogger2.newPost() – however, I have tested blogger.getRecentPosts and both corresponding methods under the metaWeblog API. That said, I will probably hack something up that explains how to add an API to your homegrown PHP weblog. There are functions out there that do the hardwork, and there are functions I’ve written that do the rest of the work, so you just need to write the correct queries for your database.

Anyway, I can now post from digg.com, which is cool. Ultimately, if we ever support user blogs on OSNews, we’ll roll out an API so people can comment on OSNews and Digg stories via the API. Could be interesting.

Tagged , , , ,