LoginSignup
2
1

More than 5 years have passed since last update.

Amazon MWSのサンプルコードでClass not found

Last updated at Posted at 2016-09-05

Amazon MWSとは

Amazonマーケットプレイスに出店してるショップ向けに、商品管理や注文管理ができるAPIが提供されている。

Amazon マーケットプレイスWebサービス(MWS)
https://developer.amazonservices.jp/

クライアントライブラリとサンプルコードもある。

PHP クライアントライブラリ
https://developer.amazonservices.jp/doc/bde/feeds/v20090101/php.html

今回はフィードAPIを使用してる。

クラスのオートロードでうまくいかなかった

このライブラリのサンプルコードは__autoload関数でオートロードしてるため、
composerとかを使ってたらどうもMWSのクラスが見つからない。

PHP Fatal error:  Class 'MarketplaceWebService_Client' not found in hoge.php on line 99

そこでspl_autoload_registerを使うように修正した。

元のコードがこれ

src/MarketplaceWebService/Samples/.config.inc.php
     function __autoload($className){
        $filePath = str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
        $includePaths = explode(PATH_SEPARATOR, get_include_path());
        foreach($includePaths as $includePath){
            if(file_exists($includePath . DIRECTORY_SEPARATOR . $filePath)){
                require_once $filePath;
                return;
            }
        }
    }

修正後はこちら

    function MWSAutoLoad($className) {
        $filePath = str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
        $includePaths = explode(PATH_SEPARATOR, get_include_path());
        foreach($includePaths as $includePath){
            if(file_exists($includePath . DIRECTORY_SEPARATOR . $filePath)){
                require_once $filePath;
                return;
            }
        }
    }

    if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
        spl_autoload_register('MWSAutoLoad', true, true);
    } else {
        spl_autoload_register('MWSAutoLoad');
    }
2
1
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
2
1