PHP - creating XML for SOAP Request - php

I have zero experience in using SOAP and have done searching everywhere to solve this problem.
I am trying to create an XML request to send to a SOAP server from my PHP code. It's a site to get a live quotation for car insurance.
WSDL link: https://eins2.zurich.com.my/VIXAPI/VixService.svc?wsdl
I have tested using SOAPUI software and it gave me XML as below for the request:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
<soapenv:Header/>
<soapenv:Body>
<tem:fnGetVehicleDtlsByVIX>
<!--Optional:-->
<tem:VehInputInfo>{"AgentCode":"D02940-000","ParticipantCode":"06","RequestDateTime":"2017-Mar-17 11:00:00 PM","ID":"850321-07-5179","VehNo":"WA823H","Signature":"E448A5DE70160A7C541306B38ABAE3C8826ACD262DF217F9AA8B32244374C5E2E66D26D31874BBD832E43A6A569D20F2DFE8F674AECCFD698850BEBFB13767FD"}</tem:VehInputInfo>
</tem:fnGetVehicleDtlsByVIX>
</soapenv:Body>
</soapenv:Envelope>
The element VehInputInfo is the required input and is a JSON formatted string.
The response was correct (via SOAPUI software, check the screenshot here) and the next step is I'm trying to pass the XML request above in my PHP code.
Below is my PHP code:
<?
// error reporting
error_reporting(E_ALL - E_NOTICE);
ini_set('display_errors','On');
//header('Content-type: text/xml');
$wsdl = 'https://eins2.zurich.com.my/VIXAPI/VixService.svc?wsdl';
$client = new SoapClient($wsdl, array('trace'=> 1));
$xml = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
<soapenv:Header/>
<soapenv:Body>
<tem:fnGetVehicleDtlsByVIX>
<!--Optional:-->
<tem:VehInputInfo>{"AgentCode":"D02940-000","ParticipantCode":"06","RequestDateTime":"2017-Mar-17 11:00:00 PM","ID":"850321-07-5179","VehNo":"WA823H","Signature":"E448A5DE70160A7C541306B38ABAE3C8826ACD262DF217F9AA8B32244374C5E2E66D26D31874BBD832E43A6A569D20F2DFE8F674AECCFD698850BEBFB13767FD"}</tem:VehInputInfo>
</tem:fnGetVehicleDtlsByVIX>
</soapenv:Body>
</soapenv:Envelope>';
//echo $xml;
try
{
$result = $client-> fnGetVehicleDtlsByVIX($xml);
}
catch (Exception $e)
{
var_dump($e->getMessage());
var_dump($client->__getLastRequest());
var_dump($client->__getLastResponse());
}
But all I got was errors
Am not sure if it is the correct way to create the XML or is there any other way to do this?
Anyone can help me?
Thank you in advance.

I solved the problem.
Changed to using cURL as below. It might not be the best answer but it solved my problem.
<?php
// error reporting
error_reporting(E_ALL - E_NOTICE);
ini_set('display_errors','On');
$soapUrl = "https://eins2.zurich.com.my/VIXAPI/VixService.svc?wsdl"; //URL of WSDL
// xml post structure
$xml_post_string = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
<soapenv:Header/>
<soapenv:Body>
<tem:fnGetVehicleDtlsByVIX>
<!--Optional:-->
<tem:VehInputInfo>{"AgentCode":"D02940-000","ParticipantCode":"06","RequestDateTime":"2017-Mar-17 11:00:00 PM","ID":"850321-07-5179","VehNo":"WA823H","Signature":"E448A5DE70160A7C541306B38ABAE3C8826ACD262DF217F9AA8B32244374C5E2E66D26D31874BBD832E43A6A569D20F2DFE8F674AECCFD698850BEBFB13767FD"}</tem:VehInputInfo>
</tem:fnGetVehicleDtlsByVIX>
</soapenv:Body>
</soapenv:Envelope>';
$headers = array(
"POST: https://eins2.zurich.com.my/VIXAPI/VixService.svc HTTP/1.1",
"Content-type: text/xml;charset=\"UTF-8\"",
"Accept-Encoding: gzip,deflate",
"Cache-Control: no-cache",
"Pragma: no-cache",
"SOAPAction: \"http://tempuri.org/IVixService/fnGetVehicleDtlsByVIX\"",
"Content-Length: ".strlen($xml_post_string),
"Host: eins2.zurich.com.my",
"Connection: Keep-Alive"
); //SOAPAction: your op URL
$url = $soapUrl;
//print_r($headers);
// 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);
print_r($response);

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

Access web service via curl PHP or some thing similar

