How to call soap api using php? - php

I have following soap response. How can i call soap using php.Any Idea. Thanks in Advance.
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<wsse:Security xmlns:wsse="http://schemas.xmlsoap.org/ws/2002/07/secext">
<wsse:UsernameToken>
<wsse:Username>XXXXXXXXXXXX</wsse:Username>
<wsse:Password>XXXXXXXXXXXXXXXXX</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>
</soap:Header>
<soapenv:Body>
<cus1:GetCustomerDetailsRequest xmlns:cus1="XXXXXXXXXXXXXXXXXXXXXXX" xmlns:com="XXXXXXXXXXXXXXXXXXXXXXXX" xmlns:cus="XXXXXXXXXXXXXXXXX">
<cus:GetCustomerDetails>
<AccountMSISDN>1234567890</AccountMSISDN>
</cus:GetCustomerDetails>
</cus1:GetCustomerDetailsRequest>
</soapenv:Body>
</soapenv:Envelope>

You should be able to have a play around with the fields, parameters and methods you need by using a free online soap tool like this:
http://www.soapclient.com/soaptest.html
Also, you should have a look at this answer: SOAP request in PHP with CURL

$soapUrl = "http://www.example.com/masterdata/masterdata.asmx?wsdl";
$soapUser = "username"; // username
$soapPassword = "password"; // password
// xml post structure
$xml_post_string = 'soap string'; // data from the form, e.g. some ID number
$headers = array(
"Content-type: text/xml;charset=\"utf-8\"",
"Accept: text/xml",
"Cache-Control: no-cache",
"Pragma: no-cache",
"SOAPAction: http://tempuri.org/GetMasterData",
"Content-length: ".strlen($xml_post_string),
); //SOAPAction: your op URL
$url = $soapUrl;
// PHP cURL for https connection with auth
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, $soapUser.":".$soapPassword); // username and password - declared at the top of the doc
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
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);
// converting
$response = curl_exec($ch);
curl_close($ch);
// a new dom object
$dom = new domDocument('1.0', 'utf-8');
// load the html into the object
$dom->loadXml($response);
$array = json_decode($dom->textContent,TRUE);
print_r($array);

Related

How to send XML to wsdl endpoint

I hope you all doing well. I need little help with SOAP API.
I want to send XML and I have WSDL endpoint. Can someone help me, how to do this? help would be appreciated.
First, what would be a good approach CURL and soapClient?
This is the endpoint
http://some_ip/some_channel/services/some_service?wsdl
And the XML is here:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:Body>
<ban:someMthod>
<chnlNum>somenumber</chnlNum>
<chnlPasWd>some password</chnlPasWd>
<userNum>some number</userNum>
<amount>some amount</amount>
<requestDate>some date</requestDate>
<requestId>some random number</requestId>
</ban:someMethod>
</soapenv:Body>
</soapenv:Envelope>
The whole request will look like this.
$soapurl = "http://some_ip_endpoint";
$date = date("Y-m-d h:i:sa");
$requestID = (new DateTime())->getTimestamp();
// Make your own custom request.
$xml_post_string = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:Body>
<ban:someMthod>
<chnlNum>somenumber</chnlNum>
<chnlPasWd>some password</chnlPasWd>
<userNum>some number</userNum>
<amount>some amount</amount>
<requestDate>' . $date . '</requestDate>
<requestId>' . $requestID . '</requestId>
</ban:someMethod>
</soapenv:Body>
</soapenv:Envelope>';
// Don't for get to mention a proper header
$headers = array(
"Content-Type: text/xml; charset=UTF-8",
"Pragma: no-cache",
"Content-length: " . strlen($xml_post_string),
"Connection: Keep-Alive"
);
// The actual curl request.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $soapurl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
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);
// Get a response and filter it out accordingly
$response = curl_exec($ch);
curl_close($ch);
$xml = $response;
$xml = preg_replace("/(<\/?)(\w+):([^>]*>)/", '$1$2$3', $xml);
$xml = simplexml_load_string($xml);
$json = json_encode($xml);

Execute multiple parallel cURL against the same URL?

