I am trying call SOAP API request using CURL and here is my code:
$wsdl = "http://xxxxx//xxxxx//xxxxx";
$body = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:par="http:///xxxxx.com">
<soapenv:Header/>
<soapenv:Body>
<par:createReservationReq>
<par:garageID>47</par:garageID>
<par:orderNumber>orderNumber'.$id.'</par:orderNumber>
<!--1 or more repetitions:-->
<par:vehicles>
<par:barCode>barCode'.$id.'</par:barCode>
<!--Optional:-->
<par:licensePlate>licensePlate'.$id.'</par:licensePlate>
<par:startTime>2015-11-20T22:53:45.547</par:startTime>
<par:endTime>2015-11-21T22:53:45.547</par:endTime>
<!--Optional:-->
<par:vehicleMake>vehicleMake'.$id.'</par:vehicleMake>
<!--Optional:-->
<par:vehicleModel>vehicleMode'.$id.'</par:vehicleModel>
<!--Optional:-->
<par:vehicleColor>vehicleColor'.$id.'</par:vehicleColor>
</par:vehicles>
<!--Optional:-->
<par:grossPrice>152.6</par:grossPrice>
<!--Optional:-->
<par:commFee>162.6</par:commFee>
<!--Optional:-->
<par:customerName>customerName'.$id.'</par:customerName>
<!--Optional:-->
<par:customerEmail>customerEmail'.$id.'</par:customerEmail>
<!--Optional:-->
<par:customerPhone>customerPhone'.$id.'</par:customerPhone>
<!--Optional:-->
<par:reentryAllowed>true</par:reentryAllowed>
</par:createReservationReq>
</soapenv:Body>
</soapenv:Envelope>';
// initializing cURL with the IPG API URL:
$ch = curl_init($wsdl);
// setting the request type to POST:
curl_setopt($ch, CURLOPT_POST, 1);
// setting the content type:
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: text/xml"));
// setting the authorization method to BASIC:
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
// filling the request body with your SOAP message:
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
// telling cURL to verify the server certificate:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
// telling cURL to return the HTTP response body as operation result
// value when calling curl_exec:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// calling cURL and saving the SOAP response message in a variable which
// contains a string like "<SOAP-ENV:Envelope ...>...</SOAP-ENV:Envelope>":
$result = curl_exec($ch);
// loads the XML
$xml = simplexml_load_string($result);
Now I don't understand how to read value from xml response. I am adding my response data here so you can get proper idea:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:Body xmlns:s="http://www.w3.org/2003/05/soap-envelope">
<tns:createReservationResp xmlns:tns="http://xxxxx.com">
<tns:success>true</tns:success>
<tns:message>Test Garage order orderNumber168. created 2015-11-19 00:30:57.905</tns:message>
<tns:orderTime>2015-11-19 00:30:57.905</tns:orderTime>
<tns:reservations>
<tns:garageID>47</tns:garageID>
<tns:barCode>barCode168</tns:barCode>
<tns:licensePlate>licensePlate168</tns:licensePlate>
<tns:orderNumber>orderNumber168</tns:orderNumber>
<tns:used>false</tns:used>
<tns:cancelled>false</tns:cancelled>
<tns:startTime>2015-11-20 22:53:45.547</tns:startTime>
<tns:endTime>2015-11-21 22:53:45.547</tns:endTime>
<tns:cancelledTime/>
<tns:checkInTime/>
<tns:checkOutTime/>
<tns:grossPrice>152.6</tns:grossPrice>
<tns:commFee>162.6</tns:commFee>
<tns:customerName>customerName168</tns:customerName>
<tns:customerEmail>customerEmail168</tns:customerEmail>
<tns:customerPhone>customerPhone168</tns:customerPhone>
<tns:vehicleMake>vehicleMake168</tns:vehicleMake>
<tns:vehicleModel>vehicleModel68</tns:vehicleModel>
<tns:vehicleColor>vehicleColor168</tns:vehicleColor>
<tns:reentryAllowed>true</tns:reentryAllowed>
</tns:reservations>
</tns:createReservationResp>
</soapenv:Body>
</soapenv:Envelope>
I want to read this value from response:
<tns:success>true</tns:success>
<tns:message>Test Garage order orderNumber168. created 2015-11-19 00:30:57.905</tns:message>
<tns:orderTime>2015-11-19 00:30:57.905</tns:orderTime>
<tns:garageID>47</tns:garageID>
<tns:barCode>barCode168</tns:barCode>
Any Idea?
Thanks.
In general you should use SoapClient when dealing with SOAP instead of curl. It provides much cleaner interface. In your case one needs to work with SimpleXML functions, a good tutorial is on the SitePoint.
$soapenv = $xml->children("http://schemas.xmlsoap.org/soap/envelope/");
$tns = $soapenv->Body->children("http://xxxxx.com");
echo "success: " . $tns->createReservationResp->success . "\n";
echo "message: " . $tns->createReservationResp->message . "\n";
echo "orderTime: " . $tns->createReservationResp->orderTime . "\n";
echo "garageID: " . $tns->createReservationResp->reservations->garageID . "\n";
echo "barCode: " . $tns->createReservationResp->reservations->barCode . "\n";
Related
I was trying to hit XML request from PHP SOAP Client according to the following provided SOAP API documentation.
WSDL Schema
Refer to the following URL.
http://[SERVER_IP]/PowerSuite/PSXMLSearch.asmx?wsdl
4 Input/Output
4.1 Search Supplier Code and Name (PSXMLSearchSupplier)
Input : PSXML_SEARCH_SUPP
Output : PSXML_SEARCH_SUPP_Response
6 Field definition
6.1 PSXML_SEARCH_SUPP - The main entry point and the authentication information.
MSGID
USERID
PASSWORD
OPTION
SUPPNO
But when i run with SOAP client it gives me a following error
Object reference not set to an instance of an object
So after long research on internet, i decided to send XML request via CURL, i have created following Schema for XML request and send it via CURL request
$soapUrl = "https://[MY_SERVER_DOMAIN]/PowerSuite/PSXMLSearch.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>
<PSXML_SEARCH_SUPP xmlns="https://[MY_SERVER_DOMAIN]/PowerSuite/PSXMLSearch.asmx">
<MSGID>TRAVEK_E1</MSGID>
<USERID>GCOT</USERID>
<PASSWORD>CKHOSKIS6</PASSWORD>
<OPTION>LIKE</OPTION>
<SUPPNO>RK0048</SUPPNO>
</PSXML_SEARCH_SUPP>
</soap:Body>
</soap:Envelope>';
$headers = array(
"Content-type: text/xml;charset=\"utf-8\"",
"Accept: text/xml",
"Cache-Control: no-cache",
"Pragma: no-cache",
"SOAPAction: \"https://[MY_SERVER_DOMAIN]/PowerSuite\"",
"Content-length: ".strlen($xml_post_string),
);
$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, "GCONNECT:Connect#786");
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.
echo "<pre>";
print_r($parser);
echo "</pre>";
die();
But even after that i only get empty XML response as can see below
SimpleXMLElement Object
(
)
Please guide am i sending a correct XML schema, or their needs to be some thing amend into it?
I checked with RapidAPI, it shows the correct response, i have shown the request in the image below:
In FORM
https://gcdnb.pbrd.co/images/a9NdABt6rXQl.png
In XML
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xh="http://www.xmlhk.com/">
<soapenv:Header/>
<soapenv:Body>
<xh:PSXMLSearchSupplier>
<!--Optional:-->
<xh:PSXML_SEARCH_SUPP>
<!--Optional:-->
<xh:MSGID>TRAVEK_EBOOK001</xh:MSGID>
<!--Optional:-->
<xh:USERID>ABCUSER</xh:USERID>
<!--Optional:-->
<xh:PASSWORD>Connect#786</xh:PASSWORD>
<!--Optional:-->
<xh:OPTION>LIKE</xh:OPTION>
<!--Optional:-->
<xh:SUPPNO>ABDECS</xh:SUPPNO>
<!--Optional:-->
<xh:SUPPNAME>?</xh:SUPPNAME>
</xh:PSXML_SEARCH_SUPP>
</xh:PSXMLSearchSupplier>
</soapenv:Body>
</soapenv:Envelope>
I tried the same above with CURL request and got that working, i found that,following to be changed in my previous CURL request
, which on the raw tab in ReadyAPI
"SOAPAction: \"http://www.xmlhk.com/PSXMLSearchSupplier\"",
To work this with SOAP Client i just need to used multi dimensional array with $params['PSXML_SEARCH_SUPP'] shows below
$wsdl = "https://ps.gerrys.com.pk/PowerSuite/PSXMLSearch.asmx?WSDL";
$params['PSXML_SEARCH_SUPP'] = array(
'MSGID' => "TRAVEK_EBOOK001",
'USERID' => "GCONNECT",
'PASSWORD' => "Connect#786",
'OPTION' => 'LIKE',
'SUPPNO' => 'RK0048'
);
try {
$soap = new SoapClient($wsdl);
$data = $soap->PSXMLSearchSupplier($params);
echo "<pre>";
print_r($data);
echo "</pre>";
die();
}
catch(Exception $e) {
$e->getMessage();
var_dump($e->getMessage());
die();
}
I need to post to a client's URL (in case it matters the client is on a .Net platform) using SOAP and XML. The request needs to look something like this:
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:tem="http://tempuri.org/" xmlns:XXX="http://schemas.datacontract.org/2004/07/XXXAPI.Entities.XXX" xmlns:XXX1="http://schemas.datacontract.org/2004/07/XXXAPI.Entities.Admin"> <soap:Header/> <soap:Body>
<tem:SaveXXXStatus>
<!--Optional:-->
<tem:req>
<!--Optional:-->
<XXX:AWBNumber>69184678146</XXX:AWBNumber>
<!--Optional:-->
… etc. …
<!--Optional:-->
<XXX:pincode></XXX:pincode>
</tem:req>
<!--Optional:-->
<tem:profile>
<!--Optional:-->
<XXX1:Api_type>S</XXX1:Api_type>
<!--Optional:-->
<XXX1:Area></XXX1:Area>
<!--Optional:-->
<XXX1:LicenceKey>xxxxxxxxxxxxxxxxxxx</XXX1:LicenceKey>
<!--Optional:-->
<XXX1:LoginID>XXXYYY</XXX1:LoginID>
<!--Optional:-->
<XXX1:Version>1</XXX1:Version>
</tem:profile>
</tem:SaveXXXStatus> </soap:Body> </soap:Envelope>
I am using the following code:
$ch = curl_init();
//var_dump($ch);
curl_setopt($ch, CURLOPT_URL,"https://example.com?wsdl");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'tem:"http://tempuri.org/"',
'Content-Type: text/xml',
'XXX:"http://schemas.datacontract.org/2004/07/XXXAPI.Entities.XXX"'
'XXX1:"http://schemas.datacontract.org/2004/07/XXXAPI.Entities.Admin"'
));
//curl_setopt($ch, CURLOPT_USERPWD, "XXXYYY:xxxxxxxxxxxxx"); //Probably not needed
curl_setopt($ch, CURLOPT_POST, 1);
$strRequest = "";
$strRequest .= "AWBNumber=69184678161";
… etc….
$strRequest .= "&pincode=";
$strRequest .= "&Api_type=S";
$strRequest .= "&Area=";
$strRequest .= "&LicenceKey=xxxxxxxx";
$strRequest .= "&LoginID=XXXYYY";
$strRequest .= "&Version=1";
curl_setopt($ch, CURLOPT_POSTFIELDS,$strRequest);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec($ch);
var_dump($server_output);
This post shows that the header (envelope etc.) can simply be added to the POSTFIELDS string but I tried that and it didn't work. Besides it seems like such a hack!
Anyway, no combination is working - I am getting a zero length string as a result ($server_output). What is the right way to pass the headers and what else needs to be fixed here?
i also stuck in this few days ago... but tried this way and got results
$xml_post_string ='<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:tem="http://tempuri.org/" xmlns:XXX="http://schemas.datacontract.org/2004/07/XXXAPI.Entities.XXX" xmlns:XXX1="http://schemas.datacontract.org/2004/07/XXXAPI.Entities.Admin"> <soap:Header/> <soap:Body>
<tem:SaveXXXStatus>
<!--Optional:-->
<tem:req>
<!--Optional:-->
<XXX:AWBNumber>69184678146</XXX:AWBNumber>
<!--Optional:-->
… etc. …
<!--Optional:-->
<XXX:pincode></XXX:pincode>
</tem:req>
<!--Optional:-->
<tem:profile>
<!--Optional:-->
<XXX1:Api_type>S</XXX1:Api_type>
<!--Optional:-->
<XXX1:Area></XXX1:Area>
<!--Optional:-->
<XXX1:LicenceKey>xxxxxxxxxxxxxxxxxxx</XXX1:LicenceKey>
<!--Optional:-->
<XXX1:LoginID>XXXYYY</XXX1:LoginID>
<!--Optional:-->
<XXX1:Version>1</XXX1:Version>
</tem:profile>
</tem:SaveXXXStatus> </soap:Body> </soap:Envelope>';
$headers = array(
"Content-type: text/xml;charset=\"utf-8\"",
"Accept: text/xml",
"Cache-Control: no-cache",
"Pragma: no-cache",
"SOAPAction: url",
"Content-length: " . strlen($xml_post_string),
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_URL, 'yoururl');
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);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
print_r($response);
As I mentioned in the comments already, it would be much easier using the build in PHP SoapClient class and objects as entities. Here 's a little example, how you could solve your issue.
The PHP Soap Client
PHP got its own build in SoapClient which works pretty fine. Have a look how to initialize the client for development.
try {
$client = new \SoapClient('https://example.com?wsdl', [
'cache_wsdl' )> WSDL_CACHE_NONE,
'exceptions' => true,
'trace' => true,
]);
} catch (\SoapFault $fault) {
echo "<pre>";
var_dump($fault->getMessage());
echo "</pre>";
if ($client) {
echo "<pre>";
var_dump($client->__getLastRequest(), $client->__getLastResponse());
echo "</pre>";
}
}
This is a simple init of the SoapClient class with development options. Setting the trace option to true enables the use of the clients internal functions __getLastRequest() and __getLastResponse(). So you can see, what the client has sent and what the response looks like, if there 's any. I 'm using this for checking the xml that the client has sent.
Simple entities as objects that can be used by soap
SOAP defines itself as complex and simple type definitions. You can see this, if you call the clients own __getTypes() function. There will be displayed a lot of structs and simple type definitions, which are stored in the given wsdl file or in xsd files mentioned in the wsdl file. With this informations we are able to build our own object. In this example I 'm using simple stdClass objects. In production manner, you should use computed own objects.
$req = new \stdClass();
$req->AWBNumber = new \SoapVar(
69184678146,
XSD_INT,
null,
null,
'AWBNumber',
'http://schemas.datacontract.org/2004/07/XXXAPI.Entities.XXX'
);
$encodedReq = new \SoapVar($req, SOAP_ENC_OBJECT, null, null, 'req', 'http://tempuri.org/');
$saveXXXStatus = new \stdClass();
$saveXXXStatus->req = $encodedReq;
$encodedSaveXXXStatus = new \SoapVar($saveXXXStatus, SOAP_ENC_OBJECT, null, null, 'SaveXXXStatus, 'http://tempuri.org/');
// send the content with the soap client
$result = $client->SaveXXXStatus($encodedSaveXXXStatus);
Please keep in mind, that this is a short example which is incomplete and will lead to a soap fault. But what I 've done here? The req node in your xml is an object. You 'll find the definition of this object in the above metioned __getTypes() function output. In this example I 've compiled this object as a stdClass with the property AWBNumber. The AWBNumber itself is a SoapVar object. We use a soap var because of the namespaces, which are used by the soap client. After defining a property we encode the req object as a soap object, which is also a SoapVar instance.
After all we call the webservice method SaveXXXStatus with the encoded parameter.
The Last Request
If you send this example, the last request should look like:
<ns1:envelope xmlns:ns1="http://www.w3.org/2003/05/soap-envelope"
xmlns:ns2="http://tempuri.org/"
xmlns:ns3="http://schemas.datacontract.org/2004/07/XXXAPI.Entities.XXX">
<ns1:body>
<ns2:SaveXXXStatus>
<ns2:req>
<ns3:AWBNumber>69184678146</ns3:AWBNumber>
</ns2:req>
</ns2:SaveXXXStatus>
</ns1:body>
</ns1:envelope>
As I said before, this is just an example. You have to code all the nodes as SoapVar objects and append it to parents and finally call the webservice method with the complete encoded data.
Simple as pie, hm?
SoapUI newbie here. I am invoking this secure Soap API with SoapUI 5.3 and this is how the security header was configured in SoapUI:
Right click project and select show project view
Select WS-Security Configurations tab
Select Outgoing WS-Security Configuration tab and create a new config
WSS Entries created:Username(checked adds a nonce and adds a created boxes and selected passwordType as PasswordText), Timestamp(TTL 30, checked the Sets precision of timestamp to milliseconds box)
When sending my SOAP request, Auth type is Basic and selected the above configuration.
The SOAP request header now looks like this:
<soapenv:Header>
<wsse:Security
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">
<wsu:Timestamp
wsu:Id="TS-68AE03334F6A9DBDDC150599839319940">
<wsu:Created>
2017-09-21T12:53:13.199Z
</wsu:Created>
<wsu:Expires>
2017-09-21T12:53:43.199Z
</wsu:Expires>
</wsu:Timestamp>
<wsse:UsernameToken
wsu:Id="UsernameToken-68AE03334F6A9DBDDC150599839319939">
<wsse:Username>
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:Nonce
EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">
8NBStc5kqrgPWG9pj09Svw==
</wsse:Nonce>
<wsu:Created>
2017-09-21T12:53:13.199Z
</wsu:Created>
</wsse:UsernameToken>
</wsse:Security>
</soapenv:Header>
And everything works very fine in SoapUI. But ultimately, I am going to write a Java or PHP client to consume this API. There are things I want to understand:
<wsu:Timestamp wsu:Id="TS-68AE03334F6A9DBDDC150599839319940">
The wsu:Id attribute for the Timestamp and Username tokens, is there any criteria or I can generate a random value for each in each request?
The nonce: Same story as 1 above. However, the encodingType is Base64Binary. My understanding is that nonce is a random value, unique for each request to fight off replay attacks. Putting the encodingType in mind, how do I consistently create a good one from my php/Java client? When I base64decode the value generated by SoapUI, it still comes back as binary, I expected a String, what is SoapUI doing here??
The response in SoapUI is XML, but when I send the request from PHP, I receive either a binary response or just this:
bool(false)
So how does SoapUI convert the response to plain XML and how can I do the same in my code??
Here is my PHP test script which has worked for me like a charm with other Soap APIs:
<?php
$soap = '<soapenv:Envelope
xmlns:ns="http://soap.crmapi.util.redknee.com/common/xsd/2011/05"
xmlns:ns1="http://soap.crmapi.util.redknee.com/common/xsd/2008/08"
xmlns:ns2="http://soap.crmapi.util.redknee.com/subscriptions/xsd/2011/01"
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:sub="http://soap.crmapi.util.redknee.com/subscriptions/xsd/Subscriptions-v2.0">
<soapenv:Header>
<wsse:Security
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">
<wsu:Timestamp
wsu:Id="TS-68AE03334F6A9DBDDC150599839319940">
<wsu:Created>
2017-09-21T12:53:13.199Z
</wsu:Created>
<wsu:Expires>
2017-09-21T12:53:43.199Z
</wsu:Expires>
</wsu:Timestamp>
<wsse:UsernameToken
wsu:Id="UsernameToken-68AE03334F6A9DBDDC150599839319939">
<wsse:Username>
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:Nonce
EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">
8NBStc5kqrgPWG9pj09Svw==
</wsse:Nonce>
<wsu:Created>
2017-09-21T12:53:13.199Z
</wsu:Created>
</wsse:UsernameToken>
</wsse:Security>
</soapenv:Header>
<soapenv:Body>
<sub:methodName>
<sub:header>
</sub:header>
<sub:subscriptionRef>
<ns2:mobileNumber>
256723009772
</ns2:mobileNumber>
<ns2:spid>
10
</ns2:spid>
</sub:subscriptionRef>
<!--Zero or more repetitions:-->
</sub:methodName>
</soapenv:Body>
</soapenv:Envelope>';
$header = array(
'Connection: Keep-Alive',
'Content-Length: ' . strlen($soap),
'SOAPAction:urn:methodName',
'Host:IP:Port'
);
$url = 'https://ip:port/services/SubscriptionService/';
$ch = curl_init($url); // LIVE
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $soap);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 5000);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$result = curl_exec($ch);
echo "\nResult\n$result\n";
var_dump($result);
I want to configure an api of soap with php, following the xml is used for the api, but its not responding, please help me to recover this problem
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:fram="http://framework.zend.com">
<soapenv:Header/>
<soapenv:Body>
<fram:getCountries>
<securityInfo>
<userName>username</userName>
<password>password</password>
<agentCode>code</agentCode>
<lang>en</lang>
</securityInfo>
</fram:getCountries>
</soapenv:Body>
</soapenv:Envelope>
The below showing the php code I used to call xml. But I didnt get any response to my php code, but the xml is correct, I have asked to the provider they told me the xml is correct but my php code is not correct. I dont what I do to solve this problem :(
<?php
$soapUrl = "http://testapi.roombookpro.com/en/soap/index?wsdl"; // asmx URL of WSDL
$soapUser = "username"; // username
$soapPassword = "password"; // password
// xml post structure
$xml_post_string = '<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:fram="http://framework.zend.com">
<soapenv:Header/>
<soapenv:Body>
<fram:getCountries>
<securityInfo>
<!-- You may enter the following 4 items in any order -->
<userName>username</userName>
<password>password</password>
<agentCode>agentcode</agentCode>
<lang>en</lang>
</securityInfo>
</fram:getCountries>
</soapenv:Body>
</soapenv: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://testapi.roombookpro.com/en/soap/index#getCountries",
"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);
var_dump($parser);
echo $parser;
// user $parser to get your data out of XML response and to display it.
?>
In your code,your request is pointing to wsdl link below,
http://testapi.roombookpro.com/en/soap/index?wsdl
and thats why you are not getting soap response rather you getting the definitions.
you should use the following soap url to get the soap response,
http://testapi.roombookpro.com/en/soap/index
EDIT:
and to get the XML response (as string):
$response = curl_exec($ch);
curl_close($ch);
print_r($response);
to get as SimpleXMLElement Object :
$xml = simplexml_load_string($response);
foreach ($xml->xpath('//ns2:Soap_Model_SOAP_Location_Country') as $item)
{
print_r($item);
//to access individual elements
// echo $item->id ;
// echo $item ->code;
// echo $item->name;
// echo "\n";
}
which gives array of XML objects:
SimpleXMLElement Object
(
[id] => 252
[code] => ZW
[name] => Zimbabwe
)
I am calling this soap API with curl and not able read response value properly so can you please guide me what I am doing wrong.
Here is my code:
$wsdl = "http://xxxx/xxxx/xxxx";
$body = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:par="http://xxxx.com">
<soapenv:Header/>
<soapenv:Body>
<par:cancelReservationReq>
<par:garageID>47</par:garageID>
<par:orderNumber>orderNumber168</par:orderNumber>
<!--Optional:-->
<par:barCode>barCode168</par:barCode>
<!--Optional:-->
<par:orderTime>2015-11-19T00:30:57.905</par:orderTime>
</par:cancelReservationReq>
</soapenv:Body>
</soapenv:Envelope>';
// initializing cURL with the IPG API URL:
$ch = curl_init($wsdl);
// setting the request type to POST:
curl_setopt($ch, CURLOPT_POST, 1);
// setting the content type:
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: text/xml"));
// setting the authorization method to BASIC:
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
// filling the request body with your SOAP message:
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
// telling cURL to verify the server certificate:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
// telling cURL to return the HTTP response body as operation result
// value when calling curl_exec:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// calling cURL and saving the SOAP response message in a variable which
// contains a string like "<SOAP-ENV:Envelope ...>...</SOAP-ENV:Envelope>":
$result = curl_exec($ch);
// loads the XML
$xml = simplexml_load_string($result);
$soapenv = $xml->children("http://schemas.xmlsoap.org/soap/envelope/");
$tns = $soapenv->Body->children("http://xxxx.com");
echo "success ".$success = $tns->cancelReservationResp->success;
echo "message ".$message = $tns->cancelReservationResp->message;
echo "garageID ".$garageID = $tns->cancelReservationResp->reservations->garageID;
API response data:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:Body xmlns:s="http://www.w3.org/2003/05/soap-envelope">
<tns:cancelReservationResp xmlns:tns="http://xxxx.com">
<tns:success>true</tns:success>
<tns:message/>
<tns:reservations>
<tns:garageID>47</tns:garageID>
<tns:barCode>barCode168</tns:barCode>
<tns:licensePlate>licensePlate168</tns:licensePlate>
<tns:orderNumber/>
<tns:used>false</tns:used>
<tns:cancelled>true</tns:cancelled>
<tns:startTime>2015-11-20 22:53:45.547</tns:startTime>
<tns:endTime>2015-11-21 22:53:45.547</tns:endTime>
<tns:cancelledTime>2015-11-19 00:51:58.717</tns:cancelledTime>
<tns:checkInTime/>
<tns:checkOutTime/>
<tns:grossPrice>152.6000</tns:grossPrice>
<tns:commFee>162.6000</tns:commFee>
<tns:customerName>customerName168</tns:customerName>
<tns:customerEmail>customerEmail168</tns:customerEmail>
<tns:customerPhone>customerPhone168</tns:customerPhone>
<tns:vehicleMake>vehicleMake168</tns:vehicleMake>
<tns:vehicleModel>vehicleModel68</tns:vehicleModel>
<tns:vehicleColor>vehicleColor168</tns:vehicleColor>
<tns:reentryAllowed>true</tns:reentryAllowed>
</tns:reservations>
</tns:cancelReservationResp>
</soapenv:Body>
</soapenv:Envelope>
I am getting this <tns:success>true</tns:success> value from $success variable but not getting anything from $garageID variable.
Any idea why $garageID is getting blank?
Thanks.