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.
?>
Related
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.)
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);
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);
Am using PHP curl to do SOAP requests to a webservice, I have an array of IDs of over 500 items, for each ID, I send a SOAP request and get a XML response for which I test some parameters and generate/store in a variable. Unfortunately, I can only loop through 9 IDs of the array, over which I get a "500 Server error" in firebug on a jquery post.
How do I loop through ALL these 500 items? Could it be done with parallel connections OR multi-threading? If so how do you it in PHP curl?
Here is my sample code.
<?php
$VehicleIDs = "153,106,128,149,121,123,125,133,130,115,124,116,102,100,101,103,144,113,...........";//over 500 items
$VehicleIDsArray = explode(",", $VehicleIDs);
for($i=0; $i <= count($massVehicleIDsArray); $i++){
$soapUrl = "http://xxx.asmx";
$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>
<GetInUseEventsForVehicleID>
<VehicleID>'.$massVehicleIDsArray[$i].'</VehicleID>
</GetInUseEventsForVehicleID>
</soap:Body>
</soap:Envelope>';
$headers = array(
"Content-type: text/xml;charset=\"utf-8\"",
"Accept: text/xml",
"Cache-Control: no-cache",
"Pragma: no-cache",
"SOAPAction: xxx", // your op URL
"Content-length: ".strlen($xml_post_string),
);
// PHP cURL for https connection with auth
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_URL, $soapUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string); // xml 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);
/*....................
MORE CODE TO COMPARE EACH SOAP RESPONSE
...................*/
}
?>
Thank you
I thinks base problem is in low time limit http://www.php.net/manual/en/info.configuration.php#ini.max-execution-time, and yes curl supports parallel execution http://php.net/manual/ru/function.curl-multi-exec.php , there are some open-source libs for parallel curl executing.
http://www.phpclasses.org/package/4091-PHP-Retrieve-multiple-pages-simultaneously.html
https://github.com/Fivell/Utfy
https://github.com/jmathai/php-multi-curl
and so on.
But you still might hit the maximum execution time or the memory limit though
Another posibility is that you call the wsdl method in a php many times in same page
create before a client connection and later do something similar
$params1 = array(xml nodes);
$client->WSDLMETHOD($params1);
echo $client->__getLastRequest();
echo $result = $client->__getLastResponse();
//reapeat this and you will do two soap request
$params2 = array(xml nodes);
$client->WSDLMETHOD($params2);
echo $client->__getLastRequest();
echo $result = $client->__getLastResponse();
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.
?>