91media - Blog

Working with YQL in CodeIgniter

YQL is a pretty neat tool for web developers. Consider it an API for APIs. It really takes the pain out of mashing together several webservices by providing a unified interface to access these services. Go check it out, if you haven’t already.

When working with YQL to build RIAs you’d probably want to go with JavaScript using libraries such as YUI or jQuery. Recently, however, we had the urgent need to use YQL from CodeIgniter for collecting information from several webservices and cache them on our own servers. This is important to us, because in the app we’re currently building, we’ll need to compare different versions of information and query results. Therefore we decided to go server-side. Since we’re building on CodeIgniter anyway, we decided to write a little library. It’s nothing special, but still we wanted to share it with you. Hope you find it useful. If you have any improvement ideas or feature ideas, let us know!

<?php
_set_latest_query($query);
		
		$url = "http://query.yahooapis.com/v1/public/yql?q=";
		$url .= urlencode($query);
		$url .= "&format=json&env=store://datatables.org/alltableswithkeys";
		
		$response = $this->_query($url);
		
		return $this->_decode_response($response);
	}
	
	private function _set_latest_query($value) {
		$this->_latest_query = $value;
	}
	
    function get_latest_query() {
		return $this->_latest_query;
	}
	
	private function _query($url) {
		return file_get_contents($url);
	}
	
	private function _decode_response($response) {
		$this->_dec_response = json_decode($response);
		return $this->_dec_response->query->results;
	}
}
Blog comments powered by Disqus
More Information