[ Index ] |
PHP Cross Reference of moodle-2.8 |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * Zend Framework 5 * 6 * LICENSE 7 * 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 [email protected] so we can send you a copy immediately. 15 * 16 * @category Zend 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 21 * @version $Id$ 22 */ 23 24 25 /** 26 * @see Zend_Http_Client 27 */ 28 require_once 'Zend/Http/Client.php'; 29 30 31 /** 32 * @category Zend 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 37 */ 38 class Zend_Service_Audioscrobbler 39 { 40 /** 41 * Zend_Http_Client Object 42 * 43 * @var Zend_Http_Client 44 * @access protected 45 */ 46 protected $_client; 47 48 /** 49 * Array that contains parameters being used by the webservice 50 * 51 * @var array 52 * @access protected 53 */ 54 protected $_params; 55 56 /** 57 * Holds error information (e.g., for handling simplexml_load_string() warnings) 58 * 59 * @var array 60 * @access protected 61 */ 62 protected $_error = null; 63 64 65 /** 66 * Sets up character encoding, instantiates the HTTP client, and assigns the web service version. 67 */ 68 public function __construct() 69 { 70 $this->set('version', '1.0'); 71 72 iconv_set_encoding('output_encoding', 'UTF-8'); 73 iconv_set_encoding('input_encoding', 'UTF-8'); 74 iconv_set_encoding('internal_encoding', 'UTF-8'); 75 } 76 77 /** 78 * Set Http Client 79 * 80 * @param Zend_Http_Client $client 81 */ 82 public function setHttpClient(Zend_Http_Client $client) 83 { 84 $this->_client = $client; 85 } 86 87 /** 88 * Get current http client. 89 * 90 * @return Zend_Http_Client 91 */ 92 public function getHttpClient() 93 { 94 if($this->_client == null) { 95 $this->lazyLoadHttpClient(); 96 } 97 return $this->_client; 98 } 99 100 /** 101 * Lazy load Http Client if none is instantiated yet. 102 * 103 * @return void 104 */ 105 protected function lazyLoadHttpClient() 106 { 107 $this->_client = new Zend_Http_Client(); 108 } 109 110 /** 111 * Returns a field value, or false if the named field does not exist 112 * 113 * @param string $field 114 * @return string|false 115 */ 116 public function get($field) 117 { 118 if (array_key_exists($field, $this->_params)) { 119 return $this->_params[$field]; 120 } else { 121 return false; 122 } 123 } 124 125 /** 126 * Generic set action for a field in the parameters being used 127 * 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 131 */ 132 public function set($field, $value) 133 { 134 $this->_params[$field] = urlencode($value); 135 136 return $this; 137 } 138 139 /** 140 * Protected method that queries REST service and returns SimpleXML response set 141 * 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 147 * @access protected 148 */ 149 protected function _getInfo($service, $params = null) 150 { 151 $service = (string) $service; 152 $params = (string) $params; 153 154 if ($params === '') { 155 $this->getHttpClient()->setUri("http://ws.audioscrobbler.com{$service}"); 156 } else { 157 $this->getHttpClient()->setUri("http://ws.audioscrobbler.com{$service}?{$params}"); 158 } 159 160 $response = $this->getHttpClient()->request(); 161 $responseBody = $response->getBody(); 162 163 if (preg_match('/No such path/', $responseBody)) { 164 /** 165 * @see Zend_Http_Client_Exception 166 */ 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)) { 170 /** 171 * @see Zend_Http_Client_Exception 172 */ 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()) { 176 /** 177 * @see Zend_Http_Client_Exception 178 */ 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()); 181 } 182 183 set_error_handler(array($this, '_errorHandler')); 184 185 if (!$simpleXmlElementResponse = simplexml_load_string($responseBody)) { 186 restore_error_handler(); 187 /** 188 * @see Zend_Service_Exception 189 */ 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; 194 throw $exception; 195 } 196 197 restore_error_handler(); 198 199 return $simpleXmlElementResponse; 200 } 201 202 /** 203 * Utility function to get Audioscrobbler profile information (eg: Name, Gender) 204 * 205 * @return array containing information 206 */ 207 public function userGetProfileInformation() 208 { 209 $service = "/{$this->get('version')}/user/{$this->get('user')}/profile.xml"; 210 return $this->_getInfo($service); 211 } 212 213 /** 214 * Utility function get this user's 50 most played artists 215 * 216 * @return array containing info 217 */ 218 public function userGetTopArtists() 219 { 220 $service = "/{$this->get('version')}/user/{$this->get('user')}/topartists.xml"; 221 return $this->_getInfo($service); 222 } 223 224 /** 225 * Utility function to get this user's 50 most played albums 226 * 227 * @return SimpleXMLElement object containing result set 228 */ 229 public function userGetTopAlbums() 230 { 231 $service = "/{$this->get('version')}/user/{$this->get('user')}/topalbums.xml"; 232 return $this->_getInfo($service); 233 } 234 235 /** 236 * Utility function to get this user's 50 most played tracks 237 * @return SimpleXML object containing resut set 238 */ 239 public function userGetTopTracks() 240 { 241 $service = "/{$this->get('version')}/user/{$this->get('user')}/toptracks.xml"; 242 return $this->_getInfo($service); 243 } 244 245 /** 246 * Utility function to get this user's 50 most used tags 247 * 248 * @return SimpleXMLElement object containing result set 249 */ 250 public function userGetTopTags() 251 { 252 $service = "/{$this->get('version')}/user/{$this->get('user')}/tags.xml"; 253 return $this->_getInfo($service); 254 } 255 256 /** 257 * Utility function that returns the user's top tags used most used on a specific artist 258 * 259 * @return SimpleXMLElement object containing result set 260 */ 261 public function userGetTopTagsForArtist() 262 { 263 $service = "/{$this->get('version')}/user/{$this->get('user')}/artisttags.xml"; 264 $params = "artist={$this->get('artist')}"; 265 return $this->_getInfo($service, $params); 266 } 267 268 /** 269 * Utility function that returns this user's top tags for an album 270 * 271 * @return SimpleXMLElement object containing result set 272 */ 273 public function userGetTopTagsForAlbum() 274 { 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); 278 } 279 280 /** 281 * Utility function that returns this user's top tags for a track 282 * 283 * @return SimpleXMLElement object containing result set 284 */ 285 public function userGetTopTagsForTrack() 286 { 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); 290 } 291 292 /** 293 * Utility function that retrieves this user's list of friends 294 * @return SimpleXMLElement object containing result set 295 */ 296 public function userGetFriends() 297 { 298 $service = "/{$this->get('version')}/user/{$this->get('user')}/friends.xml"; 299 return $this->_getInfo($service); 300 } 301 302 /** 303 * Utility function that returns a list of people with similar listening preferences to this user 304 * 305 * @return SimpleXMLElement object containing result set 306 */ 307 public function userGetNeighbours() 308 { 309 $service = "/{$this->get('version')}/user/{$this->get('user')}/neighbours.xml"; 310 return $this->_getInfo($service); 311 } 312 313 /** 314 * Utility function that returns a list of the 10 most recent tracks played by this user 315 * 316 * @return SimpleXMLElement object containing result set 317 */ 318 public function userGetRecentTracks() 319 { 320 $service = "/{$this->get('version')}/user/{$this->get('user')}/recenttracks.xml"; 321 return $this->_getInfo($service); 322 } 323 324 /** 325 * Utility function that returns a list of the 10 tracks most recently banned by this user 326 * 327 * @return SimpleXMLElement object containing result set 328 */ 329 public function userGetRecentBannedTracks() 330 { 331 $service = "/{$this->get('version')}/user/{$this->get('user')}/recentbannedtracks.xml"; 332 return $this->_getInfo($service); 333 } 334 335 /** 336 * Utility function that returns a list of the 10 tracks most recently loved by this user 337 * 338 * @return SimpleXMLElement object containing result set 339 */ 340 public function userGetRecentLovedTracks() 341 { 342 $service = "/{$this->get('version')}/user/{$this->get('user')}/recentlovedtracks.xml"; 343 return $this->_getInfo($service); 344 } 345 346 /** 347 * Utility function that returns a list of dates of available weekly charts for a this user 348 * 349 * Should actually be named userGetWeeklyChartDateList() but we have to follow audioscrobbler's naming 350 * 351 * @return SimpleXMLElement object containing result set 352 */ 353 public function userGetWeeklyChartList() 354 { 355 $service = "/{$this->get('version')}/user/{$this->get('user')}/weeklychartlist.xml"; 356 return $this->_getInfo($service); 357 } 358 359 360 /** 361 * Utility function that returns weekly album chart data for this user 362 * 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 366 */ 367 public function userGetWeeklyAlbumChart($from = NULL, $to = NULL) 368 { 369 $params = ""; 370 371 if ($from != NULL && $to != NULL) { 372 $from = (int)$from; 373 $to = (int)$to; 374 $params = "from={$from}&to={$to}"; 375 } 376 377 $service = "/{$this->get('version')}/user/{$this->get('user')}/weeklyalbumchart.xml"; 378 return $this->_getInfo($service, $params); 379 } 380 381 /** 382 * Utility function that returns weekly artist chart data for this user 383 * 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 387 */ 388 public function userGetWeeklyArtistChart($from = NULL, $to = NULL) 389 { 390 $params = ""; 391 392 if ($from != NULL && $to != NULL) { 393 $from = (int)$from; 394 $to = (int)$to; 395 $params = "from={$from}&to={$to}"; 396 } 397 398 $service = "/{$this->get('version')}/user/{$this->get('user')}/weeklyartistchart.xml"; 399 return $this->_getInfo($service, $params); 400 } 401 402 /** 403 * Utility function that returns weekly track chart data for this user 404 * 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 408 */ 409 public function userGetWeeklyTrackChart($from = NULL, $to = NULL) 410 { 411 $params = ""; 412 413 if ($from != NULL && $to != NULL) { 414 $from = (int)$from; 415 $to = (int)$to; 416 $params = "from={$from}&to={$to}"; 417 } 418 419 $service = "/{$this->get('version')}/user/{$this->get('user')}/weeklytrackchart.xml"; 420 return $this->_getInfo($service, $params); 421 } 422 423 424 /** 425 * Utility function that returns a list of artists similiar to this artist 426 * 427 * @return SimpleXMLElement object containing result set 428 */ 429 public function artistGetRelatedArtists() 430 { 431 $service = "/{$this->get('version')}/artist/{$this->get('artist')}/similar.xml"; 432 return $this->_getInfo($service); 433 } 434 435 /** 436 * Utility function that returns a list of this artist's top listeners 437 * 438 * @return SimpleXMLElement object containing result set 439 */ 440 public function artistGetTopFans() 441 { 442 $service = "/{$this->get('version')}/artist/{$this->get('artist')}/fans.xml"; 443 return $this->_getInfo($service); 444 } 445 446 /** 447 * Utility function that returns a list of this artist's top-rated tracks 448 * 449 * @return SimpleXMLElement object containing result set 450 */ 451 public function artistGetTopTracks() 452 { 453 $service = "/{$this->get('version')}/artist/{$this->get('artist')}/toptracks.xml"; 454 return $this->_getInfo($service); 455 } 456 457 /** 458 * Utility function that returns a list of this artist's top-rated albums 459 * 460 * @return SimpleXMLElement object containing result set 461 */ 462 public function artistGetTopAlbums() 463 { 464 $service = "/{$this->get('version')}/artist/{$this->get('artist')}/topalbums.xml"; 465 return $this->_getInfo($service); 466 } 467 468 /** 469 * Utility function that returns a list of this artist's top-rated tags 470 * 471 * @return SimpleXMLElement object containing result set 472 */ 473 public function artistGetTopTags() 474 { 475 $service = "/{$this->get('version')}/artist/{$this->get('artist')}/toptags.xml"; 476 return $this->_getInfo($service); 477 } 478 479 480 /** 481 * Get information about an album 482 * 483 * @return SimpleXMLElement 484 */ 485 public function albumGetInfo() 486 { 487 $service = "/{$this->get('version')}/album/{$this->get('artist')}/{$this->get('album')}/info.xml"; 488 return $this->_getInfo($service); 489 } 490 491 /** 492 * Get top fans of the current track. 493 * 494 * @return SimpleXMLElement 495 */ 496 public function trackGetTopFans() 497 { 498 $service = "/{$this->get('version')}/track/{$this->get('artist')}/{$this->get('track')}/fans.xml"; 499 return $this->_getInfo($service); 500 } 501 502 /** 503 * Get top tags of the current track. 504 * 505 * @return SimpleXMLElement 506 */ 507 public function trackGetTopTags() 508 { 509 $service = "/{$this->get('version')}/track/{$this->get('artist')}/{$this->get('track')}/toptags.xml"; 510 return $this->_getInfo($service); 511 } 512 513 /** 514 * Get Top Tags. 515 * 516 * @return SimpleXMLElement 517 */ 518 public function tagGetTopTags() 519 { 520 $service = "/{$this->get('version')}/tag/toptags.xml"; 521 return $this->_getInfo($service); 522 } 523 524 /** 525 * Get top albums by current tag. 526 * 527 * @return SimpleXMLElement 528 */ 529 public function tagGetTopAlbums() 530 { 531 $service = "/{$this->get('version')}/tag/{$this->get('tag')}/topalbums.xml"; 532 return $this->_getInfo($service); 533 } 534 535 /** 536 * Get top artists by current tag. 537 * 538 * @return SimpleXMLElement 539 */ 540 public function tagGetTopArtists() 541 { 542 $service = "/{$this->get('version')}/tag/{$this->get('tag')}/topartists.xml"; 543 return $this->_getInfo($service); 544 } 545 546 /** 547 * Get Top Tracks by currently set tag. 548 * 549 * @return SimpleXMLElement 550 */ 551 public function tagGetTopTracks() 552 { 553 $service = "/{$this->get('version')}/tag/{$this->get('tag')}/toptracks.xml"; 554 return $this->_getInfo($service); 555 } 556 557 /** 558 * Get weekly chart list by current set group. 559 * 560 * @see set() 561 * @return SimpleXMLElement 562 */ 563 public function groupGetWeeklyChartList() 564 { 565 $service = "/{$this->get('version')}/group/{$this->get('group')}/weeklychartlist.xml"; 566 return $this->_getInfo($service); 567 } 568 569 /** 570 * Retrieve weekly Artist Charts 571 * 572 * @param int $from 573 * @param int $to 574 * @return SimpleXMLElement 575 */ 576 public function groupGetWeeklyArtistChartList($from = NULL, $to = NULL) 577 { 578 579 if ($from != NULL && $to != NULL) { 580 $from = (int)$from; 581 $to = (int)$to; 582 $params = "from={$from}&$to={$to}"; 583 } else { 584 $params = ""; 585 } 586 587 $service = "/{$this->get('version')}/group/{$this->get('group')}/weeklyartistchart.xml"; 588 return $this->_getInfo($service, $params); 589 } 590 591 /** 592 * Retrieve Weekly Track Charts 593 * 594 * @param int $from 595 * @param int $to 596 * @return SimpleXMLElement 597 */ 598 public function groupGetWeeklyTrackChartList($from = NULL, $to = NULL) 599 { 600 if ($from != NULL && $to != NULL) { 601 $from = (int)$from; 602 $to = (int)$to; 603 $params = "from={$from}&to={$to}"; 604 } else { 605 $params = ""; 606 } 607 608 $service = "/{$this->get('version')}/group/{$this->get('group')}/weeklytrackchart.xml"; 609 return $this->_getInfo($service, $params); 610 } 611 612 /** 613 * Retrieve Weekly album charts. 614 * 615 * @param int $from 616 * @param int $to 617 * @return SimpleXMLElement 618 */ 619 public function groupGetWeeklyAlbumChartList($from = NULL, $to = NULL) 620 { 621 if ($from != NULL && $to != NULL) { 622 $from = (int)$from; 623 $to = (int)$to; 624 $params = "from={$from}&to={$to}"; 625 } else { 626 $params = ""; 627 } 628 629 $service = "/{$this->get('version')}/group/{$this->get('group')}/weeklyalbumchart.xml"; 630 return $this->_getInfo($service, $params); 631 } 632 633 /** 634 * Saves the provided error information to this instance 635 * 636 * @param integer $errno 637 * @param string $errstr 638 * @param string $errfile 639 * @param integer $errline 640 * @param array $errcontext 641 * @return void 642 */ 643 protected function _errorHandler($errno, $errstr, $errfile, $errline, array $errcontext) 644 { 645 $this->_error = array( 646 'errno' => $errno, 647 'errstr' => $errstr, 648 'errfile' => $errfile, 649 'errline' => $errline, 650 'errcontext' => $errcontext 651 ); 652 } 653 654 /** 655 * Call Intercept for set($name, $field) 656 * 657 * @param string $method 658 * @param array $args 659 * @return Zend_Service_Audioscrobbler 660 */ 661 public function __call($method, $args) 662 { 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." 667 ); 668 } 669 $field = strtolower(substr($method, 3)); 670 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." 675 ); 676 } 677 $this->set($field, $args[0]); 678 679 return $this; 680 } 681 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 20:29:05 2014 | Cross-referenced by PHPXref 0.7.1 |