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.         if (!isset($req['appkey'])) {
  103.             $req['appkey'Services_Digg::$appKey;
  104.         }
  105.  
  106.         $endPoint '';
  107.         if (isset($req['endPoint'])) {
  108.             $endPoint $req['endPoint'];
  109.             unset($req['endPoint']);
  110.         }
  111.  
  112.         $sets = array();
  113.         foreach ($req as $key => $val{
  114.             $sets[$key '=' . urlencode($val);
  115.         }
  116.  
  117.         $uri Services_Digg::$uri $endPoint '?' . implode('&'$sets);
  118.         $ch curl_init();
  119.         curl_setopt($chCURLOPT_URL$uri);
  120.         curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
  121.         curl_setopt($chCURLOPT_USERAGENT
  122.                     'Services_Digg_Proxy (' Services_Digg::$appKey ')');
  123.  
  124.         if (isset($_SERVER['HTTP_REFERER'])) {
  125.             curl_setopt($chCURLOPT_REFERER$_SERVER['HTTP_REFERER']);
  126.         }
  127.  
  128.         $h $this->requestHeaders;
  129.         if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  130.             $h['X-Forwarded-For: ' $_SERVER['HTTP_X_FORWARDED_FOR'];
  131.         elseif (isset($_SERVER['REMOTE_ADDR'])) {
  132.             $h['X-Forwarded-For: ' $_SERVER['REMOTE_ADDR'];
  133.         }
  134.  
  135.         if (count($h)) {
  136.             curl_setopt($chCURLOPT_HTTPHEADER$h);
  137.         }
  138.  
  139.         curl_setopt($chCURLOPT_HEADERFUNCTIONarray($this'passHeaders'));
  140.  
  141.         $response curl_exec($ch);
  142.         if ($response === false{
  143.             throw new Services_Digg_Exception(curl_error($ch)curl_errno($ch));
  144.         }
  145.  
  146.         $code curl_getinfo($chCURLINFO_HTTP_CODE)
  147.         
  148.         if (isset($this->responseCodes[$code])) {
  149.             header('HTTP/1.1 ' $code ' ' $this->responseCodes[$code]);
  150.         }
  151.  
  152.         foreach ($this->passThrough as $header{
  153.             if (strlen($header)) {
  154.                 header($header);
  155.             }
  156.         }
  157.  
  158.         echo $response;
  159.         curl_close($ch);
  160.     }
  161.  
  162.     /**
  163.      * Pass through all headers
  164.      *
  165.      * @access      private
  166.      * @param       resource        $ch 
  167.      * @param       string          $header 
  168.      * @return      int 
  169.      */
  170.     private function passHeaders($ch$header
  171.     {
  172.         static $allow = array(
  173.             'Content-Type',
  174.             'Content-Length'            
  175.         );
  176.  
  177.         list($h,= explode(':'$header);
  178.         if (in_array($h$allow)) {
  179.             $this->passThrough[trim($header);
  180.         }
  181.  
  182.         return strlen($header);
  183.     }
  184.  
  185.     /**
  186.      * Add an additional request header
  187.      *
  188.      * A way to set specific headers to be sent to the API if you wish to
  189.      * overload the request behavior.
  190.      *
  191.      * @access      public
  192.      * @param       string      $header 
  193.      */
  194.     public function addRequestHeader($header)
  195.     {
  196.         $this->requestHeaders[$header;
  197.     }
  198. }
  199.  
  200. ?>

Documentation generated on Fri, 10 Jul 2009 01:30:05 +0000 by phpDocumentor 1.4.2. PEAR Logo Copyright © PHP Group 2004.