Search code examples
laravelsoapsoap-client

How to make SOAP request in Laravel?


enter image description here

This request requires Basic Auth: user and password.

enter image description here

Headers with two fields.

enter image description here

and xml body.

This API is tested working in Postman. But I am not sure how to make this work in Laravel, I only work with JSON before.

Any suggestion how can I get started? or any package I can use?


Solution

  • You can use http post

     $http=Http::withHeaders([
                'Content-Type'=>'application/xml',
                'SOAPAction'=>'balance'
            ])->post(url,$xmlBodyContent);
        
        
        
            return response($http->body())
                ->withHeaders([
                    'Content-Type' => 'text/xml'
                ]);
    

    Another way is to use withBody method

    $http=Http::withHeaders([
                'Content-Type'=>'application/xml',
                'SOAPAction'=>'balance'
            ])->withBody($xmlBodyContent,"text/xml")->post(url);
    

    for xml doc notation Ref:https://www.php.net/manual/en/simplexml.examples-basic.php

    To generate array to xml,use array-to-xml