Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

init

  • Loading branch information...
commit 969b7a50b58c36094acf4fc1b81a4ca71414f370 0 parents
Seth Laptop authored

Showing 2 changed files with 101 additions and 0 deletions. Show diff stats Hide diff stats

  1. +98 0 nap.php
  2. +3 0  readme.md
98 nap.php
... ... @@ -0,0 +1,98 @@
  1 +<?
  2 +/*
  3 + Nap - A quick REST
  4 + 1,388 B
  5 + 1,114 B
  6 + 1,341 B
  7 + 2,080 B
  8 +*/
  9 +
  10 +class NapTool {
  11 + public static function makeMethod($method, $id = "") {
  12 + if ($method == "GET" && $id == ""):
  13 + return "get_all";
  14 + else :
  15 + return strtolower($method);
  16 + endif;
  17 + }
  18 + public static function getCodeMsg($code) {
  19 + $codes = array(
  20 + 200 => "OK",
  21 + 201 => "Created",
  22 + 400 => "Bad Request",
  23 + 404 => "Not Found",
  24 + 405 => "Method Not Found",
  25 + 500 => "Internal Server Error",
  26 + 501 => "Not Implemented"
  27 + );
  28 + return isset($codes[$code]) ? $code.' '.$codes[$code] : $code;
  29 + }
  30 +
  31 + public static function makeHeader($code) {
  32 + $code_message = self::getCodeMsg($code);
  33 + return "HTTP/1.1 $code_message";
  34 + }
  35 + public static function errorToJson($error) {
  36 + return '{"error":"'.$error.'"}';
  37 + }
  38 + public static function validationToJson($raw) {
  39 + $errors = str_replace("\r\n", "", $raw);
  40 + $errors = rtrim(trim($errors), ',');
  41 + return '{"error":['.$errors.']}';
  42 + }
  43 +}
  44 +
  45 +class Nap {
  46 + public $method;
  47 + public $header;
  48 + public $error;
  49 + public $mime;
  50 + public $output = "";
  51 + public function __construct() {
  52 + $this->mime = 'application/json';
  53 + }
  54 + public static function init($id) {
  55 + $instance = new self();
  56 + $instance->method = NapTool::makeMethod($_SERVER['REQUEST_METHOD'], $id);
  57 + return $instance;
  58 + }
  59 + public function setHeader($code) {
  60 + $this->header = NapTool::makeHeader($code);
  61 + }
  62 + public function setError($_error) {
  63 + $this->error = $_error;
  64 + }
  65 +
  66 + public function setOutput($_output) {
  67 + $this->output = $_output;
  68 + }
  69 +}
  70 +
  71 +class NapRequest {
  72 + public $status;
  73 + public $output;
  74 +
  75 + public function __construct() {
  76 + }
  77 +
  78 + public static function init($url, $post = "") {
  79 + $instance = new self();
  80 + $instance->curl($url, $post);
  81 + return $instance;
  82 + }
  83 +
  84 + private function curl($url, $post = "") {
  85 + $curl = curl_init();
  86 + curl_setopt($curl, CURLOPT_URL, $url);
  87 + curl_setopt($curl, CURLOPT_HEADER, 0);
  88 + curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  89 + if ($post != "") :
  90 + curl_setopt($curl,CURLOPT_POSTFIELDS, $post);
  91 + endif;
  92 + $this->output = curl_exec($curl);
  93 + $this->status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  94 +
  95 + curl_close ($curl);
  96 + }
  97 +
  98 +}
3  readme.md
Source Rendered
... ... @@ -0,0 +1,3 @@
  1 +# Nap - A Quick Rest
  2 +
  3 +For Codeigniter

0 comments on commit 969b7a5

Please sign in to comment.
Something went wrong with that request. Please try again.