8 * This source file is subject to the new BSD license that is bundled
9 * with this package in the file LICENSE.txt.
10 * It is also available through the world-wide-web at this URL:
11 * http://framework.zend.com/license/new-bsd
12 * If you did not receive a copy of the license and are unable to
13 * obtain it through the world-wide-web, please send an email
14 * to license@zend.com so we can send you a copy immediately.
17 * @package Zend_Service
18 * @subpackage Audioscrobbler
19 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
20 * @license http://framework.zend.com/license/new-bsd New BSD License
26 * @see Zend_Http_Client
28 require_once 'Zend/Http/Client.php';
33 * @package Zend_Service
34 * @subpackage Audioscrobbler
35 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
36 * @license http://framework.zend.com/license/new-bsd New BSD License
38 class Zend_Service_Audioscrobbler
41 * Zend_Http_Client Object
43 * @var Zend_Http_Client
49 * Array that contains parameters being used by the webservice
57 * Holds error information (e.g., for handling simplexml_load_string() warnings)
62 protected $_error = null;
66 * Sets up character encoding, instantiates the HTTP client, and assigns the web service version.
68 public function __construct()
70 $this->set('version', '1.0');
72 iconv_set_encoding('output_encoding', 'UTF-8');
73 iconv_set_encoding('input_encoding', 'UTF-8');
74 iconv_set_encoding('internal_encoding', 'UTF-8');
80 * @param Zend_Http_Client $client
82 public function setHttpClient(Zend_Http_Client
$client)
84 $this->_client
= $client;
88 * Get current http client.
90 * @return Zend_Http_Client
92 public function getHttpClient()
94 if($this->_client
== null) {
95 $this->lazyLoadHttpClient();
97 return $this->_client
;
101 * Lazy load Http Client if none is instantiated yet.
105 protected function lazyLoadHttpClient()
107 $this->_client
= new Zend_Http_Client();
111 * Returns a field value, or false if the named field does not exist
113 * @param string $field
114 * @return string|false
116 public function get($field)
118 if (array_key_exists($field, $this->_params
)) {
119 return $this->_params
[$field];
126 * Generic set action for a field in the parameters being used
128 * @param string $field name of field to set
129 * @param string $value value to assign to the named field
130 * @return Zend_Service_Audioscrobbler Provides a fluent interface
132 public function set($field, $value)
134 $this->_params
[$field] = urlencode($value);
140 * Protected method that queries REST service and returns SimpleXML response set
142 * @param string $service name of Audioscrobbler service file we're accessing
143 * @param string $params parameters that we send to the service if needded
144 * @throws Zend_Http_Client_Exception
145 * @throws Zend_Service_Exception
146 * @return SimpleXMLElement result set
149 protected function _getInfo($service, $params = null)
151 $service = (string) $service;
152 $params = (string) $params;
154 if ($params === '') {
155 $this->getHttpClient()->setUri("http://ws.audioscrobbler.com{$service}");
157 $this->getHttpClient()->setUri("http://ws.audioscrobbler.com{$service}?{$params}");
160 $response = $this->getHttpClient()->request();
161 $responseBody = $response->getBody();
163 if (preg_match('/No such path/', $responseBody)) {
165 * @see Zend_Http_Client_Exception
167 require_once 'Zend/Http/Client/Exception.php';
168 throw new Zend_Http_Client_Exception('Could not find: ' . $this->_client
->getUri());
169 } elseif (preg_match('/No user exists with this name/', $responseBody)) {
171 * @see Zend_Http_Client_Exception
173 require_once 'Zend/Http/Client/Exception.php';
174 throw new Zend_Http_Client_Exception('No user exists with this name');
175 } elseif (!$response->isSuccessful()) {
177 * @see Zend_Http_Client_Exception
179 require_once 'Zend/Http/Client/Exception.php';
180 throw new Zend_Http_Client_Exception('The web service ' . $this->_client
->getUri() . ' returned the following status code: ' . $response->getStatus());
183 set_error_handler(array($this, '_errorHandler'));
185 if (!$simpleXmlElementResponse = simplexml_load_string($responseBody)) {
186 restore_error_handler();
188 * @see Zend_Service_Exception
190 require_once 'Zend/Service/Exception.php';
191 $exception = new Zend_Service_Exception('Response failed to load with SimpleXML');
192 $exception->error
= $this->_error
;
193 $exception->response
= $responseBody;
197 restore_error_handler();
199 return $simpleXmlElementResponse;
203 * Utility function to get Audioscrobbler profile information (eg: Name, Gender)
205 * @return array containing information
207 public function userGetProfileInformation()
209 $service = "/{$this->get('version')}/user/{$this->get('user')}/profile.xml";
210 return $this->_getInfo($service);
214 * Utility function get this user's 50 most played artists
216 * @return array containing info
218 public function userGetTopArtists()
220 $service = "/{$this->get('version')}/user/{$this->get('user')}/topartists.xml";
221 return $this->_getInfo($service);
225 * Utility function to get this user's 50 most played albums
227 * @return SimpleXMLElement object containing result set
229 public function userGetTopAlbums()
231 $service = "/{$this->get('version')}/user/{$this->get('user')}/topalbums.xml";
232 return $this->_getInfo($service);
236 * Utility function to get this user's 50 most played tracks
237 * @return SimpleXML object containing resut set
239 public function userGetTopTracks()
241 $service = "/{$this->get('version')}/user/{$this->get('user')}/toptracks.xml";
242 return $this->_getInfo($service);
246 * Utility function to get this user's 50 most used tags
248 * @return SimpleXMLElement object containing result set
250 public function userGetTopTags()
252 $service = "/{$this->get('version')}/user/{$this->get('user')}/tags.xml";
253 return $this->_getInfo($service);
257 * Utility function that returns the user's top tags used most used on a specific artist
259 * @return SimpleXMLElement object containing result set
261 public function userGetTopTagsForArtist()
263 $service = "/{$this->get('version')}/user/{$this->get('user')}/artisttags.xml";
264 $params = "artist={$this->get('artist')}";
265 return $this->_getInfo($service, $params);
269 * Utility function that returns this user's top tags for an album
271 * @return SimpleXMLElement object containing result set
273 public function userGetTopTagsForAlbum()
275 $service = "/{$this->get('version')}/user/{$this->get('user')}/albumtags.xml";
276 $params = "artist={$this->get('artist')}&album={$this->get('album')}";
277 return $this->_getInfo($service, $params);
281 * Utility function that returns this user's top tags for a track
283 * @return SimpleXMLElement object containing result set
285 public function userGetTopTagsForTrack()
287 $service = "/{$this->get('version')}/user/{$this->get('user')}/tracktags.xml";
288 $params = "artist={$this->get('artist')}&track={$this->get('track')}";
289 return $this->_getInfo($service, $params);
293 * Utility function that retrieves this user's list of friends
294 * @return SimpleXMLElement object containing result set
296 public function userGetFriends()
298 $service = "/{$this->get('version')}/user/{$this->get('user')}/friends.xml";
299 return $this->_getInfo($service);
303 * Utility function that returns a list of people with similar listening preferences to this user
305 * @return SimpleXMLElement object containing result set
307 public function userGetNeighbours()
309 $service = "/{$this->get('version')}/user/{$this->get('user')}/neighbours.xml";
310 return $this->_getInfo($service);
314 * Utility function that returns a list of the 10 most recent tracks played by this user
316 * @return SimpleXMLElement object containing result set
318 public function userGetRecentTracks()
320 $service = "/{$this->get('version')}/user/{$this->get('user')}/recenttracks.xml";
321 return $this->_getInfo($service);
325 * Utility function that returns a list of the 10 tracks most recently banned by this user
327 * @return SimpleXMLElement object containing result set
329 public function userGetRecentBannedTracks()
331 $service = "/{$this->get('version')}/user/{$this->get('user')}/recentbannedtracks.xml";
332 return $this->_getInfo($service);
336 * Utility function that returns a list of the 10 tracks most recently loved by this user
338 * @return SimpleXMLElement object containing result set
340 public function userGetRecentLovedTracks()
342 $service = "/{$this->get('version')}/user/{$this->get('user')}/recentlovedtracks.xml";
343 return $this->_getInfo($service);
347 * Utility function that returns a list of dates of available weekly charts for a this user
349 * Should actually be named userGetWeeklyChartDateList() but we have to follow audioscrobbler's naming
351 * @return SimpleXMLElement object containing result set
353 public function userGetWeeklyChartList()
355 $service = "/{$this->get('version')}/user/{$this->get('user')}/weeklychartlist.xml";
356 return $this->_getInfo($service);
361 * Utility function that returns weekly album chart data for this user
363 * @param integer $from optional UNIX timestamp for start of date range
364 * @param integer $to optional UNIX timestamp for end of date range
365 * @return SimpleXMLElement object containing result set
367 public function userGetWeeklyAlbumChart($from = NULL, $to = NULL)
371 if ($from != NULL && $to != NULL) {
374 $params = "from={$from}&to={$to}";
377 $service = "/{$this->get('version')}/user/{$this->get('user')}/weeklyalbumchart.xml";
378 return $this->_getInfo($service, $params);
382 * Utility function that returns weekly artist chart data for this user
384 * @param integer $from optional UNIX timestamp for start of date range
385 * @param integer $to optional UNIX timestamp for end of date range
386 * @return SimpleXMLElement object containing result set
388 public function userGetWeeklyArtistChart($from = NULL, $to = NULL)
392 if ($from != NULL && $to != NULL) {
395 $params = "from={$from}&to={$to}";
398 $service = "/{$this->get('version')}/user/{$this->get('user')}/weeklyartistchart.xml";
399 return $this->_getInfo($service, $params);
403 * Utility function that returns weekly track chart data for this user
405 * @param integer $from optional UNIX timestamp for start of date range
406 * @param integer $to optional UNIX timestamp for end of date range
407 * @return SimpleXMLElement object containing result set
409 public function userGetWeeklyTrackChart($from = NULL, $to = NULL)
413 if ($from != NULL && $to != NULL) {
416 $params = "from={$from}&to={$to}";
419 $service = "/{$this->get('version')}/user/{$this->get('user')}/weeklytrackchart.xml";
420 return $this->_getInfo($service, $params);
425 * Utility function that returns a list of artists similiar to this artist
427 * @return SimpleXMLElement object containing result set
429 public function artistGetRelatedArtists()
431 $service = "/{$this->get('version')}/artist/{$this->get('artist')}/similar.xml";
432 return $this->_getInfo($service);
436 * Utility function that returns a list of this artist's top listeners
438 * @return SimpleXMLElement object containing result set
440 public function artistGetTopFans()
442 $service = "/{$this->get('version')}/artist/{$this->get('artist')}/fans.xml";
443 return $this->_getInfo($service);
447 * Utility function that returns a list of this artist's top-rated tracks
449 * @return SimpleXMLElement object containing result set
451 public function artistGetTopTracks()
453 $service = "/{$this->get('version')}/artist/{$this->get('artist')}/toptracks.xml";
454 return $this->_getInfo($service);
458 * Utility function that returns a list of this artist's top-rated albums
460 * @return SimpleXMLElement object containing result set
462 public function artistGetTopAlbums()
464 $service = "/{$this->get('version')}/artist/{$this->get('artist')}/topalbums.xml";
465 return $this->_getInfo($service);
469 * Utility function that returns a list of this artist's top-rated tags
471 * @return SimpleXMLElement object containing result set
473 public function artistGetTopTags()
475 $service = "/{$this->get('version')}/artist/{$this->get('artist')}/toptags.xml";
476 return $this->_getInfo($service);
481 * Get information about an album
483 * @return SimpleXMLElement
485 public function albumGetInfo()
487 $service = "/{$this->get('version')}/album/{$this->get('artist')}/{$this->get('album')}/info.xml";
488 return $this->_getInfo($service);
492 * Get top fans of the current track.
494 * @return SimpleXMLElement
496 public function trackGetTopFans()
498 $service = "/{$this->get('version')}/track/{$this->get('artist')}/{$this->get('track')}/fans.xml";
499 return $this->_getInfo($service);
503 * Get top tags of the current track.
505 * @return SimpleXMLElement
507 public function trackGetTopTags()
509 $service = "/{$this->get('version')}/track/{$this->get('artist')}/{$this->get('track')}/toptags.xml";
510 return $this->_getInfo($service);
516 * @return SimpleXMLElement
518 public function tagGetTopTags()
520 $service = "/{$this->get('version')}/tag/toptags.xml";
521 return $this->_getInfo($service);
525 * Get top albums by current tag.
527 * @return SimpleXMLElement
529 public function tagGetTopAlbums()
531 $service = "/{$this->get('version')}/tag/{$this->get('tag')}/topalbums.xml";
532 return $this->_getInfo($service);
536 * Get top artists by current tag.
538 * @return SimpleXMLElement
540 public function tagGetTopArtists()
542 $service = "/{$this->get('version')}/tag/{$this->get('tag')}/topartists.xml";
543 return $this->_getInfo($service);
547 * Get Top Tracks by currently set tag.
549 * @return SimpleXMLElement
551 public function tagGetTopTracks()
553 $service = "/{$this->get('version')}/tag/{$this->get('tag')}/toptracks.xml";
554 return $this->_getInfo($service);
558 * Get weekly chart list by current set group.
561 * @return SimpleXMLElement
563 public function groupGetWeeklyChartList()
565 $service = "/{$this->get('version')}/group/{$this->get('group')}/weeklychartlist.xml";
566 return $this->_getInfo($service);
570 * Retrieve weekly Artist Charts
574 * @return SimpleXMLElement
576 public function groupGetWeeklyArtistChartList($from = NULL, $to = NULL)
579 if ($from != NULL && $to != NULL) {
582 $params = "from={$from}&$to={$to}";
587 $service = "/{$this->get('version')}/group/{$this->get('group')}/weeklyartistchart.xml";
588 return $this->_getInfo($service, $params);
592 * Retrieve Weekly Track Charts
596 * @return SimpleXMLElement
598 public function groupGetWeeklyTrackChartList($from = NULL, $to = NULL)
600 if ($from != NULL && $to != NULL) {
603 $params = "from={$from}&to={$to}";
608 $service = "/{$this->get('version')}/group/{$this->get('group')}/weeklytrackchart.xml";
609 return $this->_getInfo($service, $params);
613 * Retrieve Weekly album charts.
617 * @return SimpleXMLElement
619 public function groupGetWeeklyAlbumChartList($from = NULL, $to = NULL)
621 if ($from != NULL && $to != NULL) {
624 $params = "from={$from}&to={$to}";
629 $service = "/{$this->get('version')}/group/{$this->get('group')}/weeklyalbumchart.xml";
630 return $this->_getInfo($service, $params);
634 * Saves the provided error information to this instance
636 * @param integer $errno
637 * @param string $errstr
638 * @param string $errfile
639 * @param integer $errline
640 * @param array $errcontext
643 protected function _errorHandler($errno, $errstr, $errfile, $errline, array $errcontext)
645 $this->_error
= array(
648 'errfile' => $errfile,
649 'errline' => $errline,
650 'errcontext' => $errcontext
655 * Call Intercept for set($name, $field)
657 * @param string $method
659 * @return Zend_Service_Audioscrobbler
661 public function __call($method, $args)
663 if(substr($method, 0, 3) !== "set") {
664 require_once "Zend/Service/Exception.php";
665 throw new Zend_Service_Exception(
666 "Method ".$method." does not exist in class Zend_Service_Audioscrobbler."
669 $field = strtolower(substr($method, 3));
671 if(!is_array($args) ||
count($args) != 1) {
672 require_once "Zend/Service/Exception.php";
673 throw new Zend_Service_Exception(
674 "A value is required for setting a parameter field."
677 $this->set($field, $args[0]);