Search code examples
phpsoapwsdlpostman

SOAP Request works with Postman, not in PHP


I am trying to call a function that is made available by slovak post office through SOAP.

They published their WSDL at https://mojezasielky.posta.sk/integration/webServices/api?wsdl

I have already registered and sucessfully used their services by creating a XML that is sent through Postman. Although I have a problem to make it work in PHP - it always returns an error stating that user was not found which does not make sense (the error being SOAP-ENV:Server user_not_found).

In php I am trying to call it this way (does not work):

$params = array(
    'apiKey' => "123abc",
    'userId' => "abc123",
    'EPH' => $ephString // the long XML specified later in the question. I know I should use objects, this is just for testing purposes

);
try {
    $wsdl = 'https://mojezasielky.posta.sk/integration/webServices/api';

    $client = new SoapClient($wsdl, $params);
    $requestResponse = ($client->__soapCall("importSheet", $params));
    echo $requestResponse;
} catch (SoapFault $exception) {
    echo $exception;
}

Here is my XML request (don't mind the comments, it just explains what specific values are)

<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">
    <Body>
        <importSheetRequest xmlns="http://mojezasielky.posta.sk/api">
            <auth>
                <userId>idOfUserAcc</userId>
                <apiKey>theSuperSecretAPiKey</apiKey>
            </auth>
            <EPH>
               <InfoEPH>
                  <Mena>EUR</Mena>
                  <TypEPH>1</TypEPH> <!-- 1 = EPH - vzdy sa zadava len cislo 1 pri podaji -->
                  <PocetZasielok>1</PocetZasielok> <!-- Tu tiez necham cislo 1, vzdy budem po jednom podavat-->
                  <DruhZasielky>1</DruhZasielky> <!-- 1 doporuceny, 2 poisteny, 8 expres kurier, 15 easy express 1, 16 easy express 10, pozor, pre nemecko nesmie byt poisteny! -->
                  <Odosielatel>
                     <OdosielatelID />
                     <Meno>Name Surname</Meno>
                     <Organizacia>Company s.r.o.</Organizacia>
                     <Ulica>Narodna 1</Ulica>
                     <Mesto>Bratislava</Mesto>
                     <PSC>85101</PSC>
                     <Krajina>SK</Krajina>
                     <Email>[email protected]</Email>
                  </Odosielatel>
               </InfoEPH>
               <Zasielky>
                  <Zasielka>
                     <Adresat>
                        <Meno>zahranicie test bez cislo uctu</Meno>
                        <Organizacia /> <!-- povinna len ak je vyplnene meno + moze ostat prazdna -->
                        <Ulica>cokolvek</Ulica>
                        <Mesto>hocico</Mesto>
                        <PSC>10000</PSC>
                        <Krajina>DE</Krajina> <!-- 2znakovy iso kod -->
                     </Adresat>
                     <Info>
                        <CenaPoistneho>100</CenaPoistneho>
                     </Info>
                  </Zasielka>
               </Zasielky>
            </EPH>
        </importSheetRequest>
    </Body>
</Envelope>

Postman headers configuration (works with the XML specified) Postman headers configuration

How should I use PHPs soap client to make it work?


Solution

  • You have wrong arguments (auth node should contain userId and apiKey). Try following:

    $wsdl = 'https://mojezasielky.posta.sk/integration/webServices/api?wsdl';
    $args = [
        'auth' => [
            'userId' => 'YOUR_SECRET_ID',
            'apiKey' => 'YOUR_SECRET_KEY',
        ],
        'EPH' => $ephString,
        'contract' => false,
    ];
    $soap = new SoapClient($wsdl);
    $response = $soap->importSheet($args);
    

    See also EPH API manual and technical specification for eHarok.