rest-proxy.php

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

commit 377d91a39269b8a4e4a583ce5fb703fc439f99f8
parent 4569cacd65eefa9728228bc38934fbd30a433411
Author: finwo <finwo@pm.me>
Date:   Tue, 19 Apr 2016 21:14:02 +0200

RC-1

Diffstat:
A.buildpacks | 1+
AProcfile | 2++
Mcomposer.json | 5++++-
Mcomposer.lock | 61++++++++++++++++++++++++++++++++++++++++++++++++++++++-------
Msrc/Finwo/Framework/Application.php | 2++
Msrc/Finwo/RestProxy/Controller/RestController.php | 58+++++++++++++++++++++++++++++++++++++++++++++-------------
6 files changed, 108 insertions(+), 21 deletions(-)

diff --git a/.buildpacks b/.buildpacks @@ -0,0 +1 @@ +https://github.com/heroku/heroku-buildpack-php.git diff --git a/Procfile b/Procfile @@ -0,0 +1 @@ +web: vendor/bin/heroku-php-apache2 web/ +\ No newline at end of file diff --git a/composer.json b/composer.json @@ -13,11 +13,14 @@ } }, "require": { + "php": ">=5.3", + "ext-curl": "*", "mustangostang/spyc": "^0.5.1", "finwo/property-accessor": "*@dev", "adoy/oauth2": "^1.3", "finwo/data-tools": "dev-master", "netresearch/jsonmapper": "^0.11.0", - "PHP-DI/invoker": "^1.3" + "PHP-DI/invoker": "^1.3", + "heroku/heroku-buildpack-php": "^101.0" } } diff --git a/composer.lock b/composer.lock @@ -4,8 +4,8 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "30c1581acde8d7118cc2c82cfc74a0ba", - "content-hash": "4ff42c18a3c18166064b1fba0792795b", + "hash": "dbebb718c0f2eb4e1b8e63ec2f53337a", + "content-hash": "1f06ab92a8648132973e1d75e5a9072a", "packages": [ { "name": "adoy/oauth2", @@ -115,12 +115,12 @@ "source": { "type": "git", "url": "https://github.com/finwo/property-accessor.git", - "reference": "3135b3e4f34f6887b37e0c23811aa9566df4abab" + "reference": "434c4355987cf5ebd7475f3124a847d71b3e4152" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/finwo/property-accessor/zipball/3135b3e4f34f6887b37e0c23811aa9566df4abab", - "reference": "3135b3e4f34f6887b37e0c23811aa9566df4abab", + "url": "https://api.github.com/repos/finwo/property-accessor/zipball/434c4355987cf5ebd7475f3124a847d71b3e4152", + "reference": "434c4355987cf5ebd7475f3124a847d71b3e4152", "shasum": "" }, "type": "library", @@ -136,7 +136,51 @@ "email": "robin@finwo.nl" } ], - "time": "2016-04-17 19:15:10" + "time": "2016-04-19 13:13:15" + }, + { + "name": "heroku/heroku-buildpack-php", + "version": "v101", + "source": { + "type": "git", + "url": "https://github.com/heroku/heroku-buildpack-php.git", + "reference": "efca12bb59ccdebec79cabddd94e7c1ef51de908" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/heroku/heroku-buildpack-php/zipball/efca12bb59ccdebec79cabddd94e7c1ef51de908", + "reference": "efca12bb59ccdebec79cabddd94e7c1ef51de908", + "shasum": "" + }, + "bin": [ + "bin/heroku-hhvm-apache2", + "bin/heroku-hhvm-nginx", + "bin/heroku-php-apache2", + "bin/heroku-php-nginx" + ], + "type": "library", + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "David Zuelke", + "email": "dz@heroku.com" + } + ], + "description": "Toolkit for starting a PHP application locally, with or without foreman, using the same config for PHP/HHVM and Apache2/Nginx as on Heroku", + "homepage": "http://github.com/heroku/heroku-buildpack-php", + "keywords": [ + "apache", + "apache2", + "foreman", + "heroku", + "hhvm", + "nginx", + "php" + ], + "time": "2016-04-12 02:41:29" }, { "name": "mustangostang/spyc", @@ -280,6 +324,9 @@ }, "prefer-stable": false, "prefer-lowest": false, - "platform": [], + "platform": { + "php": ">=5.3", + "ext-curl": "*" + }, "platform-dev": [] } diff --git a/src/Finwo/Framework/Application.php b/src/Finwo/Framework/Application.php @@ -170,8 +170,10 @@ class Application switch(strtolower($format)) { case 'json': + header('Content-Type: application/json'); die(json_encode($answer)); case 'xml': + header('Content-Type: application/xml'); $transformer = new XmlTransformer(); die($transformer->objToXML($answer)); default: diff --git a/src/Finwo/RestProxy/Controller/RestController.php b/src/Finwo/RestProxy/Controller/RestController.php @@ -4,22 +4,54 @@ namespace Finwo\RestProxy\Controller; use Finwo\Framework\RestController as BaseController; use Finwo\Framework\Route; +use OAuth2\Client; class RestController extends BaseController { - public function getAction($resource = '', $query = array()) + protected function get_furl($url) { - return array( - 'html' => array( - 'head' => array( - 'I\'m fine, thank you' - ), - 'body' => array( - 'div' => array( - 'How are you doing?' - ) - ) - ) - ); + $furl = false; + + // First check response headers + $headers = get_headers($url); + + // Test for 301 or 302 + if(preg_match('/^HTTP\/\d\.\d\s+(301|302)/',$headers[0])) + { + foreach($headers as $value) + { + if(substr(strtolower($value), 0, 9) == "location:") + { + $furl = trim(substr($value, 9, strlen($value))); + } + } + } + // Set final URL + $furl = ($furl) ? $furl : $url; + + return $furl; + } + + public function getAction($resource = '', $baseuri = '', $key = '', $secret = '', $query = '', $method = 'get', $header = array()) + { + // Kickstart client + $client = new Client($key, $secret); + + // Construct uri + $uri = $this->get_furl($baseuri . '/' . $resource); + + // Unset a list of values, we won't send them directly + $remove = array('resource','baseuri','key','secret','method','header'); + foreach($remove as $key) unset($query[$key]); + + // Set options for curl + $client->setCurlOptions(array( + CURLOPT_FOLLOWLOCATION => true, + CURLOPT_USERAGENT => sprintf('PHP/%s (%s)', PHP_VERSION, PHP_OS), + CURLOPT_REFERER => 'http://finwo.nl/', + CURLOPT_CUSTOMREQUEST => strtoupper($method), + )); + + return $client->fetch($uri, $query, strtoupper($method), $header); } } \ No newline at end of file