Location: PHPKode > scripts > Opendada ringtone search API Wrapper > opendada-ringtone-search-api-wrapper/opendada.class.php
<?php
/**
 * Gaurav Mishra | http://phpcollection.com/
 * MSN: hide@address.com
 * Email: hide@address.com
 * Started: 22/08/2009 09:44 (IST) 
 * Tested: No
 * PHP 4/5: 5
 * No warranty is given to code used
 */

/**
 * OpenDada API
 * 
 * @package   OpenDada API
 * @url       http://phpcollecttion.com/classes/dada
 * @desc      Package which allows easy interaction with the Dada.net content service
 * @author    Gaurav Mishra
 * @copyright 2009
 * @version   0.0.1
 * @access    public
 * @license   GPL
 */
 
 /**
Todo:-
1) Add functionality to get more than 10 ringtones.
2) Put the class in phpclasses.org
3) Add option of inserting partner id

*/

class OpenDada
{
    /** Your API KEY */
    public $key;

    /** URI to the API , used for making REST call. */
    const CHART_REQUEST =  "http://api.dada-ent.com/chart/";
    const SEARCH_REQUEST = "http://api.dada-ent.com/search/";

    /**
     * OpenDada::__construct()
     * 
     * @param  mixed  $apikey   Your developer API Key
     * @param  bool   $check    Should we run checks to make sure all the extensions are loaded?
     * @return void
     */

    public function __construct($apikey = null, $partner_id = null, $check = true)
    {
        /** Construct class, runs on initialization **/
        if ($check) {
            /** If the check parametre is set **/
            if (!extension_loaded('curl') /** Is the cURL extension loaded? **/ ) {
                throw new Exception('You don\'t have cURL loaded.');
                /** If not, throw an exception **/
            }
            if (!extension_loaded('SimpleXML') /** Is the SimpleXML extension loaded? **/ ) {
                throw new Exception('You don\'t have the SimpleXML lib loaded.');
                /** If not, throw an exception **/
            }
        }
        /** if $apikey aren't the default values of null, set them in the class. **/
        if (!is_null($apikey))
            $this->dev_api = $apikey;    
        else
        	throw new Exception('You don\'t have the api key.');

        	
    }
    /**
     * OpenDada::get_top_10()
     * 
     * @param  void   
     * @return array  $data		Array of data of results 
     */

    function get_top_10()
    {
        /** URI used for making REST call. Each Web Service uses a unique URL.*/
        $request = constant ('self::' . CHART_REQUEST) .  'category/top10/contenttype/real';     
        /** Get the response via CURL */
		$response = $this->get($request);		
		/** Arrayed data of top 10 ringtones*/
        return $response;
    }
    /**
     * OpenDada::get_genre()
     * 
     * @param  mixed  $genre	string of data defining genre
     * @return array  $data		Array of data of results 
     */
    function get_genre($genre)
    {
        $genre = str_replace("-", " ", $genre);
        $genre = urlencode($genre);        
        /** URI used for making REST call. Each Web Service uses a unique URL.*/
 		$request = constant ('self::' . CHART_REQUEST) .  "category/$genre/contenttype/real/";     
		/** Get the response via CURL */
		$response = $this->get($request);
        /** Arrayed data of top 10 ringtones according to the genre given*/
		return $response;
    }
    /**
     * OpenDada::search_ringtone()
     * 
     * @param  mixed  $keyword 	string of data defining keyword to search   
     * @return array  $data		Array of data of results 
     */
    function search_ringtone($keyword)
    {

        $keyword = urlencode($keyword);
		/** URI used for making REST call. Each Web Service uses a unique URL.*/
	 	$request = constant ('self::' . SEARCH_REQUEST) .  "keywords/$keyword/contenttypes/real/";     
		/** Get the response via CURL */	 	
		$response = $this->get($request);
        /** Arrayed data of top 10 ringtones according to the genre given*/
        return $response;
    }
    /**
     * OpenDada::display_single_ringtone()
     * 
     * @param  mixed  $artist 	string of data defining artist to get data from   
     * @param  mixed  $title 	string of data defining title to get data from
	 * @return array  $data		Array of data of results 
     */
    function display_single_ringtone($artist, $title)
    {

        $artist = urlencode($artist);
        $title = urlencode($title);
  		/** URI used for making REST call. Each Web Service uses a unique URL.*/
		$request = constant ('self::' . SEARCH_REQUEST) .  "artist/$artist/title/$title";     
		/** Get the response via CURL */	 	
		$response = $this->get($request);
        /** Arrayed data of top 10 ringtones according to the genre given*/
        return $response;    }
  
    /**
     * OpenDada::get()
     * 
     * @param  mixed  $request 	request data from the dada.net api !
	 * @return array  $data		arrayed data from the request 
     */
    
    function get ($request)
    {
		$key = $this->dev_api;
        $header = array();
        $header[] = "API-Key:$key";


		// Initialize the session by passing the request as a parameter
        $session = curl_init($request);

        curl_setopt($session, CURLOPT_HTTPHEADER, $header);
        curl_setopt($session, CURLOPT_VERBOSE, true);
        curl_setopt($session, CURLOPT_FOLLOWLOCATION, true);

        // Set curl options by passing session and flags
        // CURLOPT_HEADER allows us to receive the HTTP header
        curl_setopt($session, CURLOPT_HEADER, true);

        // CURLOPT_RETURNTRANSFER will return the response
        curl_setopt($session, CURLOPT_RETURNTRANSFER, true);


        // Make the request
        $response = curl_exec($session);

        // Close the curl session
        curl_close($session);
        
        if (!($xml = strstr($response, '<?xml'))) {
            $xml = null;
        }
     
        // Create a SimpleXML object with XML response
        $simple_xml = simplexml_load_string($xml);

		$i = 0;

        // Traverse XML tree and save desired values from child nodes
        foreach ($simple_xml->results->result as $result) {
            $data[$i]['artist'] = (string)$result->artist;
            $data[$i]['title'] = (string)$result->title;
            $data[$i]['contentId'] = (string)$result->contentId;
            $data[$i]['imageMediumURL'] = (string)$result->imageMediumURL;
            $data[$i]['mediaId'] = (string)$result->mediaId;
            $data[$i]['artistId'] = (string)$result->artistId;
            $data[$i]['isReal'] = (string)$result->isReal;
            $data[$i]['isMp3'] = (string)$result->isMp3;
            $data[$i]['artisturl'] = (string)$result->artisturl;
            $data[$i]['landingpageartisturl'] = (string)$result->landingpageartisturl;
            $data[$i]['landingpagecontenturl'] = (string)$result->landingpagecontenturl;
            $data[$i]['mobilepagecontenturl'] = (string)$result->mobilepagecontenturl;
            $data[$i]['mobilepageartisturl'] = (string)$result->mobilepageartisturl;
            $data[$i]['previewURL'] = (string)$result->previewURL;
            $i++;
        }
   	
        return $data;

    }





}
?>
Return current item: Opendada ringtone search API Wrapper