Services_Digg
[ class tree: Services_Digg ] [ index: Services_Digg ] [ all elements ]

Source for file Proxy.php

Documentation is available at Proxy.php

  1. <?php
  2.  
  3. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  4.  
  5. /**
  6.  * A proxy for Digg's API
  7.  *
  8.  * PHP version 5.1.0+
  9.  *
  10.  * LICENSE: This source file is subject to version 3.0 of the PHP license
  11.  * that is available through the world-wide-web at the following URI:
  12.  * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
  13.  * the PHP License and are unable to obtain it through the web, please
  14.  * send a note to license@php.net so we can mail you a copy immediately.
  15.  *
  16.  * @category    Services
  17.  * @package     Services_Digg
  18.  * @author      Joe Stump <joe@joestump.net>
  19.  * @copyright   1997-2007 The PHP Group
  20.  * @license     http://www.php.net/license/3_0.txt  PHP License 3.0
  21.  * @version     CVS: $Id:$
  22.  * @link        http://pear.php.net/package/Services_Digg
  23.  */
  24.  
  25. require_once 'Services/Digg.php';
  26. require_once 'Services/Digg/Exception.php';
  27.  
  28. /**
  29.  * Services_Digg_Proxy
  30.  *
  31.  * This allows you to make JSON requests to the Digg API from your own website.
  32.  * Create a PHP script like the one below and then request it in the following
  33.  * manner:
  34.  *
  35.  * http://www.example.com/myproxy.php?endPoint=/errors
  36.  *
  37.  * Replace the endPoint argument with any of the valid endpoints outlined in
  38.  * the Digg API documentation. All other arguments are passed onto the API
  39.  * without modification.
  40.  *
  41.  * <code>
  42.  * <?php
  43.  *
  44.  * require_once 'Services/Digg/Proxy.php';
  45.  * Services_Digg::$appKey = 'http://www.example.com/myproxy.php';
  46.  * $proxy = new Services_Digg_Proxy();
  47.  * $proxy->proxy();
  48.  *
  49.  * ?>
  50.  * </code>
  51.  *
  52.  * @category    Services
  53.  * @package     Services_Digg
  54.  * @author      Joe Stump <joe@joestump.net>
  55.  */
  56. {
  57.     /**
  58.      * Possible response codes
  59.      *
  60.      * @access      private
  61.      * @var         array       $responseCodes 
  62.      */
  63.     private $responseCodes = array('200' => 'OK',
  64.                                    '403' => 'Forbidden',
  65.                                    '404' => 'Not found',
  66.                                    '500' => 'Internal error');
  67.  
  68.     /**
  69.      * Pass through these headers
  70.      *
  71.      * @access      private
  72.      * @var         array       $passThrough 
  73.      */
  74.     private $passThrough = array();
  75.  
  76.     /**
  77.      * Add additional request headers
  78.      *
  79.      * @access      private
  80.      * @var         array       $requestHeaders 
  81.      * @see         Services_Digg_Proxy::addRequestHeader();
  82.      */
  83.     private $requestHeaders = array();
  84.  
  85.     /**
  86.      * Proxy the request
  87.      *
  88.      * Takes either a GET or POST request and proxies a request to the Digg
  89.      * API via an HTTP_Request.
  90.      *
  91.      * @access      public
  92.      * @return      void 
  93.      * @see         HTTP_Request
  94.      */
  95.     public function proxy()
  96.     {
  97.         $req array_merge($_GET$_POST)// Support both GET and POST
  98.         if (!isset($req['type'])) {
  99.             $req['type''json'// Default to JSON
  100.         }
  101.  
  102.         $req['appkey'Services_Digg::$appKey;
  103.  
  104.         $endPoint '';
  105.         if (isset($req['endPoint'])) {
  106.             $endPoint $req['endPoint'];
  107.             unset($req['endPoint']);
  108.         }
  109.  
  110.         $sets = array();
  111.         foreach ($req as $key => $val{
  112.             $sets[$key '=' . urlencode($val);
  113.         }
  114.  
  115.         $uri Services_Digg::$uri $endPoint '?' . implode('&'$sets);
  116.         $ch curl_init();
  117.         curl_setopt($chCURLOPT_URL$uri);
  118.         curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
  119.         curl_setopt($chCURLOPT_USERAGENT
  120.                     'Services_Digg_Proxy (' Services_Digg::$appKey ')');
  121.  
  122.         if (isset($_SERVER['HTTP_REFERER'])) {
  123.             curl_setopt($chCURLOPT_REFERER$_SERVER['HTTP_REFERER']);
  124.         }
  125.  
  126.         $h $this->requestHeaders;
  127.         if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  128.             $h['X-Forwarded-For: ' $_SERVER['HTTP_X_FORWARDED_FOR'];
  129.         elseif (isset($_SERVER['REMOTE_ADDR'])) {
  130.             $h['X-Forwarded-For: ' $_SERVER['REMOTE_ADDR'];
  131.         }
  132.  
  133.         if (count($h)) {
  134.             curl_setopt($chCURLOPT_HTTPHEADER$h);
  135.         }
  136.  
  137.         curl_setopt($chCURLOPT_HEADERFUNCTIONarray($this'passHeaders'));
  138.  
  139.         $response curl_exec($ch);
  140.         if ($response === false{
  141.             throw new Services_Digg_Exception(curl_error($ch)curl_errno($ch));
  142.         }
  143.  
  144.         $code curl_getinfo($chCURLINFO_HTTP_CODE)
  145.         
  146.         if (isset($this->responseCodes[$code])) {
  147.             header('HTTP/1.1 ' $code ' ' $this->responseCodes[$code]);
  148.         }
  149.  
  150.         foreach ($this->passThrough as $header{
  151.             if (strlen($header)) {
  152.                 header($header);
  153.             }
  154.         }
  155.  
  156.         echo $response;
  157.         curl_close($ch);
  158.     }
  159.  
  160.     /**
  161.      * Pass through all headers
  162.      *
  163.      * @access      private
  164.      * @param       resource        $ch 
  165.      * @param       string          $header 
  166.      * @return      int 
  167.      */
  168.     private function passHeaders($ch$header
  169.     {
  170.         static $allow = array(
  171.             'Content-Type',
  172.             'Content-Length'            
  173.         );
  174.  
  175.         list($h,= explode(':'$header);
  176.         if (in_array($h$allow)) {
  177.             $this->passThrough[trim($header);
  178.         }
  179.  
  180.         return strlen($header);
  181.     }
  182.  
  183.     /**
  184.      * Add an additional request header
  185.      *
  186.      * A way to set specific headers to be sent to the API if you wish to
  187.      * overload the request behavior.
  188.      *
  189.      * @access      public
  190.      * @param       string      $header 
  191.      */
  192.     public function addRequestHeader($header)
  193.     {
  194.         $this->requestHeaders[$header;
  195.     }
  196. }
  197.  
  198. ?>

Documentation generated on Tue, 04 Dec 2007 15:00:11 -0500 by phpDocumentor 1.4.0. PEAR Logo Copyright © PHP Group 2004.