Location: PHPKode > scripts > PHP Yahoo Music API > php-yahoo-music-api/PhpYahooMusicApi.inc.php
<?php
/**
 * This class make use of Yahoo Music API to get track, artist and album information.
 * 
 * @author Rochak Chauhan [www.rochakchauhan.com]
 * @version 1.0
 * @todo This class does not support Browser Based Authentication this is version.
 */
class YahooMusicApi {
    private $appKey="";

    /**
     * Construction function
     *
     * @param string $appKey
     */
    public function __construct($appKey){
        $this->appKey=$appKey;
    }

    /**
     * Function to get content of remote file / url
     *
     * @param string $url
     * @return string
     */
    private function getHtmlCodeViaFopen($url){
        $returnStr="";
        if($fp=@fopen($url, "r")) {
            while (!feof($fp)) {
                $returnStr.=fgetc($fp);
            }
            fclose($fp);
        }
        return $returnStr;
    }

    /**
     * This function returns artist info
     *
     * @param string $artistName
     * @return array
     */
    public function searchArtist($artistName){
        $artistName=urlencode($artistName);
        $return=array();
        $searchArtistApi="http://us.music.yahooapis.com/artist/v1/list/search/artist/$artistName?appid=".$this->appKey;
        $result=$this->getHtmlCodeViaFopen($searchArtistApi);
        if (trim($result)=="") {
            return $return;
        }
        $xml=simplexml_load_string($result);

        $result = array("ArtistRelevancy"=>'',
        "ArtistId"=>'',
        "ArtistName"=>'',
        "ArtistRating"=>'',
        "ArtistFlags"=>'',
        "ArtistSalesGenreCode"=>'',
        "ArtistCatzillaID"=>'',
        "ArtistHotzillaID"=>'',
        "ArtistTrackCount"=>'',
        "ArtistWebsite"=>''
        );

        foreach ($xml->Artist as $artist) {
            $ArtistRelevancy=(string) $artist->ItemInfo->Relevancy['index'];
            $ArtistId=(string) $artist['id'];
            $ArtistName=(string) $artist['name'];
            $ArtistRating=(string) $artist['rating'];
            $ArtistFlags=(string) $artist['flags'];
            $ArtistCatzillaID=(string) $artist['catzillaID'];
            $ArtistHotzillaID=(string) $artist['hotzillaID'];
            $ArtistTrackCount=(string) $artist['trackCount'];
            $ArtistSalesGenreCode=(string) $artist['salesGenreCode'];
            $ArtistWebsite=(string) $artist['website'];

            $return[]= array("ArtistRelevancy"=>$ArtistRelevancy,
            "ArtistId"=>$ArtistId,
            "ArtistName"=>$ArtistName,
            "ArtistRating"=>$ArtistRating,
            "ArtistFlags"=>$ArtistFlags,
            "ArtistSalesGenreCode"=>$ArtistSalesGenreCode,
            "ArtistCatzillaID"=>$ArtistCatzillaID,
            "ArtistHotzillaID"=>$ArtistHotzillaID,
            "ArtistTrackCount"=>$ArtistTrackCount,
            "ArtistWebsite"=>$ArtistWebsite
            );
        }
        return $return;
    }

    /**
     * This function gets Artist info
     *
     * @param string $artistId
     * @return array
     */
    public function getArtistInfo($artistId) {
        $artistId=(int) $artistId;
        $artistInfoApi="http://us.music.yahooapis.com/artist/v1/item/$artistId?appid=".$this->appKey;

        $return=array();
        $result=$this->getHtmlCodeViaFopen($artistInfoApi);
        $xml=simplexml_load_string($result);
        $xml=(array) $xml->Artist;
        $xml=array_values($xml);

        if (!is_array($xml) || count($xml)<1) {
            return $return;
        }

        foreach ($xml[0] as $key=>$value){
            $$key=$value;
        }

        $return[]= array("ArtistId"=>$id,
        "ArtistName"=>$name,
        "ArtistRating"=>$rating,
        "ArtistFlags"=>$flags,
        "ArtistSalesGenreCode"=>$salesGenreCode,
        "ArtistWebsite"=>$website,
        "ArtistHotzillaID"=>$hotzillaID,
        "ArtistTrackCount"=>$trackCount,
        "ArtistWebsite"=>$website
        );
        return $return;

    }

