Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

init

  • Loading branch information...
commit 969b7a50b58c36094acf4fc1b81a4ca71414f370 0 parents
Seth Laptop authored
Showing with 101 additions and 0 deletions.
  1. +98 −0 nap.php
  2. +3 −0  readme.md
98 nap.php
@@ -0,0 +1,98 @@
+<?
+/*
+ Nap - A quick REST
+ 1,388 B
+ 1,114 B
+ 1,341 B
+ 2,080 B
+*/
+
+class NapTool {
+ public static function makeMethod($method, $id = "") {
+ if ($method == "GET" && $id == ""):
+ return "get_all";
+ else :
+ return strtolower($method);
+ endif;
+ }
+ public static function getCodeMsg($code) {
+ $codes = array(
+ 200 => "OK",
+ 201 => "Created",
+ 400 => "Bad Request",
+ 404 => "Not Found",
+ 405 => "Method Not Found",
+ 500 => "Internal Server Error",
+ 501 => "Not Implemented"
+ );
+ return isset($codes[$code]) ? $code.' '.$codes[$code] : $code;
+ }
+
+ public static function makeHeader($code) {
+ $code_message = self::getCodeMsg($code);
+ return "HTTP/1.1 $code_message";
+ }
+ public static function errorToJson($error) {
+ return '{"error":"'.$error.'"}';
+ }
+ public static function validationToJson($raw) {
+ $errors = str_replace("\r\n", "", $raw);
+ $errors = rtrim(trim($errors), ',');
+ return '{"error":['.$errors.']}';
+ }
+}
+
+class Nap {
+ public $method;
+ public $header;
+ public $error;
+ public $mime;
+ public $output = "";
+ public function __construct() {
+ $this->mime = 'application/json';
+ }
+ public static function init($id) {
+ $instance = new self();
+ $instance->method = NapTool::makeMethod($_SERVER['REQUEST_METHOD'], $id);
+ return $instance;
+ }
+ public function setHeader($code) {
+ $this->header = NapTool::makeHeader($code);
+ }
+ public function setError($_error) {
+ $this->error = $_error;
+ }
+
+ public function setOutput($_output) {
+ $this->output = $_output;
+ }
+}
+
+class NapRequest {
+ public $status;
+ public $output;
+
+ public function __construct() {
+ }
+
+ public static function init($url, $post = "") {
+ $instance = new self();
+ $instance->curl($url, $post);
+ return $instance;
+ }
+
+ private function curl($url, $post = "") {
+ $curl = curl_init();
+ curl_setopt($curl, CURLOPT_URL, $url);
+ curl_setopt($curl, CURLOPT_HEADER, 0);
+ curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
+ if ($post != "") :
+ curl_setopt($curl,CURLOPT_POSTFIELDS, $post);
+ endif;
+ $this->output = curl_exec($curl);
+ $this->status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
+
+ curl_close ($curl);
+ }
+
+}
3  readme.md
@@ -0,0 +1,3 @@
+# Nap - A Quick Rest
+
+For Codeigniter
Please sign in to comment.
Something went wrong with that request. Please try again.