如何从大量的市场服务获得toket

时间:2015-01-31 08:45:44

标签: soap token

我如何在大型市场获得代币。此代码返回了http://www.pickwick-shop.de/plenty/api/soap/version113/?xml等静态页面 我需要来自大型市场的令牌来进行其他行动。

< ?xml version="1.0" encoding="UTF-8"? >
<soap-env:envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.pickwick-shop.de/plenty/api/soap/version113/?xml" xmlns:ns2="GetAuthentificationToken" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap-enc="http://schemas.xmlsoap.org/soap/encoding/" soap-env:encodingstyle="http://schemas.xmlsoap.org/soap/encoding/">
<soapenv:Body>
  <ver:GetAuthentificationToken>
     <oLogin xsi:type="ver:PlentySoapRequest_GetAuthentificationToken">
        <!--You may enter the following 2 items in any order-->
        <Username xsi:type="xsd:string">Username</Username>
        <Userpass xsi:type="xsd:string">Password</Userpass>
     </oLogin>
  </ver:GetAuthentificationToken>

 </soap-env:envelope>

1 个答案:

答案 0 :(得分:1)

<?php

class PlentySoapClient extends SoapClient{

    protected $wsdl;
    protected $user;
    protected $pass;
    protected $userId;
    protected $token;

    public function __construct($wsdl, $user, $pass){
        parent::__construct($wsdl);
        $this->wsdl =   $wsdl;
        $this->user =   $user;
        $this->pass =   $pass;

        //  Set token to protected $token
        //  you will need token and userId (you get it in response GetAuthentificationToken)
        //  so store token as array or object with fields token and userId
        $this->token    =   $this->setToken();

        //  Every call has to have token in SOAP Header
        //  so you have to set it and use everytime you call to Plenty
        //  so all traffic has to go through this class
        $this->setPlentySoapHeader();

        //  after setting SOAP Header you can call for example GetItemsBase
        //  authorization will be inside SOAP Header, so you just create query and use __soapCall
    }

    public function setToken(){
        if($this->isTokenValid()){
            $this->token    =   $this->getToken();
        }else{
            $this->token    =   $this->createToken();
        }
    }

    public function createToken(){
        $callQuery  =   new StdClass();
        $callQuery->Username    =   $this->user;
        $callQuery->Userpass    =   $this->pass;

        $response   =   parent::__soapCall('GetAuthentificationToken',array($callQuery));

        //  Here somehow store your token, token is
        //  valid from midnight to midnight and you
        //  can call for token only 30 times per day
        //  per user so be careful. Get it and store
        //  then use from storage (file, DB etc.).
        //  After 30 calls user is blocked till midnight!

        return $token;
    }

    public function getToken(){
        // Here get token form stored data and return it

        return $token;
    }

    public function isTokenValid(){
        // Here check if stored token is valid (is created today)

        return $isValid;
    }

    public function setPlentySoapHeader(){
        $aHeader            =   array(
            'UserID'    => $this->token['userId'],
            'Token'     => $this->token['token']
        );

        $oSoapHeaderVar     =   new SoapVar($aHeader, SOAP_ENC_OBJECT);
        $sNamespaceHeader   =   "Authentification";
        $oSoapHeader        =   new SoapHeader($sNamespaceHeader,'verifyingToken', $oSoapHeaderVar, false);
        $this->__setSoapHeaders($oSoapHeader);
    }
}

这应该可以解决您的问题。如果您需要更多帮助,例如模特,一些其他问题,请不要犹豫。我和Plenty Soap一起工作了4年。上面的代码不是我使用的,也是4岁,但仍然有效(应该可以是一些拼写错误)。