format('xml'); $yql->query('select * from weather.forecast where location=90210'); $yql->submit(); $data = $yql->get(); } catch (Exception $e) { echo $e->getMessage(); } */ class YQL { /** @var string $_endpoint The location to query to */ private $_endpoint = "http://query.yahooapis.com/v1/public/yql"; /** @var string $_query The YQL query to pass */ private $_query; /** @var string $_format The format to return back */ private $_format = 'xml'; /** @var array $_flag Flag for making sure a section was called before submitting */ private $_flag = array(); /** * query - Prepares a query for Yahoo * @param string $yqlCode The YQL query to pass (See: http://developer.yahoo.com/yql/console/) */ public function query($yqlCode) { $this->_query = urlencode($yqlCode); array_push($this->_flag, __FUNCTION__); } /** * format - Optional. The format you'd like returned * @param string $format Use either 'xml' or 'json' */ public function format($format) { $this->_format = strtolower($format); } /** * submit - Sends the Query to Yahoo, we then have a stored result. */ public function submit() { /** Make sure the query method was called so we have a query */ if (!in_array('query', $this->_flag)) throw new Exception('You must set the query before submitting.'); /** Run through cURL */ $ch = curl_init($this->_endpoint . '?q=' . $this->_query . '&format=' . $this->_format); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $this->_result = curl_exec($ch); if (curl_error($ch)) throw new Exception(curl_error($ch)); curl_close($ch); /** Let's flag submit as called */ array_push($this->_flag, __FUNCTION__); } /** * get - Returns the result in a usable SimpleXML format (Default) or a JSON Decoded format */ public function get() { if (!in_array('submit', $this->_flag)) throw new Exception('You must call submit before getting return data'); switch ($this->_format) { case 'xml': return simplexml_load_string($this->_result); break; case 'json'; return json_decode($this->_result); break; } } }