    /**
     * This function gets Artist Releases
     *
     * @param string $artistId
     * @return array
     */
    public function getArtistReleases($artistId) {
        $artistId=(int) $artistId;
        $releasesUrl="http://us.music.yahooapis.com/release/v1/list/artist/$artistId?appid=".$this->appKey;

        $return=array();
        $result=$this->getHtmlCodeViaFopen($releasesUrl);
        $xml=simplexml_load_string($result);

        foreach ($xml->Release as $release) {
            $release=(array) $release;
            $release=array_values($release);
            $release=$release[0];
            foreach ($release as $key=>$value){
                $$key=$value;
            }
            $return[]= array("ReleaseId"=>$id,
            "ReleaseTitle"=>$title,
            "ReleaseRating"=>$rating,
            "ArtistFlags"=>$flags,
            "ReleaseRights"=>$rights,
            "ReleaseLabel"=>$label,
            "ReleaseExplicit"=>$explicit,
            "ReleaseTypeId"=>$typeID,
            "ReleaseYear"=>$releaseYear
            );
        }
        return $return;
    }

    /**
     * function to convert and XML object into an array
     *
     * @param xml object $xmlObject
     * @return array
     */
    private function xmlObjectToArray($xmlObject) {
        $return=(array) $xmlObject;
        $return=array_values($return);
        return $return[0];
    }

    /**
     * Function to get the Track Info
     *
     * @param int $trackId
     * @return array
     */
    function getTrackInfo($trackId) {
        $trackId=(int) $trackId;
        $trackURL="http://us.music.yahooapis.com/track/v1/item/$trackId?appid=".$this->appKey;
        $return=array();
        $result=$this->getHtmlCodeViaFopen($trackURL);
        $xml=simplexml_load_string($result);
        foreach ($xml->Track as $tracks) {
            $ret=array();
            // Track Info
            $track=$this->xmlObjectToArray($tracks);
            // Artist Info
            $artist=$this->xmlObjectToArray($tracks->Artist);
            // Release Info
            $release=$this->xmlObjectToArray($tracks->Album->Release);
            // Album Image Info
            if($tracks->Album->Release->Image) {
                foreach ($tracks->Album->Release->Image as $img) {
                    $albumImage[]=$this->xmlObjectToArray($img);
                }
            }
            // Category Info
            foreach ($tracks->Category as $cat) {
                $categoryArray[]=$this->xmlObjectToArray($cat);
            }
            foreach ($track as $key=>$value){ $$key=$value; }
            $ret['TrackInfo']=array("TrackId"=>$id,"TrackRating"=>$rating,"TrackTitle"=>$title,"TrackExplicit"=>$explicit,"TrackDuration"=>$duration,"TrackFlags"=>$flags,"TrackPopularity"=>$popularity,"TrackRights"=>$rights,"TrackDiscNumber"=>$discNumber,"TrackNumber"=>$trackNumber,"TrackLabel"=>$label);
            foreach ($artist as $key=>$value){ $$key=$value; }
            $ret['ArtistInfo']=array("ArtistId"=>$id,"ArtistName"=>$name,"ArtistRating"=>$rating);
            if($release) { foreach ($release as $key=>$value){ $$key=$value; } }
            $ret['AlbumInfo']=array("AlbumId"=>$id,"AlbumTitle"=>$title,"AlbumRating"=>$rating,"AlbumRights"=>$rights);
            $ret['AlbumImage']=$albumImage;
            $ret['CategoryArray']=$categoryArray;
            $return[]=$ret;
        }
        return $return;
    }

