Execute multiple parallel cURL against the same URL? - php

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.)

Related

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 XML call using PHP curl?

I have one SOAP WSDL api and i have a test api access.
My SOAP Url : https://development.avinode.com/avinode/AvinodeIntegrationWeb/ws/EmptyLegFlightDemand.ws?wsdl
i need to call and get the details from this url. i must send my username and password into this api call. so i tried to build the code. the code is given below.
<?php
$soapUrl='https://development.avinode.com:443/avinode/AvinodeIntegrationWeb/ws/EmptyLegDownload.ws?wsdl';
$xml_post_string='<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<S:Header>
<To xmlns="http://www.w3.org/2005/08/addressing">https://development.avinode.com/avinode/AvinodeIntegrationWeb/ws/EmptyLegDownload.ws</To>
<Action xmlns="http://www.w3.org/2005/08/addressing">http://www.avinode.com/integration/EmptyLegDownload#request</Action>
<ReplyTo xmlns="http://www.w3.org/2005/08/addressing">
<Address>http://www.w3.org/2005/08/addressing/anonymous</Address>
</ReplyTo>
<FaultTo xmlns="http://www.w3.org/2005/08/addressing">
<Address>http://www.w3.org/2005/08/addressing/anonymous</Address>
</FaultTo>
<MessageID xmlns="http://www.w3.org/2005/08/addressing">uuid:6a456287-923e-458e-9ccb-f900307f2b0f</MessageID>
<wsse:Security S:mustUnderstand="1">
<wsu:Timestamp xmlns:ns13="http://www.w3.org/2003/05/soap-envelope" xmlns:ns14="http://docs.oasis-open.org/ws-sx/ws-secureconversation/200512" wsu:Id="_1">
<wsu:Created>2014-10-17T15:45:42Z</wsu:Created>
<wsu:Expires>2014-10-17T15:50:42Z</wsu:Expires>
</wsu:Timestamp>
<wsse:UsernameToken xmlns:ns13="http://www.w3.org/2003/05/soap-envelope" xmlns:ns14="http://docs.oasis-open.org/ws-sx/ws-secureconversation/200512" wsu:Id="uuid_2da8da35-1c69-4f38-9899-ba6950c825f5">
<wsse:Username>MY_USERNAME</wsse:Username>
<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">PASSWORD</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>
</S:Header>
<S:Body>
<ns3:request xmlns="http://www.avinode.com/core/CommonTypes" xmlns:ns2="http://www.avinode.com/services/EmptyLegDownload" xmlns:ns3="http://www.avinode.com/integration/EmptyLegDownload">
<ns2:product>
<name>osiz</name>
<version>1.0</version>
</ns2:product>
<ns2:domain>http://flightcomparision.osiztechnologies.com</ns2:domain>
<ns2:locale>en_US</ns2:locale>
<ns2:currency>USD</ns2:currency>
<ns2:region>AMERICA</ns2:region>
<ns2:after>2014-11-01T00:00:00Z</ns2:after>
<ns2:before>2014-12-01T00:00:00Z</ns2:before>
<ns2:pax>1</ns2:pax>
<ns2:excludeBrokers>false</ns2:excludeBrokers>
<ns2:requireTailNumber>false</ns2:requireTailNumber>
</ns3:request>
</S:Body>
</S:Envelope>';
$headers = array(
"Content-Type: text/xml;charset=UTF-8",
"Accept: gzip,deflate",
"Cache-Control: no-cache",
"Pragma: no-cache",
"SOAPAction: \"\"",
"Authorization: Basic $auth",
"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, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_TIMEOUT, 500);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 500);
curl_setopt($ch, CURLOPT_MAXREDIRS, 12);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string); // the SOAP request
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$ss = curl_getinfo($ch);
//print_r($ss);
// exit;
$response = curl_exec($ch);
print_r($response);
exit;
curl_close($ch);
?>
I got only Empty response only please give me any idea highly appreciated.
I would start by adding:
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
That will allow you to see the response http code of the server.
Usually when I work on SOAP / REST services I make sure to have Fiddler running in the background, that makes debugging a lot easier.
You can find how to use Fiddler with cURL here: Configure PHP cURL

How to call soap api using 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);

How do I send multiple SOAP Requests in a loop: PHP?

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();

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