Qiita Conference 2025

桜庭洋之 (@zaru)

一緒に働きたくなるプログラマの思想

2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

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

Qiita Conference 2025 will be held!: 4/23(wed) - 4/25(Fri)

Qiita Conference is the largest tech conference in Qiita!

Keynote Speaker

ymrl、Masanobu Naruse, Takeshi Kano, Junichi Ito, uhyo, Hiroshi Tokumaru, MinoDriven, Minorun, Hiroyuki Sakuraba, tenntenn, drken, konifar

View event details
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?