r/PHPhelp • • Aug 01 '25

Solved SOAP request is empty

Hello community,
I have to deal with the dreaded EU API, and unfortunately, they use a SOAP API.
Well – I always find that terrible, but what can you do.
In any case, I'm now facing the problem that the generated XML seems to be correct (a test via Postman worked as expected), but the response from the SoapClient in PHP is always empty. Even the request via ->__getLastRequest is empty.
Does anyone here have an idea?

$doc = new DOMDocument('1.0', 'UTF-8');

>/*SOME DOM-Generating-Code*/>

$client = new SoapClient($this->wsdl, [
    'trace' => true,
    'exceptions' => true,
    'cache_wsdl' => 
WSDL_CACHE_NONE
]);

$request = $doc->saveXML();

$client->__doRequest(
            $doc->saveXML(),
            $this->url,
            $action,
            SOAP_1_1
        );

Request-Header-Output:

POST /tracesnt/ws/EudrEchoService HTTP/1.1
Host: acceptance.eudr.webcloud.ec.europa.eu
Connection: Keep-Alive
User-Agent: PHP-SOAP/7.4.33
Content-Type: text/xml; charset=utf-8
SOAPAction: "testEcho"
Content-Length: 1811

getLastResponse Output: null

Response-Header-Output

HTTP/1.1 200 OK
Content-Length: 926
Content-Type: text/xml; charset=utf-8
Date: Fri, 01 Aug 2025 10:09:58 GMT
Server: European-Commission
Set-Cookie: cookie-sticky-cls=1754042999.733.653.787235|*****************; Expires=Fri, 01-Aug-25 11:09:58 GMT; Max-Age=3600; Path=/; Secure; HttpOnly
Strict-Transport-Security: max-age=31536000; includeSubDomains
X-Oracle-Dms-Ecid: eff030ea-55bc-4fc2-862a-02e5c9d5bca0-000dcea0
X-Oracle-Dms-Rid: 0

getLastResponse Output: null

//EDIT:

However, when I send the request using a cURL call, I get the expected response.

2 Upvotes

5 comments sorted by

View all comments

Show parent comment

2

u/michawb Aug 05 '25

Hey,

After a lot of testing and problems with the namespaces and especially the prefixes (<S:BODY> etc.) I have now built the following small function that solves the problem with the namespaces differently - maybe someone else can use it

Here the XML-Test

<?xml version="1.0" encoding="UTF-8"?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
  <S:Header>
    <ns2:Security xmlns:ns1="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:ns2="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
      <ns1:TimestampType>
        <ns1:Created>2025-08-05T13:32:12.298+02:00</ns1:Created>
        <ns1:Expires>2025-08-05T13:32:17.299+02:00</ns1:Expires>
      </ns1:TimestampType>
    </ns2:Security>
  </S:Header>
  <S:Body>
    <ns3:EudrEchoResponse xmlns:ns2="http://ec.europa.eu/sanco/tracesnt/base/v4" xmlns:ns3="http://ec.europa.eu/tracesnt/eudr/echo" xmlns:ns4="http://ec.europa.eu/tracesnt/body/v3" xmlns:ns5="urn:un:unece:uncefact:codelist:standard:ISO:ISO2AlphaLanguageCode:2006-10-27">
      <ns3:status>User XXX, using third party system eudr-test and IP address XXX.XXX.XXX.XXX says: Hello World</ns3:status>
    </ns3:EudrEchoResponse>
  </S:Body>
</S:Envelope>

The function

/**
     * Traversing all XML-Elements and return array
     * @param SimpleXMLElement $element
     * @param array $array
     * @return array|mixed
     */
    public function traverseAllNamespaces(SimpleXMLElement $element, array $array = []) {

        $name = $element->getName();
        $value = trim((string)$element);

        if($value !== ''){
            $array[$name] = $value;

        }else{

            // get all namespaces
            $namespaces = $element->getNamespaces(true);

            // loop all childs in namespaces
            foreach ($namespaces as $ns) {

                foreach ($element->children($ns) as $child) {

                    $childName = $child->getName();
                    $childValue = trim((string)$child);

                    if($childValue !== ''){
                        $array[$childName] = $childValue;
                    }else{
                        $array[$childName] = array();
                        $array[$childName] = $this->traverseAllNamespaces($child, $array[$childName]);
                    }

                }

            }

        }

        return $array;
    }

and the output:

Array
(
    [Header] => Array
        (
            [Security] => Array
                (
                    [TimestampType] => Array
                        (
                            [Created] => 2025-08-05T13:32:12.298+02:00
                            [Expires] => 2025-08-05T13:32:17.299+02:00
                        )

                )

        )

    [Body] => Array
        (
            [EudrEchoResponse] => Array
                (
                    [status] => User XXX, using third party system eudr-test and IP address XXX.XXX.XXX.XXX says: Hello World
                )

        )

)