LoginSignup
4
2

More than 5 years have passed since last update.

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

Posted at

以前、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ファイルがあるので
こいつら読んでれば理解が深まると思われ。

4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2