YES API PHP Wrapper

» December 6th, 2008 » 3 Comments

I had some time and so I whipped up a quick PHP wrapper for the YES.com API. This is a pretty cool api if you haven’t seen it, check it out! Anyways, you can get the wrapper from the download link below or just copy/paste it.

< ?php

//  YES API Wrapper version 1.0
//  (c) 2008 Kevin Miller
//
//  This script is freely distributable under the terms of an MIT-style license.

class YESAPIWrapper
{

	const API_URL = "http://api.yes.com";
	const API_VERSION = "1";

	public function __construct()
	{
	}

	public function execute($method = null, $options = array())
	{
		if ($this->allowed_method($method))
		{
			return $this->fetch(self::API_URL . "/" . self::API_VERSION . "/" . $this->method . "?" . $this->options_string($options));
		}
	}

	protected $method;

	protected $allowed_paramaters = array(
		"station" => array(
			"name" => true,
			"callback" => false
		),
		"stations" => array(
			"match" => false,
			"freq" => false,
			"media" => false,
			"genre" => false,
			"loc" => false,
			"max" => false,
			"callback" => false
		),
		"log" => array(
			"name" => true,
			"ago" => false,
			"callback" => false
		),
		"recent" => array(
			"name" => false,
			"max" => false,
			"callback" => false
		),
		"chart" => array(
			"name" => false,
			"date" => false,
			"genre" => false,
			"hot" => false,
			"max" => false,
			"callback" => false
		),
		"media" => array(
			"q" => false,
			"mid" => false,
			"callback" => false
		),
		"related" => array(
			"mid" => true,
			"max" => false,
			"callback" => false
		),
		"artist" => array(
			"artist" => true,
			"callback" => false
		)
	);

	private function allowed_method($method)
	{
		foreach ($this->allowed_paramaters as $key => $value)
		{
			if ($key == $method)
			{
				$this->method = $method;
				return true;
			}
		}

		throw new Exception($key . " is a required parameter for the method (" . $this->method . ")!");
	}

	private function validate_options($options)
	{
		foreach ($this->allowed_paramaters[$this->method] as $key => $value)
		{
			if ($value && !array_key_exists($key, $options))
			{
				throw new Exception($key . " is a required parameter for the method (" . $this->method . ")!");
			}
		}		

		foreach ($options as $key => $value)
		{
			if (!array_key_exists($key, $this->allowed_paramaters[$this->method]))
			{
				throw new Exception($key . " is not an valid parameter for the method (" . $this->method . ")!");
			}
		}		

		return true;
	}

	private function options_string($options = array())
	{
		if ($this->validate_options($options))
		{
			return http_build_query($options);
		}
	}

	private function fetch($url = "")
	{
		return ($contents = file_get_contents($url)) ? json_decode($contents) : null;
	}

}

?>
Spread the Word
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • LinkedIn
  • NewsVine
  • Reddit
  • BlinkList
  • StumbleUpon
  • Technorati
  • Yahoo! Buzz
  • Propeller

Share

Comments

3 Responses to “YES API PHP Wrapper”

  1. F1X85o hi nice site thanx http://peace.com

  2. bob on January 3rd, 2009 at 2:17 am
  3. Do you have any examples/demos of this wrapper in use?

  4. Nick on January 22nd, 2009 at 5:40 am
  5. Can you please provide an example of this class in use… Thanks. :-)

  6. Jon on June 22nd, 2009 at 11:43 pm

Leave a Reply