Search code examples
phplaravelcurlxmlhttprequestsoap-client

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>`;

Curl Process

$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);

the 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"}

the output should be an xml response so what is wrong? I am new with soap request.


Solution

  • Perhaps the $xmlRequest variable is not being set properly because it's a multiline string.

    Instead of

    $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>`
    

    You could try using the heredoc notation

    $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;
    

    If it were up to me, I'd move the xml to a view.

    $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>