1

我有使用 REST-API 将产品添加到 Magento 的脚本

我的代码:

<?php
class RestApiProduct {

    private $productData;
    private $consumerKey;
    private $consumerSecret;
    private $secret;
    private $token;
    private $config;
    private $authType;
    private $resourceUrl;
    private $headerType = array('Content-Type' => 'application/json');

    public $responceInfo;
    public $responce;
    public $oauthError;
    public $productID;

    function __construct($configFile='config.ini'){
        $this->config = parse_ini_file($configFile, true);
        if(count($this->config)<5) {
            $this->responce="Please configure with getAccessToken setup";
            return false;
        }

        $this->basepath = $this->config['basepath'];
        $this->consumerKey = $this->config['consumerKey'];
        $this->consumerSecret = $this->config['consumerSecret'];
        $this->secret = $this->config['secret'];
        $this->token = $this->config['token'];
        $this->authType = OAUTH_AUTH_TYPE_AUTHORIZATION;
        $this->resourceUrl = $this->basepath.'api/rest/products';
    }

    function setProduct($data){
        $this->productData=json_encode($data);
    }

    function saveProduct(){
        try {
            $oauthClient = new OAuth($this->consumerKey, $this->consumerSecret, OAUTH_SIG_METHOD_HMACSHA1, $this->authType);
            $oauthClient->enableDebug();
            $oauthClient->setToken($this->token, $this->secret);

            $oauthClient->fetch($this->resourceUrl, $this->productData, "POST", $this->headerType);

            $this->responceInfo = $oauthClient->getLastResponseInfo();
            $this->responce = $oauthClient->getLastResponse();

        } catch (OAuthException $e) {
            $this->oauthError=$e;
        }
    }
}

$restObj= new RestApiProduct();
$restObj->setProduct($productArray);
$restObj->saveProduct();


$restObj2= new RestApiProduct('secondConfig.ini');
$restObj2->setProduct($productArray);
$restObj2->saveProduct();

?>

现在,如果我有两个配置文件(一个接一个),其中有两个不同的消费者密钥和秘密配置,也可以访问令牌和秘密。

而不是如何识别哪个消费者添加了哪个产品?

4

0 回答 0