lgDroidZ
lgDroidZ

Reputation: 43

how to send SOAP request in curl

I am a Beginner of soap, How do i send soap request? i searched in google and tried different methods but sadly it didn't worked for me.

i really appreciated your help.

here's the sample request that i should send:

POST /Universal/WebService.asmx HTTP/1.1
Host: www.sample.com
Content-Type: text/xml;charset="utf-8"
Content-Length: length
SOAPAction: https://www.sample.com/Universal/WebService.asmx/Request

<?xml version=\"1.0\" encoding=\"utf-8\"?>
<soap:Envelope xmlns:xsi="http://wwww.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<Request xmlns="https://www.sample.com/Universal/WebService.asmx">
<ID>CPHK01</ID>
<UID>TEST</UID>
<PWD>TEST</PWD>
<target_mpn>09183530925</target_mpn>
<amount>115</amount>
<ref_no>20060830143030112</ref_no>
<source_mpn>67720014</source_mpn>
</Request>
</soap:Body>
</soap:Envelope>

here's the response:

 HTTP/1.1 200 OK
 Content-Type: text/xml; charset=utf-8
 Content-Length: length

 <?xml version = "1.0" encoding="utf-8"?>
 <soap:Envelope xmlns:xsi="http://wwww.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
 <soap:Body>
 <RequestResponse xmlns="https://www.sample.com/Universal/WebService.asmx">
 <RequestResult>
 <Ref_no>20060830143030112</Ref_no>
 <StatusCode>101</StatusCode>
 </RequestResult>
 </RequestResponse>
 </soap:Body>
 </soap:Envelope>

Upvotes: 4

Views: 15463

Answers (1)

MrCode
MrCode

Reputation: 64526

PHP has a native SoapClient class. The constructor takes the WSDL as an argument. This is preferred to cURL because the SoapClient handles all of the SOAP intricacies and it allows you to deal with native objects and arrays and removes the need to manually build the SOAP envelope and XML.

try {
    $client = new SoapClient('https://www.sample.com/Universal/WebService.asmx?wsdl');
    $response = $client->Request(array(
        'ID' => 'xxxx',
        'UID' => 'xxxx',
        'PWD' => 'xxxx',
        'target_mpn' => 'xxxx',
        'amount' => 'xxxx',
        'ref_no' => 'xxxx',
        'source_mpn' => 'xxxx'
    ));

    print_r($response); // view the full response to see what is returned

    // or get the response properties:
    echo $response->RequestResult->Ref_no;
    echo $response->RequestResult->StatusCode;

} catch (Exception $e) {
    echo $e->getMessage();
}

Upvotes: 4

Related Questions