/** * Generic API override __call * * @param string url/controller name * @param array array of arguments * @return void */ public function __call($url, $args) { // load API class $check = X4Core_core::auto_load($url . '_api'); // if API exists if ($check) { // call Restler $r = new Restler(); $r->setSupportedFormats('JsonFormat', 'XmlFormat'); $r->addAPIClass($url); $r->addAuthenticationClass('SimpleAuth'); $r->handle(); } else { return false; } }
/** * This method allow dispatch rest services using 'Restler' thirdparty library * * @author Erik Amaru Ortiz <*****@*****.**> */ public function dispatchRestService($uri, $config, $apiClassesPath = '') { require_once 'restler/restler.php'; $rest = new Restler(); $rest->setSupportedFormats('JsonFormat', 'XmlFormat'); // getting all services class $restClasses = array(); $restClassesList = G::rglob('*', 0, PATH_CORE . 'services/'); foreach ($restClassesList as $classFile) { if (substr($classFile, -4) === '.php') { $restClasses[str_replace('.php', '', basename($classFile))] = $classFile; } } if (!empty($apiClassesPath)) { $pluginRestClasses = array(); $restClassesList = G::rglob('*', 0, $apiClassesPath . 'services/'); foreach ($restClassesList as $classFile) { if (substr($classFile, -4) === '.php') { $pluginRestClasses[str_replace('.php', '', basename($classFile))] = $classFile; } } $restClasses = array_merge($restClasses, $pluginRestClasses); } // hook to get rest api classes from plugins if (class_exists('PMPluginRegistry')) { $pluginRegistry =& PMPluginRegistry::getSingleton(); $pluginClasses = $pluginRegistry->getRegisteredRestClassFiles(); $restClasses = array_merge($restClasses, $pluginClasses); } foreach ($restClasses as $key => $classFile) { if (!file_exists($classFile)) { unset($restClasses[$key]); continue; } //load the file, and check if exist the class inside it. require_once $classFile; $namespace = 'Services_Rest_'; $className = str_replace('.php', '', basename($classFile)); // if the core class does not exists try resolve the for a plugin if (!class_exists($namespace . $className)) { $namespace = 'Plugin_Services_Rest_'; // Couldn't resolve the class name, just skipp it if (!class_exists($namespace . $className)) { unset($restClasses[$key]); continue; } } // verify if there is an auth class implementing 'iAuthenticate' $classNameAuth = $namespace . $className; $reflClass = new ReflectionClass($classNameAuth); // that wasn't from plugin if ($reflClass->implementsInterface('iAuthenticate') && $namespace != 'Plugin_Services_Rest_') { // auth class found, set as restler authentication class handler $rest->addAuthenticationClass($classNameAuth); } else { // add api class $rest->addAPIClass($classNameAuth); } } //end foreach rest class // resolving the class for current request $uriPart = explode('/', $uri); $requestedClass = ''; if (isset($uriPart[1])) { $requestedClass = ucfirst($uriPart[1]); } if (class_exists('Services_Rest_' . $requestedClass)) { $namespace = 'Services_Rest_'; } elseif (class_exists('Plugin_Services_Rest_' . $requestedClass)) { $namespace = 'Plugin_Services_Rest_'; } else { $namespace = ''; } // end resolv. // Send additional headers (if exists) configured on rest-config.ini if (array_key_exists('HEADERS', $config)) { foreach ($config['HEADERS'] as $name => $value) { header("{$name}: {$value}"); } } // to handle a request with "OPTIONS" method if (!empty($namespace) && $_SERVER['REQUEST_METHOD'] === 'OPTIONS') { $reflClass = new ReflectionClass($namespace . $requestedClass); // if the rest class has not a "options" method if (!$reflClass->hasMethod('options')) { header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS, HEADERS'); header('Access-Control-Allow-Headers: authorization, content-type'); header("Access-Control-Allow-Credentials", "false"); header('Access-Control-Max-Age: 60'); exit; } } // override global REQUEST_URI to pass to Restler library $_SERVER['REQUEST_URI'] = '/' . strtolower($namespace) . ltrim($uri, '/'); // handle the rest request $rest->handle(); }
<?php /* Tagline: Less is more. Description: Shows the bare minimum code needed to get your RESTful api server up and running. Example 1: GET math/add returns 2. Example 2: GET math/add/4/3 returns 7. Example 3: GET math/add?n1=6&n2=4 returns 10. Example 4: GET math/multiply returns {"result":10}. Example 5: GET math/multiply/4/3 returns {"result":12}. Example 6: GET math/multiply?n2=4 returns {"result":20}. */ #add restler to include path set_include_path(get_include_path() . PATH_SEPARATOR . '../../restler'); #set autoloader #do not use spl_autoload_register with out parameter #it will disable the autoloading of formats spl_autoload_register('spl_autoload'); $r = new Restler(); $r->addAPIClass('Math'); $r->handle();
* Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. * See the License for the specific language governing rights and limitations under the License. * The Original Code is YetiForce. * The Initial Developer of the Original Code is YetiForce. Portions created by YetiForce are Copyright (C) www.yetiforce.com. * All Rights Reserved. * *********************************************************************************************************************************** */ $currentPath = dirname(__FILE__); $crmPath = $currentPath . '/../'; chdir($crmPath); require_once 'include/database/PearDatabase.php'; require_once 'libraries/restler/restler.php'; require_once 'include/utils/VtlibUtils.php'; AppConfig::iniSet('error_log', $root_directory . 'cache/logs/mobileApps.log'); if (!in_array('mobile', $enabledServices)) { require_once 'include/exceptions/AppException.php'; $apiLog = new APINoPermittedException(); $apiLog->stop(['status' => 0, 'message' => 'Mobile - Service is not active']); } $adb = PearDatabase::getInstance(); $log =& LoggerManager::getLogger('mobileApps'); vglobal('log', $log); $adb = PearDatabase::getInstance(); $log->info('Start mobile service'); spl_autoload_register('spl_autoload'); $r = new Restler(); $r->addAPIClass('Test'); $r->addAPIClass('HistoryCall'); $r->addAPIClass('PushCall'); //$r->addAPIClass('PushMessage'); $r->handle(); $log->info('End mobile service');
<?php /* Title: Hello World Example. Tagline: Let's say hello!. Description: Basic hello world example to get started with Restler 2.0. Example 1: GET say/hello returns "Hello world!". Example 2: GET say/hello/Restler2.0 returns "Hello Restler2.0!". Example 3: GET say/hello?to=R.Arul%20Kumaran returns "Hello R.Arul Kumaran!". */ require_once '../../restler/restler.php'; require_once 'say.php'; $r = new Restler(); $r->addAPIClass('Say'); $r->handle();
<imperial> <height>5 feet 4 inches</height> <weight>185.19 pounds</weight> </imperial> </response>. Example 3: GET bmi.json returns { "bmi": 31.77, "message": "Obesity", "metric": { "height": "162.6 centimeter", "weight": "84 kilograms" }, "imperial": { "height": "5 feet 4 inches", "weight": "185.19 pounds" } } . */ require_once '../../restler/restler.php'; #set autoloader #do not use spl_autoload_register with out parameter #it will disable the autoloading of formats spl_autoload_register('spl_autoload'); $r = new Restler(); $r->setSupportedFormats('JsonFormat', 'XmlFormat'); $r->addAPIClass('BMI'); $r->handle();
HTTP/1.1 200 OK Date: Fri, 19 Aug 2011 16:34:41 GMT Server: Apache/2.2.19 (Unix) mod_ssl/2.2.19 OpenSSL/0.9.8r DAV/2 PHP/5.3.6 with Suhosin-Patch X-Powered-By: Luracast Restler v2.0.0 Expires: 0 Cache-Control: no-cache, must-revalidate Pragma: no-cache Content-Length: 66 Content-Type: application/json { "name": "Another", "email": "*****@*****.**", "id": 7 } *[db_pdo_sqlite.php]: _006_crud/db_pdo_sqlite.php *[db_serialized_file.php]: _006_crud/db_serialized_file.php *[db_pdo_mysql.php]: _006_crud/db_pdo_mysql.php . Helper Classes: DB_Session. */ require_once '../../restler/restler.php'; #set autoloader #do not use spl_autoload_register with out parameter #it will disable the autoloading of formats spl_autoload_register('spl_autoload'); $r = new Restler(); $r->addAPIClass('Author'); $r->handle();
2. Add a PHPDoc comment `@protected` to the method 3. Add `@protected` comment to the class to protect all methods of that class In order to provide access to those protected methods we use a class that implements `iAuthenticate`. Also note that An Authentication class is also an API class so all public methods that does not begin with `_` will be exposed as API for example [SimpleAuth::key](simpleauth/key). It can be used to create login/logout methods. Example 1: GET restricted returns { "error": { "code": 401, "message": "Unauthorized" } }. Example 2: GET restricted?key=rEsTlEr2 returns "protected method". Example 3: GET secured?key=rEsTlEr2 returns "protected class". */ require_once '../../restler/restler.php'; #set autoloader #do not use spl_autoload_register with out parameter #it will disable the autoloading of formats spl_autoload_register('spl_autoload'); $r = new Restler(); $r->addAPIClass('Simple', ''); $r->addAPIClass('Secured'); $r->addAuthenticationClass('SimpleAuth'); $r->handle();
<?php require_once '../lib/restler/restler.php'; require_once "CharacterUrlMapper.php"; require_once "PlayerDataUrlMapper.php"; #set autoloader #do not use spl_autoload_register with out parameter #it will disable the autoloading of formats spl_autoload_register('spl_autoload'); $r = new Restler(); $r->setSupportedFormats('JsonFormat'); $r->addAPIClass('CharacterUrlMapper', "get/character_url/"); $r->addAPIClass('PlayerDataUrlMapper', "post/get_url_from_data/"); $r->handle();
For the list of HTTP Status codes and their meaning take a look at [Wikipedia](http://en.wikipedia.org/wiki/List_of_httpStatusCodes). Example 1: GET currency/format returns { "error": { "code": 400, "message": "Bad Request" } }. Example 2: GET currency/format/not_a_number returns { "error": { "code": 412, "message": "Precondition Failed: not a valid number" } }. Example 3: GET currency/format?number=55 returns "USD55.00". */ require_once '../../restler/restler.php'; #set autoloader #do not use spl_autoload_register with out parameter #it will disable the autoloading of formats spl_autoload_register('spl_autoload'); $r = new Restler(); $r->addAPIClass('Currency'); $r->handle();
<?php header('Content-Type: application/json; charset=utf-8'); /*Los siguientes permiten el crossdomain*/ if (isset($_SERVER['HTTP_ORIGIN'])) { header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}"); header('Access-Control-Allow-Credentials: true'); header('Access-Control-Max-Age: 86400'); // cache for 1 day } if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') { if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD'])) { header("Access-Control-Allow-Methods: GET, POST, OPTIONS"); } if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS'])) { header("Access-Control-Allow-Headers:{$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}"); } exit(0); } include "config.inc.php"; require_once "restler/restler.php"; require_once "auth/simpleauth.php"; include_once "restful/article.php"; $r = new Restler(); if (isset($_GET['xml'])) { $r->setSupportedFormats('XmlFormat', 'JsonFormat'); } $r->addAuthenticationClass('SimpleAuth'); $r->addAPIClass('Article'); $r->handle();