Search code examples
phplaravelsoapguzzle

Laravel 8 guzzle SOAP request returns error 500


I'm trying to do a POST call with SOAP in Laravel 8 to get some data, but if I put headers on the call, the remote server returns error 500.

Also if no headers are defined, I receive an empty response.

Here is the code I used:

try {
    $data = $this->generateXml($get_last_day);
    $client = new Client(['verify' => false ]);
                
    $headers = [    
       'SOAPAction' => "https://api.xyz.com/DataService/PostData",
       'Content-Type' => "text/xml"
    ];
    
    $get_invoices_only_paid = $client->request('POST', config('constants.service.URL'), [
       'headers' => $headers,
       'body' => $data
    ]);
    
    Log::debug(json_encode($get_invoices_only_paid->getStatusCode()));
    dd($get_invoices_only_paid->getStatusCode());
           
} catch (Exception $e) {
    dd($e->getCode(), $e->getMessage());
}

Here is the SOAP XML I passed:

public function generateXml($date)
{
    // $xml = new SimpleXMLElement('<inf:getInvoicingTransactions xmlns:inf="http://infoservice.webservice.as24.com/"><date>'.$date->format("Y-m-d").'</date></inf:getData>');
    $xml = new SimpleXMLElement('<inf:getInvoicingTransactions xmlns:inf="http://infoservice.webservice.as24.com/"><date>2024-04-15</date></inf:getData>');

    $customXML = new SimpleXMLElement($xml->asXML());
    $dom = dom_import_simplexml($customXML);
    $cleanXml = $dom->ownerDocument->saveXML($dom->ownerDocument->documentElement);

    $soapHeader = '<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"><wsse:UsernameToken wsu:Id="UsernameToken-16" 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></SOAP-ENV:Header><soap:Body>';
    $soapFooter = '</soap:Body></soap:Envelope>';
    $xmlRequest = $soapHeader . $cleanXml . $soapFooter;

    
    return $xmlRequest;
}

and the error after the call:

500
"""
Server error: `POST https://services.as24.com:8445/infoservice/services/infoservice` resulted in a `500 500` response:\n
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><soap:Fault><faultcode>soap:Server</fau (truncated...)\n
"""

The same request in Postman works perfectly with the same headers and body data.


Solution

  • The problem is solved by editing the request adding in header ‘Connection’ => ‘keep-alive’ and adding the xml body directly from file, maybe it is a problem in the format that php generates the xml, and the keep alive allows to keep the call until it returns the data, because the response is quite big.

    This is the final code:

        try {
            // $data = $this->generateXml($get_last_day);
       
            $client = new Client(['verify' => false ]);
                       
            $headers = [    
                'Content-Type' => "text/xml",
                'Connection' => 'keep-alive',
                'Content-Length' => '816',
                'Accept-Encoding' => 'gzip, deflate, br',
                'Accept' => '*/*',
            ];
           
            $data = file_get_contents('soap-request.xml');
    
            $get_invoices_only_paid = $client->request('POST', config('constants.AS24.URL'), [
               'headers' => $headers,
               'body' => $data
            ]);
           
            $jsonFormated = $this->xmlToJson($get_invoices_only_paid->getBody());
    
                   
        } catch (Exception $e) {
            dd($e->getCode(), $e->getMessage());
        }