Search code examples
phphttpwsdlsoap-client

SOAP WSDL request - Method returns bad request error


I'm trying to make a soap call and it returns a "Bad request" error.

The example call is:

    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://centric.eu/services/CS/Trade/Standard/WS/" xmlns:cen="http://schemas.datacontract.org/2004/07/Centric.CS.Trade.Standard.WS.StockService.Contract.Request">
<soapenv:Header>
    <ws:Security soapenv:mustUnderstand="1" xmlns:ws="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
        <ws:UsernameToken>
            <ws:Username>username</ws:Username>
            <ws:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">pass</ws:Password>
         </ws:UsernameToken>
    </ws:Security>
</soapenv:Header>
   <soapenv:Body>
      <ws:GetStock>
         <ws:request>
            <cen:StockRequests>
               <!--Zero or more repetitions:-->
               <cen:StockRequest>
                  <cen:CustomerNo>123</cen:CustomerNo>
                  <cen:Division>AGU_NL</cen:Division>
                  <cen:Item>113504</cen:Item>
                  <cen:Language>NL</cen:Language>
                  <cen:Login>123</cen:Login>
               </cen:StockRequest>
            </cen:StockRequests>
         </ws:request>
      </ws:GetStock>
   </soapenv:Body>
</soapenv:Envelope>

I use the following code:

$soapclient = new \SoapClient($url, array(
            'compression' => SOAP_COMPRESSION_ACCEPT | SOAP_COMPRESSION_GZIP | 9,
            'trace' => true,
            'exceptions' => 1,
            'cache_wsdl' => 1,
        ));

$xml = '
        <ws:Security soapenv:mustUnderstand="1" xmlns:ws="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
            <ws:UsernameToken>
                <ws:Username>'.$username.'</ws:Username>
                <ws:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">'.$password.'</ws:Password>
            </ws:UsernameToken>
    </ws:Security>
        ';

        $soapheader = new \SoapHeader(
            'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd',
            'Security',
            new \SoapVar($xml, XSD_ANYXML),
            true);
        $soapclient->__setSoapHeaders($soapheader);

try {
            $soapclient->__soapCall('GetStock', 
                array(
                    'CustomerNo' => 123,
                    'Division' => 'AGU_NL',
                    'Item' => '113504',
                    'Language' => 'NL',
                    'Login' => '123',
                )
            );
        } catch(\SoapFault $e) {
            echo '<pre>';
            print_r($e->getMessage());
            echo '</pre>';
        }

The response I get is: fault code: HTTP, fault string: Bad Request I am not entirely sure whether I created the request and called the method correctly.

Any help will be appreciated

Thanks


Solution

  • You are probably better off not trying to construct the xml manually. Try something along these lines instead:

    $url = 'https://webservices.abcb2b.eu/Centric/CS/Trade/csprod/StockService.svc?wsdl';
    $username = 'username';
    $password = 'pass';
    $client = new SoapClient($url, array('trace' => 1, "exception" => 0));
    
    $wssNamespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd";
    $usernameToken = new SoapVar(array(
        new SoapVar(array(
            new SoapVar($username, XSD_STRING, null, null, 'Username', $wssNamespace),
            new SoapVar($password, XSD_STRING, null, null, 'Password', $wssNamespace)
        ), SOAP_ENC_OBJECT, null, null, 'UsernameToken', $wssNamespace)
    ), SOAP_ENC_OBJECT, null, null, null, $wssNamespace);
    $client->__setSoapHeaders(new SoapHeader($wssNamespace, 'Security', $usernameToken));
    
    
    try {
        $client->GetStock(array(
            'request' => array(
                'StockRequests' => array(
                    'StockRequest' => array(
                        'CustomerNo' => 123,
                        'Division' => 'AGU_NL',
                        'Item' => '113504',
                        'Language' => 'NL',
                        'Login' => '123',
                    )
                )
            )
        ));
    } catch(\SoapFault $e) {
        echo '<pre>';
        print_r($e->getMessage());
        echo '</pre>';
    }