Opencart 的 REST API 生成

我试图为我的客户、类别、产品、优惠券、制造商和所有其他人生成一个 REST API,以便在我的 Android 应用程序中使用它,但我无法获得任何解决方案。

谁能告诉我如何在 opencart 3.0.2.0 中创建 API?


幕布斯7119047
浏览 153回答 1
1回答

潇潇雨雨

在目录 > 控制器 > api文件夹中制作自定义控制器创建控制器文件名:allproducts.php您可以复制以下代码并粘贴allproducts.php<?phpclass ControllerApiAllproducts extends Controller {&nbsp; &nbsp; private $error = array();&nbsp; &nbsp; // All Products&nbsp; &nbsp; public function index(){&nbsp; &nbsp; &nbsp; &nbsp; $products = array();&nbsp; &nbsp; &nbsp; &nbsp; $this->load->language('catalog/product');&nbsp; &nbsp; &nbsp; &nbsp; $this->load->model('catalog/product');&nbsp; &nbsp; &nbsp; &nbsp; $this->load->model('tool/image');&nbsp; &nbsp; &nbsp; &nbsp; $error[]['no_json']= "No JSON";&nbsp; &nbsp; &nbsp; &nbsp; $product_total = $this->model_catalog_product->getTotalProducts();&nbsp; &nbsp; &nbsp; &nbsp; $results = $this->model_catalog_product->getProducts();&nbsp; &nbsp; &nbsp; &nbsp; foreach ($results as $result) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (is_file(DIR_IMAGE . $result['image'])) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $image = $this->model_tool_image->resize($result['image'], 40, 40);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $image = $this->model_tool_image->resize('no_image.png', 40, 40);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $special = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $product_specials = $this->model_catalog_product->getProductSpecials($result['product_id']);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach ($product_specials&nbsp; as $product_special) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //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())) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $special = $product_special['price'];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $shop_products['shop_products'][] = array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'product_id' => $result['product_id'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'image'&nbsp; &nbsp; &nbsp; => $image,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'name'&nbsp; &nbsp; &nbsp; &nbsp;=> $result['name'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'model'&nbsp; &nbsp; &nbsp; => $result['model'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'price'&nbsp; &nbsp; &nbsp; => $result['price'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'special'&nbsp; &nbsp; => $special,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'quantity'&nbsp; &nbsp;=> $result['quantity'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'status'&nbsp; &nbsp; &nbsp;=> $result['status']&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (isset($this->request->get['json'])) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo json_encode($shop_products);die;&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this->response->setOutput(json_encode($error));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp;&nbsp; &nbsp; }&nbsp; &nbsp; // Product info Page&nbsp; &nbsp; public function productInfo(){&nbsp; &nbsp; &nbsp; &nbsp; $this->load->language('catalog/product');&nbsp; &nbsp; &nbsp; &nbsp; $this->load->model('catalog/product');&nbsp; &nbsp; &nbsp; &nbsp; $this->load->model('tool/image');&nbsp; &nbsp; &nbsp; &nbsp; $product_details = array();&nbsp; &nbsp; &nbsp; &nbsp; $error['fail'] = 'Failed';&nbsp; &nbsp; &nbsp; &nbsp; if (isset($this->request->get['product_id'])) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //$product_details['product_id'] = $this->request->get['product_id'];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $product_details = $this->model_catalog_product->getProduct($this->request->get['product_id']);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo json_encode($product_details);die;&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this->response->setOutput(json_encode($error));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; // Category Listing Page&nbsp; &nbsp; public function categories(){&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $shop_categories = array();&nbsp; &nbsp; &nbsp; &nbsp; $this->load->model('catalog/category');&nbsp; &nbsp; &nbsp; &nbsp; $error['fail'] = 'Failed';&nbsp; &nbsp; &nbsp; &nbsp; if (isset($this->request->get['json'])) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $shop_categories =$this->model_catalog_category->getCategories();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo json_encode($shop_categories);die;&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this->response->setOutput(json_encode($error));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; // Product Listing By Category&nbsp; &nbsp; public function categoryList(){&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $this->load->model('catalog/category');&nbsp; &nbsp; &nbsp; &nbsp; $this->load->model('catalog/product');&nbsp; &nbsp; &nbsp; &nbsp; $this->load->model('tool/image');&nbsp; &nbsp; &nbsp; &nbsp; $error['fail'] = 'Failed';&nbsp; &nbsp; &nbsp; &nbsp; if (isset($this->request->get['path'])) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $url = '';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $path = '';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $parts = explode('_', (string)$this->request->get['path']);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $category_id = (int)array_pop($parts);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach ($parts as $path_id) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!$path) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $path = (int)$path_id;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $path .= '_' . (int)$path_id;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $category_info = $this->model_catalog_category->getCategory($path_id);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $category_id = 0;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; $category_info = $this->model_catalog_category->getCategory($category_id);&nbsp; &nbsp; &nbsp; &nbsp; if ($category_info) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $url = '';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //$data['categories'] = array();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $results = $this->model_catalog_category->getCategories($category_id);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach ($results as $result) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $filter_data = array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'filter_category_id'&nbsp; => $result['category_id'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'filter_sub_category' => true&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $products = array();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $filter_data = array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'filter_category_id' => $category_id&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $product_total = $this->model_catalog_product->getTotalProducts($filter_data);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $products = $this->model_catalog_product->getProducts($filter_data);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo json_encode($products); die;&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this->response->setOutput(json_encode($error));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; // All Manufacturers Listing&nbsp; &nbsp; public function manufactureList() {&nbsp; &nbsp; &nbsp; &nbsp; $this->load->model('catalog/manufacturer');&nbsp; &nbsp; &nbsp; &nbsp; $this->load->model('tool/image');&nbsp; &nbsp; &nbsp; &nbsp; $error['fail'] = 'Failed';&nbsp; &nbsp; &nbsp; &nbsp; $manufactureList = array();&nbsp; &nbsp; &nbsp; &nbsp; if (isset($this->request->get['json'])) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $manufactureList = $this->model_catalog_manufacturer->getManufacturers();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo json_encode($manufactureList);die;&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this->response->setOutput(json_encode($error));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; // Manufactur info Page&nbsp; &nbsp; public function manufactureInfo() {&nbsp; &nbsp; &nbsp; &nbsp; $this->load->model('catalog/manufacturer');&nbsp; &nbsp; &nbsp; &nbsp; $this->load->model('catalog/product');&nbsp; &nbsp; &nbsp; &nbsp; $this->load->model('tool/image');&nbsp; &nbsp; &nbsp; &nbsp; $error['fail'] = 'Failed';&nbsp; &nbsp; &nbsp; &nbsp; if (isset($this->request->get['manufacturer_id'])) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $manufactureInfo = $this->model_catalog_manufacturer->getManufacturer($manufacturer_id);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo json_encode($product_details);die;&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this->response->setOutput(json_encode($error));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; // Category Listing Page&nbsp; &nbsp; public function specialProduct(){&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $specialProduct = array();&nbsp; &nbsp; &nbsp; &nbsp; $this->load->model('catalog/product');&nbsp; &nbsp; &nbsp; &nbsp; $this->load->model('tool/image');&nbsp; &nbsp; &nbsp; &nbsp; $error['fail'] = 'Failed';&nbsp; &nbsp; &nbsp; &nbsp; if (isset($this->request->get['json'])) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $specialProduct = $this->model_catalog_product->getProductSpecials();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo json_encode($specialProduct);die;&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this->response->setOutput(json_encode($error));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}我制作了以下 API 及其路径(请参阅 Postman 应用程序):所有产品:http ://examples.com/index.php?route=api/allproducts&json产品编号:http ://examples.com/index.php?route=api/allproducts/productInfo&json&product_id=30所有类别:http ://examples.com/index.php?route=api/allproducts/categories&json分类明智的产品:http ://examples.com/index.php?route=api/allproducts/categoryList&json&path=25_28所有制造商:http ://examples.com/index.php?route=api/allproducts/manufactureList&json制造商 ID:http ://examples.com/index.php?route=api/allproducts/manufactureInfo&manufacturer_id=11特殊产品:http ://examples.com/index.php?route=api/allproducts/specialProduct&json
打开App,查看更多内容
随时随地看视频慕课网APP