/** * Create API handler * @param array $options * * Supported values: * * array( * 'base_url' => '', * 'api_key' => '', * 'data_type' => 'json', * 'header' => array() * ) * * @return MyAPI MyAPI instance */ static function create($options = array()) { $opts = array_merge(self::$_options, $options); $header = $opts['header']; $header['api-key'] = $opts['api_key']; $headers = array(); foreach ($header as $key => $value) { $headers[] = "{$key}: {$value}"; } $opts['header'] = $headers; self::$_options = $opts; if (static::$_instance === NULL) { static::$_instance = new static(); } return static::$_instance; }
<?php require_once 'MyAPI.class.php'; // Requests from the same server don't have a HTTP_ORIGIN header if (!array_key_exists('HTTP_ORIGIN', $_SERVER)) { $_SERVER['HTTP_ORIGIN'] = $_SERVER['SERVER_NAME']; } try { $API = new MyAPI($_REQUEST['request'], $_SERVER['HTTP_ORIGIN']); echo $API->processAPI(); } catch (Exception $e) { echo json_encode(array('error' => $e->getMessage())); }
<?php require_once 'MyAPI.php'; // Requests from the same server don't have a HTTP_ORIGIN header if (!array_key_exists('HTTP_ORIGIN', $_SERVER)) { $_SERVER['HTTP_ORIGIN'] = $_SERVER['SERVER_NAME']; } try { @($MyAPI = new MyAPI($_REQUEST['request'], $_SERVER['HTTP_ORIGIN'])); echo $MyAPI->processAPI(); } catch (Exception $e) { echo json_encode(array('error' => $e->getMessage())); }
<?php require '../application/libraries/MyAPI.php'; $api = MyAPI::create(array('base_url' => 'http://localhost:8001', 'api_key' => '32563b81ec7288ef87bbe39c3b7001a7bff35395eec1eac906a580e6a12d189e', 'data_type' => 'json')); $json = $api::get('/user/1'); $data = json_decode($json, TRUE); print_r($data);
include_once "classes/MyAPI.php"; include_once "classes/APIDatabase.php"; include_once "classes/Acceso.php"; /** AUTOLOADER de clases */ spl_autoload_register('apiAutoload'); function apiAutoload($classname) { if (preg_match('/[a-zA-Z]+Controlador$/', $classname)) { /** @noinspection PhpIncludeInspection */ include __DIR__ . '/controladores/' . $classname . '.php'; return true; } elseif (preg_match('/[a-zA-Z]+Modelo$/', $classname)) { /** @noinspection PhpIncludeInspection */ include __DIR__ . '/modelos/' . $classname . '.php'; return true; } elseif (preg_match('/[a-zA-Z]+View$/', $classname)) { /** @noinspection PhpIncludeInspection */ include __DIR__ . '/views/' . $classname . '.php'; return true; } return null; } if (!array_key_exists('HTTP_ORIGIN', $_SERVER)) { $_SERVER['HTTP_ORIGIN'] = $_SERVER['SERVER_NAME']; } try { $API = new MyAPI($_REQUEST['request'], $_SERVER['HTTP_ORIGIN']); echo json_encode($API->processAPI()); } catch (Exception $e) { echo array('error' => $e->getMessage()); }
foreach ($json['data'] as $key => $message) { if (isset($message['id']) && $message['id'] > $max_id) { $max_id = $message['id']; } if (isset($message['id']) && isset($file['id']) && $message['id'] == $file['id']) { $id_exists = true; } } if ($id_exists == true) { $msg_id = $max_id + 1; } // this is the updated message $postData = array('id' => "{$msg_id}", 'from' => array('id' => array_key_exists('id', $file['from']) ? $file['from']['id'] : '', 'name' => array_key_exists('name', $file['from']) ? $file['from']['name'] : ''), 'to' => array('id' => array_key_exists('id', $file['to']) ? $file['to']['id'] : '', 'name' => array_key_exists('name', $file['to']) ? $file['to']['name'] : ''), 'type' => array_key_exists('type', $file) ? $file['type'] : '', 'replyto' => array_key_exists('replyto', $file) ? $file['replyto'] : '', 'date_sent' => array_key_exists('date_sent', $file) ? $file['date_sent'] : '', 'date_read' => array_key_exists('date_read', $file) ? $file['date_read'] : '', 'subject' => array_key_exists('subject', $file) ? $file['subject'] : '', 'message' => array_key_exists('message', $file) ? $file['message'] : '', 'message_formatted' => array_key_exists('message_formatted', $file) ? $file['message_formatted'] : '', 'date_sent_formatted' => array('id' => array_key_exists('id', $file['date_sent_formatted']) ? $file['date_sent_formatted']['id'] : '', 'timestamp' => array_key_exists('timestamp', $file['date_sent_formatted']) ? $file['date_sent_formatted']['timestamp'] : '', 'month' => array_key_exists('month', $file['date_sent_formatted']) ? $file['date_sent_formatted']['month'] : '', 'day' => array_key_exists('day', $file['date_sent_formatted']) ? $file['date_sent_formatted']['day'] : '', 'year' => array_key_exists('year', $file['date_sent_formatted']) ? $file['date_sent_formatted']['year'] : '', 'week' => array_key_exists('week', $file['date_sent_formatted']) ? $file['date_sent_formatted']['week'] : '', 'dayid' => array_key_exists('dayid', $file['date_sent_formatted']) ? $file['date_sent_formatted']['dayid'] : '', 'weekday' => array_key_exists('weekday', $file['date_sent_formatted']) ? $file['date_sent_formatted']['weekday'] : '', 'mname' => array_key_exists('mname', $file['date_sent_formatted']) ? $file['date_sent_formatted']['mname'] : '', 'formatted' => array_key_exists('formatted', $file['date_sent_formatted']) ? $file['date_sent_formatted']['formatted'] : ''), 'date_read_formatted' => array('id' => array_key_exists('id', $file['date_read_formatted']) ? $file['date_read_formatted']['id'] : '', 'timestamp' => array_key_exists('timestamp', $file['date_read_formatted']) ? $file['date_read_formatted']['timestamp'] : '', 'month' => array_key_exists('month', $file['date_read_formatted']) ? $file['date_read_formatted']['month'] : '', 'day' => array_key_exists('day', $file['date_read_formatted']) ? $file['date_read_formatted']['day'] : '', 'year' => array_key_exists('year', $file['date_read_formatted']) ? $file['date_read_formatted']['year'] : '', 'week' => array_key_exists('week', $file['date_read_formatted']) ? $file['date_read_formatted']['week'] : '', 'dayid' => array_key_exists('dayid', $file['date_read_formatted']) ? $file['date_read_formatted']['dayid'] : '', 'weekday' => array_key_exists('weekday', $file['date_read_formatted']) ? $file['date_read_formatted']['weekday'] : '', 'mname' => array_key_exists('mname', $file['date_read_formatted']) ? $file['date_read_formatted']['mname'] : '', 'formatted' => array_key_exists('formatted', $file['date_read_formatted']) ? $file['date_read_formatted']['formatted'] : '')); // prepend $postData (a new message) to the beginning of the JSON array array_unshift($json['data'], $postData); $messages = $json['data']; // sort the response array according to the message id // using a user defined function usort($messages, function ($a, $b) { return $a['id'] > $b['id'] ? -1 : 1; }); return $messages; } } // create an instance of MyAPI class and invoke processAPI() try { $api = new MyAPI($_REQUEST['request']); echo $api->processAPI(); } catch (Exception $e) { echo json_encode(array('error' => $e->getMessage())); }