Help us understand the problem. What is going on with this article?

EC-CUBE(v2)でapi使ってajax

More than 3 years have passed since last update.

以前、EC-CUBEでAjax って書いたけど、
やっぱり自前で作るのはよろしくないので
備わってるAPI機能を使ってやることにした。
というか知らなかった。

template.js
(function ($) {
    function ajaxPost(param) {
        $.ajax({
            type: "POST",
            url:  "/api/json.php",
            data: { 'Operation': 'MyOperation', 'Service': 'none', 'param': param },
            dataType: "json",
            success: function(response) {
                var s = response.MyOperation.success;
                var r = response.MyOperation.response;
                if (!s) {
                    alert(r.join("\n"));
                    return;
                }
                //any
            },
            error: function(xhr, ts, et) {
                console.log(ts);
                console.log(et);
            }
        });
    }
}(jQuery));
/data/class/api/operations/MyOperation.php
<?php
class API_MyOperation extends SC_Api_Abstract_Ex {

    protected $operation_name = 'MyOperation';
    protected $operation_description = 'いろいろ';
    protected $default_auth_types = self::API_AUTH_TYPE_OPEN;
    protected $default_enable = '1';
    protected $default_is_log = '0';
    protected $default_sub_data = '';

    private $response;
    private $errors;
    private $success;

    public function doAction($arrParam) {
        $param = $arrParam['param'];

        if ( empty($param) ) {
            $this->errors[] = "paramないやで";
        } else {
            $this->response = $param;
        }

        $this->arrResponse = [
                'success' => empty($this->errors),
                'response' => empty($this->errors) ? $this->response : $this->errors,
        ];
        return true;
    }

    public function getRequestValidate() {
        return ['DefaultResponse' => []];
    }

    public function getResponseGroupName() {
        return 'MyOperation';
    }

    protected function lfInitParam(&$objFormParam) {
    }

}

AmazonのAPIを元に作成したらしい。
各「MyOperation」を適宜変更してご利用ください。

$this->arrResponse の入れ方はWordpress方式でやってますので
これまた気に入らない方は適宜変更を。

$default_auth_types

認証レベルを設定できる

/data/class/api/SC_Api_Abstract.php
<?php
    const API_AUTH_TYPE_REFERER = '1';          // リファラー
    const API_AUTH_TYPE_SESSION_TOKEN = '2';    // CSRF TOKEN
    const API_AUTH_TYPE_API_SIGNATURE = '3';    // API 署名認証 推奨
    const API_AUTH_TYPE_CUSTOMER = '4';         // 会員認証
    const API_AUTH_TYPE_MEMBER = '5';           // 管理者認証
    const API_AUTH_TYPE_CUSTOMER_LOGIN_SESSION = '6';   // 顧客ログインセッションが有効
    const API_AUTH_TYPE_MEMBER_LOGIN_SESSION = '7';     // 管理者ログインセッションが有効
    const API_AUTH_TYPE_IP = '8';               // IP認証
    const API_AUTH_TYPE_HOST = '9';             // ホスト認証
    const API_AUTH_TYPE_SSL = '10';             // SSL強制
    const API_AUTH_TYPE_OPEN = '99';            // 完全オープン

/data/class/api/operations ディレクトリに
既存APIファイルがあるので
こいつら読んでれば理解が深まると思われ。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした