laravel 中的 DHL Soap 请求

DHL Soap request in laravel

$url = "https://wsbexpress.dhl.com:443/gbl/expressRateBook";
$action = "euExpressRateBook_providerServices_ShipmentHandlingServices_Binder_getRateRequest";
$xmlRequest = `<?xml version='1.0' encoding='UTF-8'?>
<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' xmlns:rat='http://scxgxtt.phx-dc.dhl.com/euExpressRateBook/RateMsgRequest'>
    <soapenv:Header>
        <wsse:Security soapenv:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
            <wsse:UsernameToken wsu:Id="UsernameToken-5" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
                <wsse:Username>*******</wsse:Username>
                <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">*********</wsse:Password>
            </wsse:UsernameToken>
        </wsse:Security>
    </soapenv:Header>
    <soapenv:Body>
        <rat:RateRequest>
            <RequestedShipment>
                <DropOffType>REGULAR_PICKUP</DropOffType>
                <NextBusinessDay>Y</NextBusinessDay>
                <RequestValueAddedServices>Y</RequestValueAddedServices>
                <Ship>
                    <Shipper>
                        <City>NANTES</City>
                        <PostalCode>44000</PostalCode>
                        <CountryCode>FR</CountryCode>
                    </Shipper>
                    <Recipient>
                        <City>BEIJING</City>
                        <PostalCode>100001</PostalCode>
                        <CountryCode>CN</CountryCode>
                    </Recipient>
                </Ship>
                <Packages>
                    <RequestedPackages number='1'>
                        <Weight>
                            <Value>10.300</Value>
                        </Weight>
                        <Dimensions>
                            <Length>1.00</Length>
                            <Width>1.00</Width>
                            <Height>1.00</Height>
                        </Dimensions>
                    </RequestedPackages>
                </Packages>
                <ShipTimestamp>2020-09-30T17:00:00GMT+01:00</ShipTimestamp>
                <UnitOfMeasurement>SI</UnitOfMeasurement>
                <Content>NON_DOCUMENTS</Content>
                <PaymentInfo>DAP</PaymentInfo>
                <Account>224055879</Account>
            </RequestedShipment>
        </rat:RateRequest>
    </soapenv:Body>
</soapenv:Envelope>`;

卷曲过程

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept:text/xml","Content-Type: text/xml;charset=UTF-8", 'SOAPAction:' . $action));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xmlRequest);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
            
$output = curl_exec($ch);
var_dump($output);

输出:

    HTTP/1.1 500 Internal Server Error
    Date: Tue, 29 Jun 2021 17:29:11 GMT
    Server: 
    Content-Length: 0
    Connection: close
    X-CorrelationID: Id-6758db6070d8c5c722cf8f44 0
    Accept: text/xml
    ClientSide: 3.250.135.63
    SOAPAction: euExpressRateBook_providerServices_ShipmentHandlingServices_Binder_getRateRequest
    Strict-Transport-Security: max-age=31536000; includeSubDomains
    X-Content-Type-Options: nosniff
    X-XSS-Protection: 1; mode=block
    Content-Type: text/xml;charset=UTF-8
    Set-Cookie: BIGipServer~WSB~pl_wsb-express-chd.dhl.com_443=546130085.64288.0000; path=/; Httponly; Secure
    
    bool(true)
    {"status":"SUCCESS"}

输出应该是一个 xml 响应,所以有什么问题吗?我是新来的 soap 请求。

也许 $xmlRequest 变量没有正确设置,因为它是一个多行字符串。

而不是

$xmlRequest = `<?xml version='1.0' encoding='UTF-8'?>
<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' xmlns:rat='http://scxgxtt.phx-dc.dhl.com/euExpressRateBook/RateMsgRequest'>
    <soapenv:Header>
        <wsse:Security soapenv:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
            <wsse:UsernameToken wsu:Id="UsernameToken-5" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
                <wsse:Username>*******</wsse:Username>
                <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">*********</wsse:Password>
            </wsse:UsernameToken>
        </wsse:Security>
    </soapenv:Header>
    <soapenv:Body>
        <rat:RateRequest>
            <RequestedShipment>
                <DropOffType>REGULAR_PICKUP</DropOffType>
                <NextBusinessDay>Y</NextBusinessDay>
                <RequestValueAddedServices>Y</RequestValueAddedServices>
                <Ship>
                    <Shipper>
                        <City>NANTES</City>
                        <PostalCode>44000</PostalCode>
                        <CountryCode>FR</CountryCode>
                    </Shipper>
                    <Recipient>
                        <City>BEIJING</City>
                        <PostalCode>100001</PostalCode>
                        <CountryCode>CN</CountryCode>
                    </Recipient>
                </Ship>
                <Packages>
                    <RequestedPackages number='1'>
                        <Weight>
                            <Value>10.300</Value>
                        </Weight>
                        <Dimensions>
                            <Length>1.00</Length>
                            <Width>1.00</Width>
                            <Height>1.00</Height>
                        </Dimensions>
                    </RequestedPackages>
                </Packages>
                <ShipTimestamp>2020-09-30T17:00:00GMT+01:00</ShipTimestamp>
                <UnitOfMeasurement>SI</UnitOfMeasurement>
                <Content>NON_DOCUMENTS</Content>
                <PaymentInfo>DAP</PaymentInfo>
                <Account>224055879</Account>
            </RequestedShipment>
        </rat:RateRequest>
    </soapenv:Body>
</soapenv:Envelope>`