I need to execute 2 requests in parallel using cURL to get a reply from the web service.
The problem is that I need to get the encrypted password from the first XML's output and pass it to the second XML to get 100 success response from the API.
Currently, I have created 2 cURL to achieve this but the API responds "101 Password Expired" because the encrypted password is valid only for the first request.
Here is my code for reference:
1st cURL:
$soapUrl = "http://localhost:54934/frmMutualFund.asmx?op=getPassword"; // asmx URL of WSDL
// xml post structure
$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>
<getPassword xmlns="http://tempuri.org/">
<pUserId>12345</pUserId>
<pPassword>98765</pPassword>
<pPasskey>ksjhdfksj</pPasskey>
</getPassword>
</soap:Body>
</soap:Envelope>'; // data from the form, e.g. some ID number
$headers = array(
"Content-type: text/xml;charset=\"utf-8\"",
"Accept: text/xml",
"Cache-Control: no-cache",
"Pragma: no-cache",
"SOAPAction: http://tempuri.org/getPassword",
"Content-length: ".strlen($xml_post_string),
); //SOAPAction: your op URL
$url = $soapUrl;
// PHP cURL for https connection with auth
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//curl_setopt($ch, CURLOPT_USERPWD, $soapUser.":".$soapPassword); // username and password - declared at the top of the doc
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
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);
// converting
$response = curl_exec($ch);
2nd cURL:
$soapUrl = "http://localhost:54934/frmMutualFund.asmx"; // asmx URL of WSDL
// xml post structure
$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>
<MFUAPI xmlns="http://tempuri.org/">
<pFlag>06</pFlag>
<pUserId>12345</pUserId>
<pEncPassword>'.$response.'</pEncPassword>
<pParam>some_parameters</pParam>
</MFUAPI>
</soap:Body>
</soap:Envelope>'; // data from the form, e.g. some ID number
$headers = array(
"Content-type: text/xml;charset=\"utf-8\"",
"Accept: text/xml",
"Cache-Control: no-cache",
"Pragma: no-cache",
"SOAPAction: http://tempuri.org/MFUAPI",
"Content-length: ".strlen($xml_post_string),
); //SOAPAction: your op URL
$url = $soapUrl;
// PHP cURL for https connection with auth
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//curl_setopt($ch, CURLOPT_USERPWD, $soapUser.":".$soapPassword); // username and password - declared at the top of the doc
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
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);
// converting
$response = curl_exec($ch);
curl_close($ch);
// converting
$response1 = str_replace("<soap:Body>","",$response);
$response2 = str_replace("</soap:Body>","",$response1);
// convertingc to XML
$parser = simplexml_load_string($response2);
// user $parser to get your data out of XML response and to display it.
print_r($parser);
first
I need to execute 2 requests in parallel
then
The problem is that I need to get the encrypted password from the
first XML's output and pass it to the second XML to get 100 success
response from the API.
if the request body of the 2nd request depends on the response of the first request, then you cannot do these 2 requests in parallel.
(generally speaking. you might be able to do some micro-optimization where you send the first request, and partially send the 2nd request, then wait for the password of the first request to be retrieved, then finish the 2nd request, but it'd be difficult to pull off, the gains would probably be very small and if you time it wrong you'll probably get a timeout for the 2nd request and have to start the 2nd request all over again, which would be even slower than just doing the 2 requests sequentially in the first place. you'll also need reliable recovery code for 2nd request timeout'ed while waiting for the password of the 1st request, it would be easy to code it wrong.)

Soap Client in PHP

Hi Can anyone explain me in implementing below SOAP XML in PHP, I saw some questions they were handled using CURL but I want to use SoapClient library in PHP Can anyone help me.
I saw some people used below code in PHP get the simple SOAP , How can I implement the same way in my code
<?php
//Create the client object
$soapclient = new SoapClient('http://www.example.com:8080/test/services/test?wsdl');
//Use the functions of the client, the params of the function are in
//the associative array
$params = array(
'locationID' => '19087525238255',
'custFirstName' => 'Seure',
'custLastName' => 'Install',
'customerType' => 'RESI'
);
$response = $soapclient->octService($params);
var_dump($response);
?>
SOAP XML
<soapenv:Envelope xmlns:soapenv = "http://schemas.xmlsoap.org/soap/envelope/" xmlns:com = "http://test.com/">
<soapenv:Header/>
<soapenv:Body>
<com:OCTService>
<locationID>19087525238255</locationID>
<customer>
<custFirstName>JOHN</custFirstName>
<custLastName>ADAM</custLastName>
<customerType>RESI</customerType>
</customer>
<order>
<orderScheduleType>NoSchedule</orderScheduleType>
<orderScheduledate/>
<reasonCode>NT</reasonCode>
<salesRep>0001</salesRep>
</order>
<Equipments>
<equipment>
<serialNumber>*</serialNumber>
<type>N</type>
</equipment>
<equipment>
<serialNumber>*</serialNumber>
<type>NH</type>
</equipment>
<equipment>
<serialNumber>*</serialNumber>
<type>NH</type>
</equipment>
</Equipments>
<csgServiceCodes>
<CSGServiceCode>
<rateCode>SR002</rateCode>
<packageCode/>
</CSGServiceCode>
<CSGServiceCode>
<rateCode>BA</rateCode>
<packageCode/>
</CSGServiceCode>
</csgServiceCodes>
<voiceFeatures>
<nativeNumbersCount>0</nativeNumbersCount>
<portedNmbers>?</portedNmbers>
</voiceFeatures>
<HuntGroup>
<huntGroupType>?</huntGroupType>
</HuntGroup>
</com:OCTService>
</soapenv:Body>
</soapenv:Envelope>
I not able to achieve using CURL POST.
Here important thing is SOAPAction which you can use in the WSDL document
$headers = array(
"Accept-Encoding: gzip,deflate",
"Content-Type: text",
"Cache-Control: no-cache",
"Username: yourusername",
"Password: password",
"SOAPAction: urn:OCTService",
"Content-length: ".strlen($xml_post_string),
); //SOAPAction: your op URL
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//curl_setopt($ch, CURLOPT_USERPWD, $soapUser.":".$soapPassword); // username and password - declared at the top of the doc
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
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);
// converting
$response = curl_exec($ch);
echo $response;
curl_close($ch);
// converting
$response1 = str_replace("<soap:Body>","",$response);
$response2 = str_replace("</soap:Body>","",$response1);
// convertingc to XML
$parser = simplexml_load_string($response2);

