Source for file SocketCaller.php

Documentation is available at SocketCaller.php

  1. <?
  2.  
  3. /** Calls API methods using REST requests using PHP sockets.
  4.  *
  5.  * @package    php-lastfm-api
  6.  * @author  Felix Bruns <felixbruns@web.de>
  7.  * @version    1.0
  8.  */
  9. final class SocketCaller extends Caller {
  10.     /** A SocketCaller instance.
  11.      *
  12.      * @var SocketCaller 
  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 SocketCaller();
  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    private
  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_query($params'''&');
  69.  
  70.             /* Extract URL information. */
  71.             $info parse_url(self::API_URL);
  72.  
  73.             /* TODO: Accept-Encoding: deflate, gzip */
  74.             /* Set request data. */
  75.             if($requestMethod === 'POST'){
  76.                 $data  "POST " $info['path'" HTTP/1.1\r\n";
  77.                 $data .= "Host: "$info['host'."\r\n";
  78.                 $data .= "User-Agent: PHP last.fm API (PHP/" phpversion(")\r\n";
  79.                 $data .= "Content-Type: application/x-www-form-urlencoded\r\n";
  80.                 $data .= "Content-Length: " strlen($query."\r\n";
  81.                 $data .= "Connection: Close\r\n\r\n";
  82.                 $data .= $query;
  83.             }
  84.             else{
  85.                 $data  "GET " $info['path'"?" $query ." HTTP/1.1\r\n";
  86.                 $data .= "Host: "$info['host'."\r\n";
  87.                 $data .= "User-Agent: PHP last.fm API (PHP/" phpversion(")\r\n";
  88.                 $data .= "Connection: Close\r\n\r\n";
  89.             }
  90.  
  91.             /* Open socket. */
  92.             $socket fsockopen($info['host']80);
  93.  
  94.             /* Write request. */
  95.             fwrite($socket$data);
  96.  
  97.             /* Clear response headers. */
  98.             $this->headers array();
  99.  
  100.             /* Read headers. */
  101.             while(($line fgets($socket)) !== false && $line != "\r\n"){
  102.                 $this->header($line);
  103.             }
  104.  
  105.             /* Read response. */
  106.             $response "";
  107.             while(($line fgets($socket)) !== false && $line != "\r\n"){
  108.                 $response .= $line;
  109.             }
  110.  
  111.             /* Close socket. */
  112.             fclose($socket);
  113.  
  114.             /* Cache it. */
  115.             if($this->cache != null){
  116.                 if(array_key_exists('Expires'$this->headers)){
  117.                     $this->cache->store(
  118.                         $hash$response,
  119.                         strtotime($this->headers['Expires'])
  120.                     );
  121.                 }
  122.                 else{
  123.                     $expiration $this->cache->getPolicy()->getExpirationTime($params);
  124.  
  125.                     if($expiration 0){
  126.                         $this->cache->store($hash$responsetime($expiration);
  127.                     }
  128.                 }
  129.             }
  130.         }
  131.  
  132.         /* Create SimpleXMLElement from response. */
  133.         $response new SimpleXMLElement($response);
  134.  
  135.         /* Return response or throw an error. */
  136.         if(Util::toString($response['status']=== 'ok'){
  137.             if($response->children()->{0}){
  138.                 return $response->children()->{0};
  139.             }
  140.         }
  141.         else{
  142.             throw new Error(
  143.                 Util::toString($response->error),
  144.                 Util::toInteger($response->error['code'])
  145.             );
  146.         }
  147.     }
  148.  
  149.     /** Header callback.
  150.      *
  151.      * @param    string    $header    A HTTP response header.
  152.      *
  153.      * @access    private
  154.      * @internal
  155.      */
  156.     private function header($header){
  157.         $parts explode(': '$header2);
  158.  
  159.         if(count($parts== 2){
  160.             list($key$value$parts;
  161.  
  162.             $this->headers[$keytrim($value);
  163.         }
  164.  
  165.         return strlen($header);
  166.     }
  167. }
  168.  
  169. ?>

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