您可以尝试使用 heredoc 表示法

$xmlRequest = <<<XML
<?xml version='1.0' encoding='UTF-8'?>
<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' xmlns:rat='http://scxgxtt.phx-dc.dhl.com/euExpressRateBook/RateMsgRequest'>
    <soapenv:Header>
        <wsse:Security soapenv:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
            <wsse:UsernameToken wsu:Id="UsernameToken-5" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
                <wsse:Username>*******</wsse:Username>
                <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">*********</wsse:Password>
            </wsse:UsernameToken>
        </wsse:Security>
    </soapenv:Header>
    <soapenv:Body>
        <rat:RateRequest>
            <RequestedShipment>
                <DropOffType>REGULAR_PICKUP</DropOffType>
                <NextBusinessDay>Y</NextBusinessDay>
                <RequestValueAddedServices>Y</RequestValueAddedServices>
                <Ship>
                    <Shipper>
                        <City>NANTES</City>
                        <PostalCode>44000</PostalCode>
                        <CountryCode>FR</CountryCode>
                    </Shipper>
                    <Recipient>
                        <City>BEIJING</City>
                        <PostalCode>100001</PostalCode>
                        <CountryCode>CN</CountryCode>
                    </Recipient>
                </Ship>
                <Packages>
                    <RequestedPackages number='1'>
                        <Weight>
                            <Value>10.300</Value>
                        </Weight>
                        <Dimensions>
                            <Length>1.00</Length>
                            <Width>1.00</Width>
                            <Height>1.00</Height>
                        </Dimensions>
                    </RequestedPackages>
                </Packages>
                <ShipTimestamp>2020-09-30T17:00:00GMT+01:00</ShipTimestamp>
                <UnitOfMeasurement>SI</UnitOfMeasurement>
                <Content>NON_DOCUMENTS</Content>
                <PaymentInfo>DAP</PaymentInfo>
                <Account>224055879</Account>
            </RequestedShipment>
        </rat:RateRequest>
    </soapenv:Body>
</soapenv:Envelope>
XML;

如果由我决定,我会将 xml 移至视图。

$params = [
    'username' => $soap_username,
    'password' => $soap_password,
    ... // other params for the xml
];

$url = "https://wsbexpress.dhl.com:443/gbl/expressRateBook";
$action = "euExpressRateBook_providerServices_ShipmentHandlingServices_Binder_getRateRequest";
$xmlRequest = view('soap.getRateRequest', $params)->render();

resources/view/soap/getRateRequest.blade.php

<?xml version='1.0' encoding='UTF-8'?>
<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' xmlns:rat='http://scxgxtt.phx-dc.dhl.com/euExpressRateBook/RateMsgRequest'>
    <soapenv:Header>
        <wsse:Security soapenv:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
            <wsse:UsernameToken wsu:Id="UsernameToken-5" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
                <wsse:Username>{{ $username }}</wsse:Username>
                <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">{{ $password }}</wsse:Password>
            </wsse:UsernameToken>
        </wsse:Security>
    </soapenv:Header>
    <soapenv:Body>
        <rat:RateRequest>
            <RequestedShipment>
                <DropOffType>REGULAR_PICKUP</DropOffType>
                <NextBusinessDay>Y</NextBusinessDay>
                <RequestValueAddedServices>Y</RequestValueAddedServices>
                <Ship>
                    <Shipper>
                        <City>NANTES</City>
                        <PostalCode>44000</PostalCode>
                        <CountryCode>FR</CountryCode>
                    </Shipper>
                    <Recipient>
                        <City>BEIJING</City>
                        <PostalCode>100001</PostalCode>
                        <CountryCode>CN</CountryCode>
                    </Recipient>
                </Ship>
                <Packages>
                    <RequestedPackages number='1'>
                        <Weight>
                            <Value>10.300</Value>
                        </Weight>
                        <Dimensions>
                            <Length>1.00</Length>
                            <Width>1.00</Width>
                            <Height>1.00</Height>
                        </Dimensions>
                    </RequestedPackages>
                </Packages>
                <ShipTimestamp>2020-09-30T17:00:00GMT+01:00</ShipTimestamp>
                <UnitOfMeasurement>SI</UnitOfMeasurement>
                <Content>NON_DOCUMENTS</Content>
                <PaymentInfo>DAP</PaymentInfo>
                <Account>224055879</Account>
            </RequestedShipment>
        </rat:RateRequest>
    </soapenv:Body>
</soapenv:Envelope>