Php soap client xml request example

In this post, i will show you How to send soap envelope XML requests in PHP. No need to declare a long request array, just simple pass soap xml as request using header information. The soap envelope contains the headers and soap body. You need to call a function or method on a web server with some parameters to process the request.

Some users might get confused when seeing the soap XML request format. Like they use SOAPAction URL value as request action or don’t get the header parameters. In this tutorial, we are going to clear your confusion.

Let’s see how soap envelope XML look like.

Soap Envelope XML Request Format is like that:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

POST/webserviceexternal/contracts.asmx HTTP/1.1

Host:example.com

Content-Type:text/xml;charset=utf-8

Content-Length: length

SOAPAction:"http://example.com/CreateItems"

xml version="1.0"encoding="utf-8"?>

    <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:xsd="http://www.w3.org/2001/XMLSchema"

xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">

        <soap:Body>

            <extLoginData xmlns="http://www.JOI.com/schemas/ViaSub.WMS/">

            <Login>string</Login>

            <Password>string</Password>

            </extLoginData>

            <Items xmlns="http://www.JOI.com/schemas/ViaSub.WMS/">

            <Item>

            <SKU>int</SKU>

            <Description>string</Description>

            <CustomerID>int</CustomerID>

            <InventoryMethod>string</InventoryMethod>

            </Item>

            </Items>

            <warnings xmlns="http://www.JOI.com/schemas/ViaSub.WMS/">string</warnings>

        </soap:Body>

    </soap:Envelope>

As you see above Host, Content-Type, Content-Length, and SoapAction, all are the header parameters.

And right below is the XML request string you need to pass. Make sure you put the right value in XML parameter element.

You May Also Like:

How to Authenticate with D&B Direct API Web Service
How to Implement UPS API and Create Shipping Label
How to Get User Data From Slideshare.net API
How to Send Order Data in Shipstation Via API Call

How to Post Soap Envelop XML Request in PHP

We can post soap XML using curl method. Define the curl request header and post the soap envelop XML data. Check the below code to execute this request.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

$xml_post_string='xml version="1.0"encoding="utf-8"?>

    <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"

xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">

        <soap:Body>

            <extLoginData xmlns="http://www.JOI.com/schemas/ViaSub.WMS/">

            <Login>user</Login>

            <Password>password</Password>

            </extLoginData>

            <Items xmlns="http://www.JOI.com/schemas/ViaSub.WMS/">

            <Item>

            <SKU>test123</SKU>

            <Description>sample item</Description>

            <CustomerID>123</CustomerID>

            <InventoryMethod>FIFO</InventoryMethod>

            </Item>

            </Items>

            <warnings xmlns="http://www.JOI.com/schemas/ViaSub.WMS/">string</warnings>

        </soap:Body>

    </soap:Envelope>';

$headers = array(

"Content-Type: text/xml; charset=utf-8",

"Host: example.com",

"Content-length: ".strlen($xml_post_string),

"SOAPAction: http://example.com/CreateItems"

);

$url = 'https://secure-example.com/sample/contracts.asmx?op=CreateItems';  // WSDL web service url for request method/function

$ch =curl_init();

curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0);

curl_setopt($ch,CURLOPT_URL,$url);

curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);

curl_setopt($ch,CURLOPT_TIMEOUT,10);

curl_setopt($ch, CURLOPT_POST,true);

curl_setopt($ch,CURLOPT_POSTFIELDS,$xml_post_string);// the SOAP request

curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);

echo$response=curl_exec($ch);

curl_close($ch)

You need to change SOAPaction and request URL as per soap envelop request pattern and call method this curl method in PHP. This is quite easy to implement this soap api call.

  • How to Convert HTML to PDF Document in PHP using fpdf
  • Visitor Counter in PHP for Website
  • Multiple Star Rating Feature on Single Page in PHP, MySQL and Ajax

About Harish

I am professional web developer and blogger. Use this blog to share own api and other web development experience with you. I do accept paid work. Write to me at -

View all posts by Harish