Opencart Rest API生成 [英] Opencart Rest API generation
本文介绍了Opencart Rest API生成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图生成我所有类别,产品和所有其他产品的Rest Api,以将其用于我的移动应用程序,但是我没有任何解决方案.谁能在opencart 3.0.2.0中告诉我,否则,如果opencart无法提供此解决方案,那么给我另一个框架,通过它我可以轻松地为数据创建API?
I tried to generate Rest Api of my whole categories, products and all others to use it into my mobile application but I can't get any solution. Can anyone tell me that in opencart 3.0.2.0, otherwise if opencart can't give this solution then give me other framework by which I can easily create APIs for my data?
推荐答案
这对我有用
我在OpenCart api文件夹中创建了一个自定义控制器
I made a custom controller in the OpenCart api folder
<?php
class ControllerApiAllproducts extends Controller {
private $error = array();
public function index()
{
$products = array();
$this->load->language('catalog/product');
$this->load->model('catalog/product');
$this->load->model('api/product');
$this->load->model('tool/image');
$error[]['no_json']= "No JSON";
// preg_match("/[^\/]+$/", $_SERVER['REQUEST_URI'], $matches);
// $last_word = $matches[0];
$product_total = $this->model_catalog_product->getTotalProducts();
$results = $this->model_catalog_product->getProducts();
foreach ($results as $result) {
if (is_file(DIR_IMAGE . $result['image'])) {
$image = $this->model_tool_image->resize($result['image'], 40, 40);
} else {
$image = $this->model_tool_image->resize('no_image.png', 40, 40);
}
$special = false;
$product_specials = $this->model_api_product->getProductSpecials($result['product_id']);
foreach ($product_specials as $product_special) {
if (($product_special['date_start'] == '0000-00-00' || strtotime($product_special['date_start']) < time()) && ($product_special['date_end'] == '0000-00-00' || strtotime($product_special['date_end']) > time())) {
$special = $product_special['price'];
break;
}
}
$shop_products['shop_products'][] = array(
'product_id' => $result['product_id'],
'image' => $image,
'name' => $result['name'],
'model' => $result['model'],
'price' => $result['price'],
'special' => $special,
'quantity' => $result['quantity'],
'status' => $result['status']
);
}
if (isset($this->request->get['json'])) {
echo json_encode($shop_products);
die;
} else {
$this->response->setOutput(json_encode($error));
}
}
public function product()
{
$this->load->language('catalog/product');
$this->load->model('catalog/product');
$this->load->model('api/product');
$this->load->model('tool/image');
$product_details = array();
$error['fail'] = 'Failed';
if (isset($this->request->get['product_id'])) {
//$product_details['product_id'] = $this->request->get['product_id'];
$product_details = $this->model_catalog_product->getProduct($this->request->get['product_id']);
echo json_encode($product_details);
die;
} else {
$this->response->setOutput(json_encode($error));
}
}
public function categories()
{
$shop_categories = array();
$this->load->model('catalog/category');
$error['fail'] = 'Failed';
if (isset($this->request->get['json'])) {
$shop_categories =$this->model_catalog_category->getCategories();
echo json_encode($shop_categories);
die;
} else {
$this->response->setOutput(json_encode($error));
}
}
}
获取类别 http://yourdomain/index.php?route = api/allproducts/categories& json
获取所有产品 http://yourdomain/index.php?route = api/allproducts&json
这篇关于Opencart Rest API生成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文