2

我正在研究 Opencart API (opencart v2.3),并按照此链接获取文档 ( Opencart )。但是没有关于 opencart API 以及如何使用它的数据,所以我按照其他网站的步骤并使用该代码在调用登录 api 时收到此消息,成功:API 会话成功启动!

但是,每当我使用另一个 API 在购物车中添加产品或查看购物车或添加订单时,我都会收到权限问题。我调试代码,发现它需要会话app_id并且当我检查时,它只存储token,而不是app_id

我使用通过谷歌搜索找到的以下代码。
常见的.php

<?php
function do_curl_request($url, $params=array()) {
  $ch = curl_init();
  curl_setopt($ch,CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_COOKIEJAR, 'E:\practice\oc2.3\tmp\apicookie.txt');
  curl_setopt($ch, CURLOPT_COOKIEFILE, 'E:\practice\oc2.3\tmp\apicookie.txt');

  $params_string = '';
  if (is_array($params) && count($params)) {
    foreach($params as $key=>$value) {
      $params_string .= $key.'='.$value.'&'; 
    }
    rtrim($params_string, '&');

    curl_setopt($ch,CURLOPT_POST, count($params));
    curl_setopt($ch,CURLOPT_POSTFIELDS, $params_string);
  }

  //execute post
  $result = curl_exec($ch);

  //close connection
  curl_close($ch);

  return $result;
}

登录.php

<?php
require "common.php";

// set up params
$url = 'http://opencart2_3.local/index.php?route=api/restopencart/login';

$fields = array(
  'key' => 'FpafURRNAHgVcaUXZozahVdEOV7mtp1Q0ejvAMAIAfiZyVqIptqZ2uV9eQvT3PytlzELULH1vQwLKikFGBOm3yky1rTuFO6sEi0eBkH1y6WgpaNWIsB0ZMiRCCbGCBZZak2uR1CBg0TpOzcbevXWGStvoUsaKgl0B3OKRoHk6mRj7e6S63HJQzQksbbz0JfCuZsY9cvhY4ArQPzNf3XfrdgE3nTG5hYQCXaKPVqtS3R2Vqr4sazwjgXYajy7h6Dv',
);

$json = do_curl_request($url, $fields);
$data = json_decode($json);
if (isset($data->token)) {
  $token = $data->token;
}
var_dump($data);

add_product.php

<?php
require "common.php";

// set up params
$url = 'http://opencart2_3.local/index.php?route=api/restopencart/addproduct';
$fields = array(
  'product_id' => '32',
  'quantity' => '1',
  'option[226]' => '15'
);

$json = do_curl_request($url, $fields);
$data = json_decode($json);
var_dump($data);

客户接口

