opencart 类别 api 返回 {"category":null} 错误

问题描述

我创建了 api 来从 opencart 获取类别列表以用于 android 应用程序。已经有很多类别列表可用,但是当我尝试点击 api 时收到错误消息 "{"category":null}"

目录->控制器->api

代码如下:

<?PHP

class ControllerApicategory extends Controller
{
public function index() 
{
        
        $this->load->model('catalog/category');
        $this->load->model('tool/image');

        $json = array();
        $json['category'] = array();
        $filter_data = array();
         
         $results = $this->model_catalog_category->getCategory($filter_data);
        # -- $_GET params ------------------------------
        
        if (isset($this->request->get['id'])) {
            $category_id = $this->request->get['id'];
        } else {
            $category_id = 0;
        }

        # -- End $_GET params --------------------------

        
        
        $json['category'] = array(
            'id'                    => $category['category_id'],'name'                  => $category['name'],'description'           => $category['description'],'href'                  => $this->url->link('product/category','category_id=' . $result['category_id'])
        );

        $json['category'] = $data['category'];
        $this->response->addHeader('Content-Type: application/json');
        $this->response->setoutput(json_encode($json));
        }
    }

解决这个问题。

解决方法

有两个错误

  1. getCategory 函数接受整数参数

$json[‘category’]= $data[‘category’];

公共函数索引() {

    $this->load->model('catalog/category');
    $this->load->model('tool/image');

    $json = array();
    $json['category'] = array();
 
    
    if (isset($this->request->get['id'])) {
        $category_id = $this->request->get['id'];
    } else {
        $category_id = 0;
    }

     $category = $this->model_catalog_category->getCategory($category_id);
    
    $json['category'] = array(
        'id'                    => $category['category_id'],'name'                  => $category['name'],'description'           => $category['description'],'href'                  => $this->url->link('product/category','category_id=' . $category['category_id'])
   );
    $this->response->addHeader('Content-Type: application/json');
    $this->response->setOutput(json_encode($json));
    }
}