2017-10-15 5 views
1

Ich habe versucht, Rest Api meiner ganzen Kategorien, Produkte und alle anderen zu generieren, um es in meine mobile Anwendung zu verwenden, aber ich kann keine Lösung erhalten. Kann mir jemand das in opencart 3.0.2.0 sagen, sonst wenn opencart diese lösung nicht geben kann dann gib mir ein anderes framework, mit dem ich einfach APIs für meine daten erstellen kann?Opencart Rest API Generation

Antwort

1

das funktionierte für mich

ich eine benutzerdefinierte Controller im Ordner OpenCart api gemacht

<?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)); 
     } 
    } 
} 

Holen Sie sich die Kategorien http://yourdomain/index.php?route=api/allproducts/categories&json

Holen Sie sich alle Produkte http://yourdomain/index.php?route=api/allproducts&json