public function index() {
    $this->load->language('api/customer');

    // Delete past customer in case there is an error
    unset($this->session->data['customer']);

    $json = array();

    if (!isset($this->session->data['api_id'])) {
        $json['error']['warning'] = $this->language->get('error_permission');
    } else {
        // Add keys for missing post vars
        $keys = array(
            'customer_id',
            'customer_group_id',
            'firstname',
            'lastname',
            'email',
            'telephone',
            'fax'
        );

        foreach ($keys as $key) {
            if (!isset($this->request->post[$key])) {
                $this->request->post[$key] = '';
            }
        }

        // Customer
        if ($this->request->post['customer_id']) {
            $this->load->model('account/customer');

            $customer_info = $this->model_account_customer->getCustomer($this->request->post['customer_id']);

            if (!$customer_info || !$this->customer->login($customer_info['email'], '', true)) {
                $json['error']['warning'] = $this->language->get('error_customer');
            }
        }

        if ((utf8_strlen(trim($this->request->post['firstname'])) < 1) || (utf8_strlen(trim($this->request->post['firstname'])) > 32)) {
            $json['error']['firstname'] = $this->language->get('error_firstname');
        }

        if ((utf8_strlen(trim($this->request->post['lastname'])) < 1) || (utf8_strlen(trim($this->request->post['lastname'])) > 32)) {
            $json['error']['lastname'] = $this->language->get('error_lastname');
        }

        if ((utf8_strlen($this->request->post['email']) > 96) || (!filter_var($this->request->post['email'], FILTER_VALIDATE_EMAIL))) {
            $json['error']['email'] = $this->language->get('error_email');
        }

        if ((utf8_strlen($this->request->post['telephone']) < 3) || (utf8_strlen($this->request->post['telephone']) > 32)) {
            $json['error']['telephone'] = $this->language->get('error_telephone');
        }

        // Customer Group
        if (is_array($this->config->get('config_customer_group_display')) && in_array($this->request->post['customer_group_id'], $this->config->get('config_customer_group_display'))) {
            $customer_group_id = $this->request->post['customer_group_id'];
        } else {
            $customer_group_id = $this->config->get('config_customer_group_id');
        }

        // Custom field validation
        $this->load->model('account/custom_field');

        $custom_fields = $this->model_account_custom_field->getCustomFields($customer_group_id);

        foreach ($custom_fields as $custom_field) {
            if (($custom_field['location'] == 'account') && $custom_field['required'] && empty($this->request->post['custom_field'][$custom_field['custom_field_id']])) {
                $json['error']['custom_field' . $custom_field['custom_field_id']] = sprintf($this->language->get('error_custom_field'), $custom_field['name']);
            } elseif (($custom_field['location'] == 'account') && ($custom_field['type'] == 'text') && !empty($custom_field['validation']) && !filter_var($this->request->post['custom_field'][$custom_field['custom_field_id']], FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => $custom_field['validation'])))) {
                $json['error']['custom_field' . $custom_field['custom_field_id']] = sprintf($this->language->get('error_custom_field'), $custom_field['name']);
            }
        }

        if (!$json) {
            $this->session->data['customer'] = array(
                'customer_id'       => $this->request->post['customer_id'],
                'customer_group_id' => $customer_group_id,
                'firstname'         => $this->request->post['firstname'],
                'lastname'          => $this->request->post['lastname'],
                'email'             => $this->request->post['email'],
                'telephone'         => $this->request->post['telephone'],
                'fax'               => $this->request->post['fax'],
                'custom_field'      => isset($this->request->post['custom_field']) ? $this->request->post['custom_field'] : array()
            );

            $json['success'] = $this->language->get('text_success');
        }
    }

    if (isset($this->request->server['HTTP_ORIGIN'])) {
        $this->response->addHeader('Access-Control-Allow-Origin: ' . $this->request->server['HTTP_ORIGIN']);
        $this->response->addHeader('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
        $this->response->addHeader('Access-Control-Max-Age: 1000');
        $this->response->addHeader('Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With');
    }

    $this->response->addHeader('Content-Type: application/json');
    $this->response->setOutput(json_encode($json));
}
4

2 回答 2

0

将令牌放在您的请求 URL 之后将使其正常工作。

假设返回的令牌api/loginKYMmXA4Bcj8nL9WD3nl0oalaJOL1KSKo

add_product.php

<?php
require "common.php";

// set up params
$url = 'http://opencart2_3.local/index.php?route=api/restopencart/addproduct&token=KYMmXA4Bcj8nL9WD3nl0oalaJOL1KSKo';
$fields = array(
  'product_id' => '32',
  'quantity' => '1',
  'option[226]' => '15'
);

$json = do_curl_request($url, $fields);
$data = json_decode($json);
var_dump($data);
于 2017-04-20T17:41:23.993 回答
0

确保将服务器的 IP 地址添加到允许的 IP 地址中。

要检查这一点,请转到System → Users → API然后编辑那个Default

在那里,单击IP Address选项卡并插入服务器 IP 地址。

要获取服务器 IP 地址,可以使用以下命令行:

$ curl ipinfo.io/ip
于 2019-10-07T13:17:06.827 回答