Source for file PeclCaller.php

Documentation is available at PeclCaller.php

  1. <?
  2.  
  3. /** Calls API methods using REST requests using PECL HTTP.
  4.  *
  5.  * @package    php-lastfm-api
  6.  * @author  Felix Bruns <felixbruns@web.de>
  7.  * @version    1.0
  8.  */
  9. final class PeclCaller extends Caller {
  10.     /** A PeclCaller instance.
  11.      *
  12.      * @var PeclCaller 
  13.      * @access    protected
  14.      */
  15.     private static $instance;
  16.  
  17.     /** An array of response headers.
  18.      *
  19.      * @var array 
  20.      * @access    private
  21.      */
  22.     private $headers;
  23.  
  24.     /** Private constructor.
  25.      *
  26.      * @access    private
  27.      */
  28.     private function __construct(){
  29.         $this->cache = new DiskCache();
  30.     }
  31.  
  32.     /** Get a Caller instance.
  33.      *
  34.      * @return    Caller    A Caller instance.
  35.      * @static
  36.      * @access    public
  37.      */
  38.     public static function getInstance(){
  39.         if(!is_object(self::$instance)){
  40.             self::$instance new PeclCaller();
  41.         }
  42.  
  43.         return self::$instance;
  44.     }
  45.  
  46.     /** Send a query using a specified request-method.
  47.      *
  48.      * @param    string    $query            Query to send. (Required)
  49.      * @param    string    $requestMethod    Request-method for calling (defaults to 'GET'). (Optional)
  50.      * @return    SimpleXMLElement        A SimpleXMLElement object.
  51.      *
  52.      * @access    protected
  53.      * @internal
  54.      */
  55.     protected function internalCall($params$requestMethod 'GET'){
  56.         /* Create caching hash. */
  57.         $hash Cache::createHash($params);
  58.  
  59.         /* Check if response is cached. */
  60.         if($this->cache != null &&
  61.             $this->cache->contains($hash&&
  62.             !$this->cache->isExpired($hash)){
  63.             /* Get cached response. */
  64.             $response $this->cache->load($hash);
  65.         }
  66.         else{
  67.             /* Build request query. */
  68.             $query http_build_str($params'''&');
  69.  
  70.             /* Set request options. */
  71.             $options array(
  72.                 'useragent' => 'PHP last.fm API (PHP/' phpversion(')'
  73.             );
  74.  
  75.             /* Clear response headers. */
  76.             $this->headers array();
  77.  
  78.             /* Get response */
  79.             if($requestMethod === 'POST'){
  80.                 $response http_post_data(self::API_URL$query$options$info);
  81.             }
  82.             else{
  83.                 $response http_get(self::API_URL '?' $query$options$info);
  84.             }
  85.  
  86.             $response http_parse_message($response);
  87.  
  88.             foreach($response->headers as $header => $value){
  89.                 $this->headers[$header$value;
  90.             }
  91.             
  92.             $response $response->body;
  93.  
  94.             /* Cache it. */
  95.             if($this->cache != null){
  96.                 if(array_key_exists('Expires'$this->headers)){
  97.                     $this->cache->store(
  98.                         $hash$response,
  99.                         strtotime($this->headers['Expires'])
  100.                     );
  101.                 }
  102.                 else{
  103.                     $expiration $this->cache->getPolicy()->getExpirationTime($params);
  104.  
  105.                     if($expiration 0){
  106.                         $this->cache->store($hash$responsetime($expiration);
  107.                     }
  108.                 }
  109.             }
  110.         }
  111.  
  112.         /* Create SimpleXMLElement from response. */
  113.         $response new SimpleXMLElement($response);
  114.  
  115.         /* Return response or throw an error. */
  116.         if(Util::toString($response['status']=== 'ok'){
  117.             if($response->children()->{0}){
  118.                 return $response->children()->{0};
  119.             }
  120.         }
  121.         else{
  122.             throw new Error(
  123.                 Util::toString($response->error),
  124.                 Util::toInteger($response->error['code'])
  125.             );
  126.         }
  127.     }
  128. }
  129.  
  130. ?>

Documentation generated on Mon, 22 Dec 2008 16:57:41 +0100 by phpDocumentor 1.4.1