How can I send a SOAP message using PHP?

I want to send a SOAP message using SOAP client. I have a WSDL file for an example. I can use the WSDL file using SOAP UI.
But my requirement is: whenever I am sending a SOAP message to a particular device or somewhere else a message ID should be generated to me for each and every message.
How can I write that SOAP Request using PHP.
Any example/links much appreciated as I am very new to this.
Something like this?
<?php
//Data, connection, auth
$dataFromTheForm = $_POST['fieldName']; // request data from the form
$soapUrl = "https://connecting.website.com/soap.asmx?op=DoSomething"; // asmx URL of WSDL
$soapUser = "username"; // username
$soapPassword = "password"; // password
// xml post structure
$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>
<GetItemPrice xmlns="http://connecting.website.com/WSDL_Service"> // xmlns value to be set to your's WSDL URL
<PRICE>'.$dataFromTheForm.'</PRICE>
</GetItemPrice >
</soap:Body>
</soap:Envelope>'; // data from the form, e.g. some ID number
$headers = array(
"Content-type: text/xml;charset=\"utf-8\"",
"Accept: text/xml",
"Cache-Control: no-cache",
"Pragma: no-cache",
"SOAPAction: http://connecting.website.com/WSDL_Service/GetPrice",
"Content-length: ".strlen($xml_post_string),
); //SOAPAction: your op URL
$url = $soapUrl;
// PHP cURL for https connection with auth
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, $soapUser.":".$soapPassword); // username and password - declared at the top of the doc
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
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);
// converting
$response = curl_exec($ch);
curl_close($ch);
// converting
$response1 = str_replace("<soap:Body>","",$response);
$response2 = str_replace("</soap:Body>","",$response1);
// convertingc to XML
$parser = simplexml_load_string($response2);
// user $parser to get your data out of XML response and to display it.
?>

SOAP request in PHP with CURL

Since the SOAP manual on php.net is not very noob friendly and I could not find any good examples I will post my question here.
How can I create PHP SOAP request to look like this?
POST /MySERVER/myWSDLservice.asmx HTTP/1.1
Host: connection.mywebsite.com
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://connection.mywebsite.com/MySERVER/GetCarType"
<?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>
<GetCarType xmlns="http://connection.mywebsite.com/MySERVER/">
<IDNumber>string</IDNumber>
</GetCarType>
</soap:Body>
</soap:Envelope>
Please note:
there is user/pass auth
SSL connection
Any suggestion / links / example much appreciated.
Tested and working!
with https, user & password
<?php
//Data, connection, auth
$dataFromTheForm = $_POST['fieldName']; // request data from the form
$soapUrl = "https://connecting.website.com/soap.asmx?op=DoSomething"; // asmx URL of WSDL
$soapUser = "username"; // username
$soapPassword = "password"; // password
// xml post structure
$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>
<GetItemPrice xmlns="http://connecting.website.com/WSDL_Service"> // xmlns value to be set to your WSDL URL
<PRICE>'.$dataFromTheForm.'</PRICE>
</GetItemPrice >
</soap:Body>
</soap:Envelope>'; // data from the form, e.g. some ID number
$headers = array(
"Content-type: text/xml;charset=\"utf-8\"",
"Accept: text/xml",
"Cache-Control: no-cache",
"Pragma: no-cache",
"SOAPAction: http://connecting.website.com/WSDL_Service/GetPrice",
"Content-length: ".strlen($xml_post_string),
); //SOAPAction: your op URL
$url = $soapUrl;
// PHP cURL for https connection with auth
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, $soapUser.":".$soapPassword); // username and password - declared at the top of the doc
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
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);
// converting
$response = curl_exec($ch);
curl_close($ch);
// converting
$response1 = str_replace("<soap:Body>","",$response);
$response2 = str_replace("</soap:Body>","",$response1);
// convertingc to XML
$parser = simplexml_load_string($response2);
// user $parser to get your data out of XML response and to display it.
?>

Categories