/** * 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(); }
<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();