为DHL制作SoapHeader

时间:2016-09-28 16:11:48

标签: php cakephp soap dhl

我正在尝试使用嵌套参数创建 soapHeader ,如dhl开发人员门户网站中的示例所示:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
           xmlns:cis="http://dhl.de/webservice/cisbase"
           xmlns:bcs="http://dhl.de/webservices/businesscustomershipping"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Header>
    <cis:Authentification>
        <cis:user>2222222222_01</cis:user>
        <cis:signature>pass</cis:signature>
    </cis:Authentification>
</soap:Header>

我做标题的方式:

class DhlSoapClient {
public $apiUrl = 'https://cig.dhl.de/cig-wsdls/com/dpdhl/wsdl/geschaeftskundenversand-api/1.0/geschaeftskundenversand-api-1.0.wsdl';
public $dhlSandboxUrl = 'https://cig.dhl.de/services/sandbox/soap';
public $soapClient;
public $credentials;

public function buildClient($credentials, $sandbox = false)
{
    $this->soapClient = new \SoapClient($this->apiUrl, ['trace' => 1]);
    $this->credentials = $credentials;
    $this->buildAuthHeader();
    return $this->soapClient;
}

public function buildAuthHeader()
{
    $authParams = [
        'user' => $this->credentials['user'],
        'signature' => $this->credentials['signature'],
        'type' => 0
    ];
    $authvalues = new \SoapVar(new \SoapVar($authParams, SOAP_ENC_OBJECT), SOAP_ENC_OBJECT);
    $soapHeader = new \SoapHeader($this->dhlSandboxUrl, 'Authentification', $authvalues);
    $this->soapClient->__setSoapHeaders($soapHeader);
}}

所以我得到了这个客户:

object(SoapClient) {
trace => (int) 1
_soap_version => (int) 1
sdl => resource
__default_headers => [
    (int) 0 => object(SoapHeader) {
        namespace => 'https://cig.dhl.de/services/sandbox/soap'
        name => 'Authentification'
        data => object(SoapVar) {
            enc_type => (int) 301
            enc_value => object(SoapVar) {
                enc_type => (int) 301
                enc_value => [
                    'user' => '2222222222_01',
                    'signature' => 'pass'
                ]}}mustUnderstand => false}]}

结果我得到了这个标题:

<SOAP-ENV:Envelope  xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
                xmlns:ns1="http://dhl.de/webservice/cisbase" 
                xmlns:ns2="http://de.ws.intraship" 
                xmlns:ns3="https://cig.dhl.de/services/sandbox/soap">
<SOAP-ENV:Header>
    <ns3:Authentification>
        <user>2222222222_01</user>
        <signature>pass</signature>
    </ns3:Authentification>
</SOAP-ENV:Header>

我试图获得$response = $this->client->CreateShipmentDD($shipmentInfo); 我明白了:

object(SoapFault) {
faultstring => 'Authorization Required'
faultcode => 'HTTP'
[protected] message => 'Authorization Required'

问题可能是认证中的参数用户签名没有前缀,因此它会导致 SoapFault异常,但它们会自动添加,所以我必须制作嵌套 SoapHeader

1 个答案:

答案 0 :(得分:1)

我遇到了同样的问题。 public void Doit(MainPage mainPage) { mainPage.MyDisplayValue = "there"; } 用于HTTP身份验证。您需要将授权值添加到Authorization Required中的$options数组。

SoapClient::__construct($wsdlUri, $options)

您可以添加位置以选择沙箱:

$this->soapClient = new \SoapClient($this->apiUrl, 
    ['trace' => 1,
    'login' => <DHL_DEVELOPER_ID>,
    'password' => <DHL_DEVELOPER_PASSWD>,
    ]
);

$this->soapClient = new \SoapClient($this->apiUrl, ['trace' => 1, 'login' => <DHL_DEVELOPER_ID>, 'password' => <DHL_DEVELOPER_PASSWD>, 'location' => <DHL_SANDBOX_URL>|<DHL_PRODUCTION_URL>, 'soap_version' => SOAP_1_1, 'encoding' => 'utf-8', ] ); 不是邮件地址。你可以在“我的帐户”中找到它。

相关问题