    /**
     * Function to search a Track
     *
     * @param string $trackName
     * @return array
     */
    public function searchTrack($trackName){
        $trackName=urlencode($trackName);
        $trackUrl="http://us.music.yahooapis.com/track/v1/list/search/track/$trackName?appid=".$this->appKey;
        $return=array();
        $result=$this->getHtmlCodeViaFopen($trackUrl);
        $xml=simplexml_load_string($result);

        foreach ($xml->Track as $tracks) {
            $ret=array();
            // Track Info
            $track=$this->xmlObjectToArray($tracks);
            // Artist Info
            $artist=$this->xmlObjectToArray($tracks->Artist);
            // Release Info
            $release=$this->xmlObjectToArray($tracks->Album->Release);
            // Album Image Info
            if($tracks->Album->Release->Image) {
                foreach ($tracks->Album->Release->Image as $img) {
                    $albumImage[]=$this->xmlObjectToArray($img);
                }
            }
            // Category Info
            foreach ($tracks->Category as $cat) {
                $categoryArray[]=$this->xmlObjectToArray($cat);
            }
            foreach ($track as $key=>$value){ $$key=$value; }
            $ret['TrackInfo']=array("TrackId"=>$id,"TrackRating"=>$rating,"TrackTitle"=>$title,"TrackExplicit"=>$explicit,"TrackDuration"=>$duration,"TrackFlags"=>$flags,"TrackPopularity"=>$popularity,"TrackRights"=>$rights,"TrackDiscNumber"=>$discNumber,"TrackNumber"=>$trackNumber,"TrackLabel"=>$label);
            foreach ($artist as $key=>$value){ $$key=$value; }
            $ret['ArtistInfo']=array("ArtistId"=>$id,"ArtistName"=>$name,"ArtistRating"=>$rating);
            if($release) { foreach ($release as $key=>$value){ $$key=$value; } }
            $ret['AlbumInfo']=array("AlbumId"=>$id,"AlbumTitle"=>$title,"AlbumRating"=>$rating,"AlbumRights"=>$rights);
            $ret['AlbumImage']=$albumImage;
            $ret['CategoryArray']=$categoryArray;
            $return[]=$ret;
        }
        return $return;
    }

    /**
     * This function gets Track info
     *
     * @param string $artistId
     * @return array
     */
    public function getTracksByArtist($artistId) {
        $artistId=(int) $artistId;        
        $releasesUrl="http://us.music.yahooapis.com/track/v1/list/artist/$artistId?appid=".$this->appKey;
        $return=array();
        $albumImage=array();
        $categoryArray=array();

        $result=$this->getHtmlCodeViaFopen($releasesUrl);
        $xml=simplexml_load_string($result);

        foreach ($xml->Track as $tracks) {
            $ret=array();
            // Track Info
            $track=$this->xmlObjectToArray($tracks);
            // Artist Info
            $artist=$this->xmlObjectToArray($tracks->Artist);
            // Release Info
            $release=$this->xmlObjectToArray($tracks->Album->Release);
            // Album Image Info
            foreach ($tracks->Album->Release->Image as $img) {
                $albumImage[]=$this->xmlObjectToArray($img);
            }
            // Category Info
            foreach ($tracks->Category as $cat) {
                $categoryArray[]=$this->xmlObjectToArray($cat);
            }
            foreach ($track as $key=>$value){ $$key=$value; }
            $ret['TrackInfo']=array("TrackId"=>$id,"TrackRating"=>$rating,"TrackTitle"=>$title,"TrackExplicit"=>$explicit,"TrackDuration"=>$duration,"TrackFlags"=>$flags,"TrackPopularity"=>$popularity,"TrackRights"=>$rights,"TrackDiscNumber"=>$discNumber,"TrackNumber"=>$trackNumber,"TrackLabel"=>$label);
            foreach ($artist as $key=>$value){ $$key=$value; }
            $ret['ArtistInfo']=array("ArtistId"=>$id,"ArtistName"=>$name,"ArtistRating"=>$rating);
            foreach ($release as $key=>$value){ $$key=$value; }
            $ret['AlbumInfo']=array("AlbumId"=>$id,"AlbumTitle"=>$title,"AlbumRating"=>$rating,"AlbumRights"=>$rights);
            $ret['AlbumImage']=$albumImage;
            $ret['CategoryArray']=$categoryArray;
            $return[]=$ret;
        }
        return $return;
    }

    /**
     * Function to get Information of Release / Albums
     *
     * @param int $releaseId
     * @return array
     */
    public function getReleaseInfo($releaseId){
        $releaseId=(int) $releaseId;
        $releaseUrl="http://us.music.yahooapis.com/release/v1/item/$releaseId?appid=".$this->appKey;
        $return=array();
        $result=$this->getHtmlCodeViaFopen($releaseUrl);
        $xml=simplexml_load_string($result);
        
        $release=$this->xmlObjectToArray($xml->Release);
        $artist=$this->xmlObjectToArray($xml->Release->Artist);
        
        $return['Release']=$release;
        $return['Artist']=$artist;
        return $return;
    }
}
?>
Return current item: PHP Yahoo Music API