I have this type of API code and how can I send request to access this web service in php? can I use curl ? Please help me
Sandbox URL :
http://api.lankagate.gov.lk:8280/GetOnGoingVehicleNoDMT/1.0
Method POST
Parameter Name - vehicleCategory
Options - 1 , 4
Success Response Code: 200
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<GetOnGoingVehicleNoResponse
xmlns="http://schemas.conversesolutions.com/xsd/dmticta/v1">
<return>
<ResponseMessage xsi:nil="true" />
<ErrorCode xsi:nil="true" />
<OngoingVehicleNo>CAW-2186</OngoingVehicleNo>
</return>
</GetOnGoingVehicleNoResponse>
</soap:Body>
</soap:Envelope>
Sample
Call URL : http://api.lankagate.gov.lk:8280/Transliteration/1.0/
Message body :
<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:v1="http://schemas.conversesolutions.com/xsd/dmticta/v1">
<soapenv:Header/>
<soapenv:Body>
<v1:GetOnGoingVehicleNo>
<v1:vehicleCategory>1</v1:vehicleCategory>
</v1:GetOnGoingVehicleNo>
</soapenv:Body>
</soapenv:Envelope>
Notes Set request header as
Authorization: Bearer Access Token
I have tried and this is my code
function get_exam_mode_of_study() {
$xml_data = '<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:v1="http://schemas.conversesolutions.com/xsd/dmticta/v1">
<soapenv:Header/>
<soapenv:Body>
<v1:GetOnGoingVehicleNo>
<v1:vehicleCategory>1</v1:vehicleCategory>
</v1:GetOnGoingVehicleNo>
</soapenv:Body>
</soapenv:Envelope>';
$URL = "http://api.lankagate.gov.lk:8280/GetOnGoingVehicleNoDMT/1.0";
$ch = curl_init($URL);
// curl_setopt($ch, CURLOPT_MUTE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
curl_setopt($ch, CURLOPT_POSTFIELDS, "$xml_data");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
print_r($output);
exit;
curl_close($ch);
// }
}
and it gives following error
<ams:fault xmlns:ams="http://wso2.org/apimanager/security"><ams:code>900902</ams:code><ams:message>Missing Credentials</ams:message><ams:description>Required OAuth credentials not provided. Make sure your API invocation call has a header: "Authorization: Bearer ACCESS_TOKEN"</ams:description></ams:fault>
You need to add the Authorization header
CURLOPT_HTTPHEADER for curl_setopt() takes an array with each header as an element.
You can use below $header array.
$header = array();
$header[] = 'Content-length: 0';
$header[] = 'Content-type: application/json';
$header[] = 'Authorization: Bearer OAuthAccess_TokenThatIReceived';
curl_setopt($ch, CURLOPT_HTTPHEADER,$header);

"Version Mismatch" error using SOAP and php to connect to API

I am trying to write some code to connect a webpage to an external API. The API's documentation tells me that the request should be done with a SOAP envelope, which should look like this:
<soap:Envelope xmlns:soap="https://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="https://www.w3.org/2001/XMLSchema">
<soap:Body>
<GetNote>
<Request>
<Security>
<Username>username</Username>
<Password>password</Password>
</Security>
<NoteID>noteID</NoteID>
</Request>
</GetNote>
</soap:Body>
</soap:Envelope>
Using some sample code created by a helpful StackOverflow user here: SOAP request in PHP with CURL I then turned it into this (slightly redacted) php:
<?php
$soapUrl = 'https://www.blah.com/DoodadService.cfc?wsdl';
$xml_post_string = '<soap:Envelope xmlns:soap="https://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="https://www.w3.org/2001/XMLSchema">
<soap:Body>
<GetNote>
<Request>
<Security>
<Username>'.$soapUser.'</Username>
<Password>'.$soapPassword.'</Password>
</Security>
<NoteID>'.$soapNoteID.'</NoteID>
</Request>
</GetNote>
</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://www.blah.com/DoodadService.cfc?wsdl",
"Content-length: ".strlen($xml_post_string),
);
$url = $soapUrl;
$ch = curl_init();
echo('set curl<br>');
$f = fopen('request.txt','w');
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);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_VERBOSE,true);
curl_setopt($ch, CURLOPT_STDERR, $f);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string); // the SOAP request
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
echo('about to execute<br>');
// converting
$response = curl_exec($ch);
fclose($f);
echo('Got '.strlen($response).' characters in response<br>');
curl_close($ch);
echo('Saved error strings to request.txt<br> ');
?>
The result was a Version Mismatch error, ie:
<soapenv:Fault>
<faultcode>
soapenv:VersionMismatch</faultcode>
<faultstring>
Version Mismatch</faultstring>
<detail>
<ns1:hostname xmlns:ns1="http://xml.apache.org/axis/">
ip-10-4-4-149</ns1:hostname>
</detail>
</soapenv:Fault>
Can anyone tell me what this error means, what is being "mismatched" against what, and how I might be able to fix it up?
Thanks!
You could try to change the Content-type header to a SOAP+XML content. The charset doesn't need quotation.
"Content-type: application/soap+xml; charset=utf-8"
Otherwise, surely you have missed something that you don't know.
You could find some examples using this API or similar cases with SOAP in another SO questions.
As it turns out, the issue was "online manual giving out of date information". Not a problem that general SOAP experts can help out with after all!
In course of consultation with the API providers, was pointed at a useful tool SoapUI - https://www.soapui.org/ - which, as it turns out, can be used to generate correct Soap envelopes given the API's URL - meaning that I am no longer dependent on this API's providers to have their documentation up to date. I recommend it to anyone else who finds themselves in a similar fix.

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

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