Init
This commit is contained in:
28
simpla/rest/.htaccess
Normal file
28
simpla/rest/.htaccess
Normal file
@@ -0,0 +1,28 @@
|
||||
<Limit GET POST PUT DELETE>
|
||||
Allow from all
|
||||
</Limit>
|
||||
|
||||
AddDefaultCharset UTF-8
|
||||
|
||||
RewriteEngine on
|
||||
|
||||
# Товары
|
||||
RewriteRule ^products/?$ index.php?resource=products [L,QSA]
|
||||
RewriteRule ^products/([\d,]+)/?$ index.php?resource=products&id=$1 [L,QSA]
|
||||
RewriteRule ^categories/(\d+)/products/?$ index.php?resource=products&category=$1 [L,QSA]
|
||||
RewriteRule ^categories/(\d+)/brands/(\d+)/products/?$ index.php?resource=products&category=$1&category=$2 [L,QSA]
|
||||
|
||||
RewriteRule ^categories/?$ index.php?resource=categories [L,QSA]
|
||||
|
||||
RewriteRule ^products/(\d+)/categories/?$ index.php?resource=products_categories&product_id=$1 [L,QSA]
|
||||
|
||||
|
||||
RewriteRule ^orders/(\d+)/purchases/?$ index.php?resource=purchases&order_id=$1 [L,QSA]
|
||||
|
||||
# Blog
|
||||
RewriteRule ^blog/?$ index.php?resource=blog [L,QSA]
|
||||
RewriteRule ^blog/([\d,]+)/?$ index.php?resource=blog&id=$1 [L,QSA]
|
||||
|
||||
# Orders
|
||||
RewriteRule ^orders/?$ index.php?resource=orders [L,QSA]
|
||||
RewriteRule ^orders/([\d,]+)/?$ index.php?resource=orders&id=$1 [L,QSA]
|
||||
9
simpla/rest/Rest.php
Normal file
9
simpla/rest/Rest.php
Normal file
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
require_once('api/Simpla.php');
|
||||
|
||||
class Rest extends Simpla
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
130
simpla/rest/RestBlog.php
Normal file
130
simpla/rest/RestBlog.php
Normal file
@@ -0,0 +1,130 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Simpla CMS
|
||||
*
|
||||
* @copyright 2012 Denis Pikusov
|
||||
* @link http://simplacms.ru
|
||||
* @author Denis Pikusov
|
||||
*
|
||||
*/
|
||||
|
||||
require_once('Rest.php');
|
||||
|
||||
class RestBlog extends Rest
|
||||
{
|
||||
public function fetch()
|
||||
{
|
||||
if($this->request->method('GET'))
|
||||
$result = $this->get();
|
||||
if($this->request->method('POST'))
|
||||
$result = $this->post();
|
||||
if($this->request->method('PUT'))
|
||||
$result = $this->put();
|
||||
if($this->request->method('DELETE'))
|
||||
$result = $this->delete();
|
||||
|
||||
return $this->indent(json_encode($result));
|
||||
}
|
||||
|
||||
public function get()
|
||||
{
|
||||
$fields = explode(',', $this->request->get('fields'));
|
||||
|
||||
$ids = array();
|
||||
foreach(explode(',', $this->request->get('id')) as $id)
|
||||
if(($id = intval($id))>0)
|
||||
$ids[] = $id;
|
||||
|
||||
$filter = array();
|
||||
if(!empty($ids))
|
||||
$filter['id'] = $ids;
|
||||
|
||||
$filter['sort'] = $this->request->get('sort');
|
||||
$filter['category_id'] = $this->request->get('category_id');
|
||||
$filter['page'] = $this->request->get('page');
|
||||
$filter['limit'] = $this->request->get('limit');
|
||||
|
||||
$products = array();
|
||||
foreach($this->blog->get_posts($filter) as $p)
|
||||
{
|
||||
$products[$p->id] = null;
|
||||
if($this->request->get('fields'))
|
||||
foreach($fields as $field)
|
||||
{
|
||||
if(isset($p->$field))
|
||||
$products[$p->id]->$field = $p->$field;
|
||||
}
|
||||
else
|
||||
$products[$p->id] = $p;
|
||||
}
|
||||
|
||||
$products_ids = array_keys($products);
|
||||
|
||||
if($join = $this->request->get('join'))
|
||||
{
|
||||
$join = explode(',', $join);
|
||||
if(in_array('images', $join))
|
||||
{
|
||||
foreach($this->products->get_images(array('product_id'=>$products_ids)) as $i)
|
||||
if(isset($products[$i->product_id]))
|
||||
{
|
||||
$products[$i->product_id]->images[$i->id] = $i;
|
||||
}
|
||||
}
|
||||
if(in_array('variants', $join))
|
||||
{
|
||||
foreach($this->variants->get_variants(array('product_id'=>$products_ids)) as $v)
|
||||
if(isset($products[$v->product_id]))
|
||||
$products[$v->product_id]->variants[$v->id] = $v;
|
||||
}
|
||||
if(in_array('categories', $join))
|
||||
{
|
||||
foreach($this->categories->get_products_categories(array('product_id'=>$products_ids)) as $pc)
|
||||
{
|
||||
if(isset($products[$pc->product_id]))
|
||||
{
|
||||
$products[$pc->product_id]["categories"][$pc->category_id] = $pc;
|
||||
$products[$pc->product_id]["categories"][$pc->category_id]->category = $this->categories->get_category(intval($pc->category_id));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return $products;
|
||||
}
|
||||
|
||||
public function post_product()
|
||||
{
|
||||
|
||||
if($this->request->method('POST'))
|
||||
{
|
||||
$product = json_decode($this->request->post());
|
||||
$variants = $product->variants;
|
||||
unset($product->variants);
|
||||
|
||||
$id = $this->products->add_product($product);
|
||||
|
||||
if(!empty($variants))
|
||||
{
|
||||
foreach($variants as $v)
|
||||
{
|
||||
$v->product_id = $id;
|
||||
$varinat_id = $this->variants->add_variant($v);
|
||||
}
|
||||
}
|
||||
if(!$id)
|
||||
return false;
|
||||
}
|
||||
|
||||
header("Content-type: application/json");
|
||||
header("Location: ".$this->config->root_url."/simpla/rest/products/".$id, true, 201);
|
||||
}
|
||||
|
||||
// Создать товар
|
||||
public function put_product()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
130
simpla/rest/RestCategories.php
Normal file
130
simpla/rest/RestCategories.php
Normal file
@@ -0,0 +1,130 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Simpla CMS
|
||||
*
|
||||
* @copyright 2012 Denis Pikusov
|
||||
* @link http://simplacms.ru
|
||||
* @author Denis Pikusov
|
||||
*
|
||||
*/
|
||||
|
||||
require_once('Rest.php');
|
||||
|
||||
class RestBlog extends Rest
|
||||
{
|
||||
public function fetch()
|
||||
{
|
||||
if($this->request->method('GET'))
|
||||
$result = $this->get();
|
||||
if($this->request->method('POST'))
|
||||
$result = $this->post();
|
||||
if($this->request->method('PUT'))
|
||||
$result = $this->put();
|
||||
if($this->request->method('DELETE'))
|
||||
$result = $this->delete();
|
||||
|
||||
return $this->indent(json_encode($result));
|
||||
}
|
||||
|
||||
public function get()
|
||||
{
|
||||
$fields = explode(',', $this->request->get('fields'));
|
||||
|
||||
$ids = array();
|
||||
foreach(explode(',', $this->request->get('id')) as $id)
|
||||
if(($id = intval($id))>0)
|
||||
$ids[] = $id;
|
||||
|
||||
$filter = array();
|
||||
if(!empty($ids))
|
||||
$filter['id'] = $ids;
|
||||
|
||||
$filter['sort'] = $this->request->get('sort');
|
||||
$filter['category_id'] = $this->request->get('category_id');
|
||||
$filter['page'] = $this->request->get('page');
|
||||
$filter['limit'] = $this->request->get('limit');
|
||||
|
||||
$products = array();
|
||||
foreach($this->blog->get_posts($filter) as $p)
|
||||
{
|
||||
$products[$p->id] = null;
|
||||
if($this->request->get('fields'))
|
||||
foreach($fields as $field)
|
||||
{
|
||||
if(isset($p->$field))
|
||||
$products[$p->id]->$field = $p->$field;
|
||||
}
|
||||
else
|
||||
$products[$p->id] = $p;
|
||||
}
|
||||
|
||||
$products_ids = array_keys($products);
|
||||
|
||||
if($join = $this->request->get('join'))
|
||||
{
|
||||
$join = explode(',', $join);
|
||||
if(in_array('images', $join))
|
||||
{
|
||||
foreach($this->products->get_images(array('product_id'=>$products_ids)) as $i)
|
||||
if(isset($products[$i->product_id]))
|
||||
{
|
||||
$products[$i->product_id]->images[$i->id] = $i;
|
||||
}
|
||||
}
|
||||
if(in_array('variants', $join))
|
||||
{
|
||||
foreach($this->variants->get_variants(array('product_id'=>$products_ids)) as $v)
|
||||
if(isset($products[$v->product_id]))
|
||||
$products[$v->product_id]->variants[$v->id] = $v;
|
||||
}
|
||||
if(in_array('categories', $join))
|
||||
{
|
||||
foreach($this->categories->get_products_categories(array('product_id'=>$products_ids)) as $pc)
|
||||
{
|
||||
if(isset($products[$pc->product_id]))
|
||||
{
|
||||
$products[$pc->product_id]["categories"][$pc->category_id] = $pc;
|
||||
$products[$pc->product_id]["categories"][$pc->category_id]->category = $this->categories->get_category(intval($pc->category_id));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return $products;
|
||||
}
|
||||
|
||||
public function post_product()
|
||||
{
|
||||
|
||||
if($this->request->method('POST'))
|
||||
{
|
||||
$product = json_decode($this->request->post());
|
||||
$variants = $product->variants;
|
||||
unset($product->variants);
|
||||
|
||||
$id = $this->products->add_product($product);
|
||||
|
||||
if(!empty($variants))
|
||||
{
|
||||
foreach($variants as $v)
|
||||
{
|
||||
$v->product_id = $id;
|
||||
$varinat_id = $this->variants->add_variant($v);
|
||||
}
|
||||
}
|
||||
if(!$id)
|
||||
return false;
|
||||
}
|
||||
|
||||
header("Content-type: application/json");
|
||||
header("Location: ".$this->config->root_url."/simpla/rest/products/".$id, true, 201);
|
||||
}
|
||||
|
||||
// Создать товар
|
||||
public function put_product()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
67
simpla/rest/RestOrders.php
Normal file
67
simpla/rest/RestOrders.php
Normal file
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Simpla CMS
|
||||
*
|
||||
* @copyright 2012 Denis Pikusov
|
||||
* @link http://simplacms.ru
|
||||
* @author Denis Pikusov
|
||||
*
|
||||
*/
|
||||
|
||||
require_once('Rest.php');
|
||||
|
||||
class RestOrders extends Rest
|
||||
{
|
||||
public function get()
|
||||
{
|
||||
$items = array();
|
||||
$filter = array();
|
||||
|
||||
// id
|
||||
$filter['id'] = $this->request->get('id');
|
||||
// Сортировка
|
||||
$filter['status'] = $this->request->get('status');
|
||||
// Страница
|
||||
$filter['page'] = $this->request->get('page');
|
||||
// Количество элементов на странице
|
||||
$filter['limit'] = $this->request->get('limit');
|
||||
|
||||
// Какие поля отдавать
|
||||
if($fields = $this->request->get('fields'))
|
||||
$fields = explode(',', $fields);
|
||||
|
||||
// Выбираем
|
||||
foreach($this->orders->get_orders($filter) as $item)
|
||||
{
|
||||
$items[$item->id] = new stdClass();
|
||||
if($fields)
|
||||
{
|
||||
foreach($fields as $field)
|
||||
if(isset($item->$field))
|
||||
$items[$item->id]->$field = $item->$field;
|
||||
}
|
||||
else
|
||||
$items[$item->id] = $item;
|
||||
}
|
||||
if(empty($items))
|
||||
return false;
|
||||
|
||||
// Выбранные id
|
||||
$items_ids = array_keys($items);
|
||||
|
||||
// Присоединяемые данные
|
||||
if($join = $this->request->get('join'))
|
||||
{
|
||||
$join = explode(',', $join);
|
||||
// Изображения
|
||||
if(in_array('purchases', $join))
|
||||
{
|
||||
foreach($this->orders->get_purchases(array('order_id'=>$items_ids)) as $i)
|
||||
if(isset($items[$i->order_id]))
|
||||
$items[$i->order_id]->purchases[] = $i;
|
||||
}
|
||||
}
|
||||
return array_values($items);
|
||||
}
|
||||
}
|
||||
198
simpla/rest/RestProducts.php
Normal file
198
simpla/rest/RestProducts.php
Normal file
@@ -0,0 +1,198 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Simpla CMS
|
||||
*
|
||||
* @copyright 2012 Denis Pikusov
|
||||
* @link http://simplacms.ru
|
||||
* @author Denis Pikusov
|
||||
*
|
||||
*/
|
||||
|
||||
require_once('Rest.php');
|
||||
|
||||
class RestProducts extends Rest
|
||||
{
|
||||
public function get()
|
||||
{
|
||||
$items = array();
|
||||
$filter = array();
|
||||
|
||||
// id
|
||||
$filter['id'] = array();
|
||||
foreach(explode(',', $this->request->get('id')) as $id)
|
||||
if(($id = intval($id)) > 0)
|
||||
$filter['id'][] = $id;
|
||||
|
||||
// Сортировка
|
||||
$filter['sort'] = $this->request->get('sort');
|
||||
// Категория
|
||||
$filter['category_id'] = $this->request->get('category');
|
||||
// Дата последнего изменения
|
||||
$filter['modified_from'] = $this->request->get('modified_from');
|
||||
// Бренд
|
||||
$filter['brand_id'] = $this->request->get('brand');
|
||||
// Страница
|
||||
$filter['page'] = $this->request->get('page');
|
||||
// Количество элементов на странице
|
||||
$filter['limit'] = $this->request->get('limit');
|
||||
|
||||
// Какие поля отдавать
|
||||
if($fields = $this->request->get('fields'))
|
||||
$fields = explode(',', $fields);
|
||||
// Выбираем
|
||||
foreach($this->products->get_products($filter) as $item)
|
||||
{
|
||||
$items[$item->id] = new stdClass();
|
||||
if($fields)
|
||||
{
|
||||
foreach($fields as $field)
|
||||
if(isset($item->$field))
|
||||
$items[$item->id]->$field = $item->$field;
|
||||
}
|
||||
else
|
||||
$items[$item->id] = $item;
|
||||
}
|
||||
if(empty($items))
|
||||
return false;
|
||||
|
||||
// Выбранные id
|
||||
$items_ids = array_keys($items);
|
||||
|
||||
// Присоединяемые данные
|
||||
if($join = $this->request->get('join'))
|
||||
{
|
||||
$join = explode(',', $join);
|
||||
// Изображения
|
||||
if(in_array('images', $join))
|
||||
{
|
||||
foreach($this->products->get_images(array('product_id'=>$items_ids)) as $i)
|
||||
if(isset($items[$i->product_id]))
|
||||
$items[$i->product_id]->images[] = $i;
|
||||
}
|
||||
// Варианты
|
||||
if(in_array('variants', $join))
|
||||
{
|
||||
foreach($this->variants->get_variants(array('product_id'=>$items_ids)) as $v)
|
||||
if(isset($items[$v->product_id]))
|
||||
$items[$v->product_id]->variants[] = $v;
|
||||
}
|
||||
// Категории
|
||||
$categories_ids = array();
|
||||
if(in_array('categories', $join))
|
||||
{
|
||||
foreach($this->categories->get_products_categories(array('product_id'=>$items_ids)) as $pc)
|
||||
{
|
||||
if(isset($items[$pc->product_id]))
|
||||
{
|
||||
$c = $pc;
|
||||
$c = $this->categories->get_category(intval($pc->category_id));
|
||||
unset($c->path);
|
||||
unset($c->subcategories);
|
||||
unset($c->children);
|
||||
$items[$pc->product_id]->categories[] = $c;
|
||||
$categories_ids[] = $pc->category_id;
|
||||
}
|
||||
}
|
||||
}
|
||||
// Свойства
|
||||
if(in_array('features', $join))
|
||||
{
|
||||
$features_ids = array();
|
||||
foreach($this->features->get_options(array('product_id'=>$items_ids)) as $o)
|
||||
{
|
||||
if(isset($items[$o->product_id]))
|
||||
{
|
||||
$options[$o->feature_id] = $o;
|
||||
$features_ids[] = $o->feature_id;
|
||||
}
|
||||
}
|
||||
foreach($this->features->get_features(array('id'=>$features_ids)) as $f)
|
||||
{
|
||||
if(isset($options[$f->id]))
|
||||
{
|
||||
$f->value = $o->value;
|
||||
$items[$o->product_id]->features[] = $f;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return array_values($items);
|
||||
}
|
||||
|
||||
public function post()
|
||||
{
|
||||
$product = json_decode($this->request->post());
|
||||
print_r($product);
|
||||
$variants = $product->variants;
|
||||
unset($product->variants);
|
||||
|
||||
$id = $this->products->add_product($product);
|
||||
|
||||
if(!empty($variants))
|
||||
{
|
||||
foreach($variants as $v)
|
||||
{
|
||||
$v->product_id = $id;
|
||||
$varinat_id = $this->variants->add_variant($v);
|
||||
}
|
||||
}
|
||||
if(!$id)
|
||||
return false;
|
||||
else
|
||||
return $id;
|
||||
|
||||
header("Content-type: application/json");
|
||||
header("Location: ".$this->config->root_url."/simpla/rest/products/".$id, true, 201);
|
||||
}
|
||||
|
||||
public function put()
|
||||
{
|
||||
$id = intval($this->request->get('id'));
|
||||
if(empty($id) || !$this->products->get_product($id))
|
||||
{
|
||||
header("HTTP/1.0 404 Not Found");
|
||||
exit();
|
||||
}
|
||||
|
||||
$product = json_decode($this->request->post());
|
||||
$variants = $product->variants;
|
||||
unset($product->variants);
|
||||
|
||||
$id = $this->products->update_product($id, $product);
|
||||
|
||||
if(!empty($variants))
|
||||
{
|
||||
$variants_ids = array();
|
||||
foreach($variants as $v)
|
||||
{
|
||||
$v->product_id = $id;
|
||||
|
||||
if($v->stock == '∞' || $v->stock == '')
|
||||
$v->stock = null;
|
||||
|
||||
if($v->id)
|
||||
$this->variants->update_variant($v->id, $v);
|
||||
else
|
||||
{
|
||||
$v->product_id = $id;
|
||||
$v->id = $this->variants->add_variant($v);
|
||||
}
|
||||
$variants_ids[] = $v->id;
|
||||
|
||||
// Удалить непереданные варианты
|
||||
$current_variants = $this->variants->get_variants(array('product_id'=>$id));
|
||||
foreach($current_variants as $current_variant)
|
||||
if(!in_array($current_variant->id, $variants_ids))
|
||||
$this->variants->delete_variant($current_variant->id);
|
||||
}
|
||||
}
|
||||
if(!$id)
|
||||
return false;
|
||||
else
|
||||
return $id;
|
||||
|
||||
header("Content-type: application/json");
|
||||
header("Location: ".$this->config->root_url."/simpla/rest/products/".$id, true, 201);
|
||||
}
|
||||
}
|
||||
325
simpla/rest/example.php
Normal file
325
simpla/rest/example.php
Normal file
@@ -0,0 +1,325 @@
|
||||
<?php
|
||||
|
||||
if($_SERVER['REQUEST_METHOD'] == 'POST')
|
||||
{
|
||||
|
||||
$data = str_replace('\n', '', $_POST['body']);
|
||||
$data = str_replace(' ', '', $data);
|
||||
$data = stripslashes($data);
|
||||
$request = new RestRequest($_POST['url'], $_POST['method'], $data);
|
||||
$request->setUsername('qwe');
|
||||
$request->setPassword('qwe');
|
||||
$request->execute();
|
||||
}
|
||||
|
||||
?><!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>REST API demo</title>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<h1>REST API demo</h1>
|
||||
<form method="post">
|
||||
<p>
|
||||
<labe>Method</label><br>
|
||||
<select name="method">
|
||||
<option value="GET" <?php if(isset($_POST['method']) && $_POST['method'] == 'GET') print "selected"; ?>>GET</option>
|
||||
<option value="PUT" <?php if(isset($_POST['method']) && $_POST['method'] == 'PUT') print "selected"; ?>>PUT</option>
|
||||
<option value="POST" <?php if(isset($_POST['method']) && $_POST['method'] == 'POST') print "selected"; ?>>POST</option>
|
||||
<option value="DELETE" <?php if(isset($_POST['method']) && $_POST['method'] == 'DELETE') print "selected"; ?>>DELETE</option>
|
||||
</select>
|
||||
</p>
|
||||
<p>
|
||||
<labe>URL</label><br>
|
||||
<input type="text" size="100" name="url" value="<?php if(isset($_POST['url']))print $_POST['url']; else print "http://simpla".dirname($_SERVER['REQUEST_URI'])."/products/"; ?>">
|
||||
</p>
|
||||
<p>
|
||||
<labe>Body:</label><br>
|
||||
<textarea cols="100" rows="20" name="body"><?php if(isset($_POST['body'])) print stripslashes($_POST['body']) ?></textarea><br>
|
||||
<input type="submit" value="Send">
|
||||
</p>
|
||||
<p>
|
||||
<labe>Response:</label><br>
|
||||
<textarea cols="100" rows="20" name="response"><?php if(is_object($request)) print stripslashes(indent($request->getResponseBody())); ?></textarea>
|
||||
</p>
|
||||
<p>
|
||||
<labe>Response Info:</label><br>
|
||||
<textarea cols="100" rows="20" name="response_info"><?php if(is_object($request)){ $info = $request->getResponseInfo(); stripslashes(print_r($info)); }?></textarea>
|
||||
</p>
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
||||
<?php
|
||||
|
||||
|
||||
class RestRequest
|
||||
{
|
||||
protected $url;
|
||||
protected $verb;
|
||||
protected $requestBody;
|
||||
protected $requestLength;
|
||||
protected $username;
|
||||
protected $password;
|
||||
protected $acceptType;
|
||||
protected $responseBody;
|
||||
protected $responseInfo;
|
||||
|
||||
public function __construct ($url = null, $verb = 'GET', $requestBody = null)
|
||||
{
|
||||
$this->url = $url;
|
||||
$this->verb = $verb;
|
||||
$this->requestBody = $requestBody;
|
||||
$this->requestLength = 0;
|
||||
$this->username = null;
|
||||
$this->password = null;
|
||||
$this->acceptType = 'application/json';
|
||||
$this->responseBody = null;
|
||||
$this->responseInfo = null;
|
||||
|
||||
if ($this->requestBody !== null)
|
||||
{
|
||||
$this->buildPostBody();
|
||||
}
|
||||
}
|
||||
|
||||
public function flush ()
|
||||
{
|
||||
$this->requestBody = null;
|
||||
$this->requestLength = 0;
|
||||
$this->verb = 'GET';
|
||||
$this->responseBody = null;
|
||||
$this->responseInfo = null;
|
||||
}
|
||||
|
||||
public function execute ()
|
||||
{
|
||||
$ch = curl_init();
|
||||
$this->setAuth($ch);
|
||||
|
||||
try
|
||||
{
|
||||
switch (strtoupper($this->verb))
|
||||
{
|
||||
case 'GET':
|
||||
$this->executeGet($ch);
|
||||
break;
|
||||
case 'POST':
|
||||
$this->executePost($ch);
|
||||
break;
|
||||
case 'PUT':
|
||||
$this->executePut($ch);
|
||||
break;
|
||||
case 'DELETE':
|
||||
$this->executeDelete($ch);
|
||||
break;
|
||||
default:
|
||||
throw new InvalidArgumentException('Current verb (' . $this->verb . ') is an invalid REST verb.');
|
||||
}
|
||||
}
|
||||
catch (InvalidArgumentException $e)
|
||||
{
|
||||
curl_close($ch);
|
||||
throw $e;
|
||||
}
|
||||
catch (Exception $e)
|
||||
{
|
||||
curl_close($ch);
|
||||
throw $e;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function buildPostBody ($data = null)
|
||||
{
|
||||
$data = ($data !== null) ? $data : $this->requestBody;
|
||||
$this->requestBody = $data;
|
||||
}
|
||||
|
||||
protected function executeGet ($ch)
|
||||
{
|
||||
$this->doExecute($ch);
|
||||
}
|
||||
|
||||
protected function executePost ($ch)
|
||||
{
|
||||
if (!is_string($this->requestBody))
|
||||
{
|
||||
$this->buildPostBody();
|
||||
}
|
||||
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->requestBody);
|
||||
curl_setopt($ch, CURLOPT_POST, 1);
|
||||
|
||||
$this->doExecute($ch);
|
||||
}
|
||||
|
||||
protected function executePut ($ch)
|
||||
{
|
||||
if (!is_string($this->requestBody))
|
||||
{
|
||||
$this->buildPostBody();
|
||||
}
|
||||
|
||||
$this->requestLength = strlen($this->requestBody);
|
||||
|
||||
$fh = fopen('php://memory', 'rw');
|
||||
fwrite($fh, $this->requestBody);
|
||||
rewind($fh);
|
||||
|
||||
curl_setopt($ch, CURLOPT_PATHINFO, $fh);
|
||||
curl_setopt($ch, CURLOPT_INFILE, $fh);
|
||||
curl_setopt($ch, CURLOPT_INFILESIZE, $this->requestLength);
|
||||
curl_setopt($ch, CURLOPT_PUT, true);
|
||||
|
||||
$this->doExecute($ch);
|
||||
|
||||
fclose($fh);
|
||||
}
|
||||
|
||||
protected function executeDelete ($ch)
|
||||
{
|
||||
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
|
||||
|
||||
$this->doExecute($ch);
|
||||
}
|
||||
|
||||
protected function doExecute (&$curlHandle)
|
||||
{
|
||||
$this->setCurlOpts($curlHandle);
|
||||
$this->responseBody = curl_exec($curlHandle);
|
||||
$this->responseInfo = curl_getinfo($curlHandle);
|
||||
|
||||
curl_close($curlHandle);
|
||||
}
|
||||
|
||||
protected function setCurlOpts (&$curlHandle)
|
||||
{
|
||||
curl_setopt($curlHandle, CURLOPT_TIMEOUT, 10);
|
||||
curl_setopt($curlHandle, CURLOPT_URL, $this->url);
|
||||
curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($curlHandle, CURLOPT_HTTPHEADER, array ('Accept: ' . $this->acceptType));
|
||||
}
|
||||
|
||||
protected function setAuth (&$curlHandle)
|
||||
{
|
||||
if ($this->username !== null && $this->password !== null)
|
||||
{
|
||||
curl_setopt($curlHandle, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
|
||||
curl_setopt($curlHandle, CURLOPT_USERPWD, $this->username . ':' . $this->password);
|
||||
}
|
||||
}
|
||||
|
||||
public function getAcceptType ()
|
||||
{
|
||||
return $this->acceptType;
|
||||
}
|
||||
|
||||
public function setAcceptType ($acceptType)
|
||||
{
|
||||
$this->acceptType = $acceptType;
|
||||
}
|
||||
|
||||
public function getPassword ()
|
||||
{
|
||||
return $this->password;
|
||||
}
|
||||
|
||||
public function setPassword ($password)
|
||||
{
|
||||
$this->password = $password;
|
||||
}
|
||||
|
||||
public function getResponseBody ()
|
||||
{
|
||||
return $this->responseBody;
|
||||
}
|
||||
|
||||
public function getResponseInfo ()
|
||||
{
|
||||
return $this->responseInfo;
|
||||
}
|
||||
|
||||
public function getUrl ()
|
||||
{
|
||||
return $this->url;
|
||||
}
|
||||
|
||||
public function setUrl ($url)
|
||||
{
|
||||
$this->url = $url;
|
||||
}
|
||||
|
||||
public function getUsername ()
|
||||
{
|
||||
return $this->username;
|
||||
}
|
||||
|
||||
public function setUsername ($username)
|
||||
{
|
||||
$this->username = $username;
|
||||
}
|
||||
|
||||
public function getVerb ()
|
||||
{
|
||||
return $this->verb;
|
||||
}
|
||||
|
||||
public function setVerb ($verb)
|
||||
{
|
||||
$this->verb = $verb;
|
||||
}
|
||||
}
|
||||
|
||||
function indent($json) {
|
||||
|
||||
$result = '';
|
||||
$pos = 0;
|
||||
$strLen = strlen($json);
|
||||
$indentStr = ' ';
|
||||
$newLine = "\n";
|
||||
$prevChar = '';
|
||||
$outOfQuotes = true;
|
||||
|
||||
for ($i=0; $i<=$strLen; $i++) {
|
||||
|
||||
// Grab the next character in the string.
|
||||
$char = substr($json, $i, 1);
|
||||
|
||||
// Are we inside a quoted string?
|
||||
if ($char == '"' && $prevChar != '\\') {
|
||||
$outOfQuotes = !$outOfQuotes;
|
||||
|
||||
// If this character is the end of an element,
|
||||
// output a new line and indent the next line.
|
||||
} else if(($char == '}' || $char == ']') && $outOfQuotes) {
|
||||
$result .= $newLine;
|
||||
$pos --;
|
||||
for ($j=0; $j<$pos; $j++) {
|
||||
$result .= $indentStr;
|
||||
}
|
||||
}
|
||||
|
||||
// Add the character to the result string.
|
||||
$result .= $char;
|
||||
|
||||
// If the last character was the beginning of an element,
|
||||
// output a new line and indent the next line.
|
||||
if (($char == ',' || $char == '{' || $char == '[') && $outOfQuotes) {
|
||||
$result .= $newLine;
|
||||
if ($char == '{' || $char == '[') {
|
||||
$pos ++;
|
||||
}
|
||||
|
||||
for ($j = 0; $j < $pos; $j++) {
|
||||
$result .= $indentStr;
|
||||
}
|
||||
}
|
||||
|
||||
$prevChar = $char;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
48
simpla/rest/index.php
Normal file
48
simpla/rest/index.php
Normal file
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
$time_start = microtime(true);
|
||||
|
||||
$resources = array(
|
||||
'products' => 'RestProducts',
|
||||
'orders' => 'RestOrders',
|
||||
'blog' => 'RestBlog'
|
||||
);
|
||||
|
||||
// Работаем в корне сайта
|
||||
chdir('../../');
|
||||
|
||||
// Ресурс с которым будем работать
|
||||
$resource = $_GET['resource'];
|
||||
|
||||
// Если существует соответсвующий класс
|
||||
if(isset($resources[$resource]))
|
||||
{
|
||||
$class_name = $resources[$resource];
|
||||
require_once('simpla/rest/'.$class_name.'.php');
|
||||
$rest = new $class_name();
|
||||
|
||||
// Действие с ресурсом
|
||||
if($rest->request->method('GET'))
|
||||
$result = $rest->get();
|
||||
if($rest->request->method('POST'))
|
||||
$result = $rest->post();
|
||||
if($rest->request->method('PUT'))
|
||||
$result = $rest->put();
|
||||
if($rest->request->method('DELETE'))
|
||||
$result = $rest->delete();
|
||||
|
||||
// Отдаём результат
|
||||
print json_encode($result);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Еслиь не существует соответсвующий класс
|
||||
header("HTTP/1.0 404 Not Found");
|
||||
exit();
|
||||
}
|
||||
|
||||
|
||||
// Отладка
|
||||
$time_end = microtime(true);
|
||||
$exec_time = round(($time_end-$time_start)*1000, 0);
|
||||
//print "[$exec_time ms]";
|
||||
32
simpla/rest/readme.txt
Normal file
32
simpla/rest/readme.txt
Normal file
@@ -0,0 +1,32 @@
|
||||
Список товаров
|
||||
/products/
|
||||
|
||||
Список товаров по id
|
||||
/products/1,2,3/
|
||||
|
||||
Выбор полей
|
||||
/products/?fields=id,name,body,position,created,images,variants,categories
|
||||
|
||||
Присоединение связанных данных
|
||||
/products/?fields=images,variants,categories
|
||||
|
||||
!Сортировка
|
||||
/products/?order=name
|
||||
|
||||
|
||||
!Количество
|
||||
/products/count/
|
||||
|
||||
!Сортировка
|
||||
/products/?order=name
|
||||
|
||||
!Постраничность
|
||||
/products/?page=5&limit=10
|
||||
|
||||
|
||||
По категориям
|
||||
/categories/3/products
|
||||
|
||||
!По брендам
|
||||
/brands/7/products
|
||||
|
||||
Reference in New Issue
Block a user