rest-proxy.php

Simple proxy for RESTful APIs
git clone git://git.finwo.net/app/rest-proxy.php
Log | Files | Refs | README

RestController.php (1702B)


      1 <?php
      2 
      3 namespace Finwo\RestProxy\Controller;
      4 
      5 use Finwo\Framework\RestController as BaseController;
      6 use Finwo\Framework\Route;
      7 use OAuth2\Client;
      8 
      9 class RestController extends BaseController
     10 {
     11     protected function get_furl($url)
     12     {
     13         $furl = false;
     14 
     15         // First check response headers
     16         $headers = get_headers($url);
     17 
     18         // Test for 301 or 302
     19         if(preg_match('/^HTTP\/\d\.\d\s+(301|302)/',$headers[0]))
     20         {
     21             foreach($headers as $value)
     22             {
     23                 if(substr(strtolower($value), 0, 9) == "location:")
     24                 {
     25                     $furl = trim(substr($value, 9, strlen($value)));
     26                 }
     27             }
     28         }
     29         // Set final URL
     30         $furl = ($furl) ? $furl : $url;
     31 
     32         return $furl;
     33     }
     34 
     35     public function getAction($resource = '', $baseuri = '', $key = '', $secret = '', $query = '', $method = 'get', $header = array())
     36     {
     37         // Kickstart client
     38         $client = new Client($key, $secret);
     39 
     40         // Construct uri
     41         $uri = $this->get_furl($baseuri . '/' . $resource);
     42 
     43         // Unset a list of values, we won't send them directly
     44         $remove = array('resource','baseuri','key','secret','method','header');
     45         foreach($remove as $key) unset($query[$key]);
     46 
     47         // Set options for curl
     48         $client->setCurlOptions(array(
     49             CURLOPT_FOLLOWLOCATION => true,
     50             CURLOPT_USERAGENT      => sprintf('PHP/%s (%s)', PHP_VERSION, PHP_OS),
     51             CURLOPT_REFERER        => 'http://finwo.nl/',
     52             CURLOPT_CUSTOMREQUEST  => strtoupper($method),
     53         ));
     54 
     55         return $client->fetch($uri, $query, strtoupper($method), $header);
     56     }
     57 }