Get a value from SOAP response from multiple xml data using PHP - php

This is the response of the soap request.
<?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>
<GetConfigurationInfoResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://micros-hosting.com/EGateway/">
<configInfoResponse>
<OperationalResult>
<Success>true</Success>
<ErrorCode>Success</ErrorCode>
<ErrorMessage>Success</ErrorMessage>
</OperationalResult>
<ConfigInfoType>
<EConfigurationInfoType>MENUITEMDEFINITIONS</EConfigurationInfoType>
</ConfigInfoType>
**<MenuItemDefinitions><?xml version="1.0" encoding="utf-8"?><?micros-type Micros.PosCore.DataStore.DbRecords.DbMenuItemDefinition[], PosCore, Version=2.5.0.0, Culture=neutral, PublicKeyToken=null?><ArrayOfDbMenuItemDefinition xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><DbMenuItemDefinition><NameOptions>0000000</NameOptions><MenuItemDefID>4055</MenuItemDefID><HierStrucID>25</HierStrucID><MenuItemMasterID>1570</MenuItemMasterID><SequenceNum>1</SequenceNum><Name1><StringNumberId>8507</StringNumberId><StringText>=Breakfast=</StringText></Name1><Name2><StringNumberId>8508</StringNumberId><StringText /></Name2><SluSort>0</SluSort><NluNumber>0</NluNumber><Tare>0</Tare><Surcharge>0</Surcharge><IconNumber>0</IconNumber><OptionBits>00000000</OptionBits><SpecialCount>0</SpecialCount><PrepTime>0</PrepTime><Name3><StringNumberId>0</StringNumberId><StringText /></Name3><LongDescriptor><StringNumberId>0</StringNumberId><StringText /></LongDescriptor><MenuItemClassObjNum>0</MenuItemClassObjNum><NluGroupIndex>0</NluGroupIndex><SluIndex>0</SluIndex><HhtSluIndex>0</HhtSluIndex><MainLevels>00000000</MainLevels><SubLevels>00000000</SubLevels><PosRef>0</PosRef><PrintClassObjNum>0</PrintClassObjNum><PrefixLevelOverride>0</PrefixLevelOverride><GuestCount>0</GuestCount><MenuLevelEntries /><DefaultCondiments /><NextScreen /><MiMasterObjNum>10010000</MiMasterObjNum><CheckAvailability>false</CheckAvailability><OutOfMenuItem>false</OutOfMenuItem></DbMenuItemDefinition><DbMenuItemDefinition;</ArrayOfDbMenuItemDefinition></MenuItemDefinitions>**
</configInfoResponse>
</GetConfigurationInfoResponse>
</soap:Body>
</soap:Envelope>
How can I get value from this <MenuItemDefinitions></MenuItemDefinitions>? This xml element inside another XML data. How to say this kind of XML?
How to filter that value using PHP? I only need get value from the inside <MenuItemDefinitions></MenuItemDefinitions> xml value.
This is the soap request php code.
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://192.168.5.10:8080/EGateway/SimphonyPosApiWeb.asmx");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">
<soap:Body>
<GetConfigurationInfo>
<vendorCode />
<employeeObjectNum>10009</employeeObjectNum>
<configurationInfoType>
<int>1</int>
</configurationInfoType>
<revenueCenter>5</revenueCenter>
<configInfoResponse />
</GetConfigurationInfo>
</soap:Body>
</soap:Envelope>");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type: text/xml;",
"SOAPAction: http://192.168.5.10:8080/EGateway/GetConfigurationInfo"
));
$response = curl_exec($ch);
curl_close($ch);
var_dump($response);

Related

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

Echo PHP Variable in XML and Post value to webservice

I need to publish PHP variable values on SOAP-based web service some how I am not able to convert PHP POST values in XML elements.
How can I get POST variable, say abc, in sName element?
My code in PHP
<?php
//session_start();
$server_name= abc;
$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>
<createtest xmlns="http://tempuri.org/">
<sA>ADD</sA>
<sType>service</sType>
<sName>"$server_name"</sName>
</createtest>
</soap:Body>
</soap:Envelope>';
$headers = array(
"Content-Type: text/xml; charset=utf-8",
"Host: http://example.com",
"Content-length: ".strlen($xml_post_string),
"SOAPAction: http://tempuri.org/createtest"
);
url = 'http://example.com'; // WSDL web service url for request
method/function
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
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);
echo $response = curl_exec($ch);
?>
You have to put quotes around abc, when you define the variable as a string:
$server_name = "abc";
And you could also put the variable in the string like this to make it easier to understand it as a variable in such a long string:
$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>
<createtest xmlns="http://tempuri.org/">
<sA>ADD</sA>
<sType>service</sType>
<sName>'.$server_name.'</sName>
</createtest>
</soap:Body>
</soap:Envelope>';

PHP with soap client API inetegration is not working

I am new for handling soap API Request-Response, I need help to implement SMS code using SMS gateway API.
I am using PHP with curl request for send SMS through SMS Gateway API below is my code to send SMS to customer(s)
<?php
define("USERNAME","#username#");
define("PASSWORD","#password#");
$soapUrl = "soap_wsdl_url";
$xml_post_string = '<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:templateSMS" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns2="urn:ElbaridTNS" xmlns:ns3="namespace" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Header>
<ns3:AuthHeader>
<username>USERNAME</username>
<password>PASSWORD</password>
</ns3:AuthHeader>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<ns1:templateSMS>
<Sms xsi:type="ns2:Sms"><phoneNumber xsi:type="xsd:string">phoneNumber</phoneNumber>
<message xsi:type="xsd:string">Hello User, this is test SMS message. We can successfully send SMS.</message>
<unicodeMessage xsi:type="xsd:string">test SMS</unicodeMessage>
<sms_type_id xsi:type="xsd:string">1</sms_type_id>
<notify xsi:type="xsd:string">0</notify>
<senderId xsi:type="xsd:string">SENDERID</senderId>
<priority xsi:type="xsd:string">2</priority>
<vbApp xsi:type="xsd:string">SoapRequest</vbApp>
<vbIdTime xsi:type="xsd:string">20161130101112</vbIdTime>
<destinationPort xsi:type="xsd:string">-1</destinationPort></Sms>
</ns1:templateSMS>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>';
$headers = array(
"Content-type: text/xml;charset=\"utf-8\"",
"Accept: text/xml",
"Cache-Control: no-cache",
"Pragma: no-cache",
"SOAPAction: templateSMS",
"Content-length: ".strlen($xml_post_string),
);
$url = $soapUrl;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
//curl_setopt($ch, CURLOPT_POSTFIELDS, $xmlRequest);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
echo $response;
$response1 = str_replace("<soap:Body>","",$response);
$response2 = str_replace("</soap:Body>","",$response1);
$parser = simplexml_load_string($response2);
echo $parser['resultCode'];
echo "<pre>";
print_r($parser);
echo "</pre>";
curl_close($ch);
?>
I got this response on $parser['resultCode'] "Send Unicast Message". I don't know what is the meaning of this response. I did R&Ds on this issue since 2 days but not getting any success.
Can anyone help me to fix the above issue?
Support team gave me a request and response sample code that should pass in the xml format, below is the request, response sample structure
Request
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:templateSMS" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns2="urn:ElbaridTNS" xmlns:ns3="namespace" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Header>
<ns3:AuthHeader>
<username>username </username>
<password>password</password>
</ns3:AuthHeader>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<ns1:templateSMS>
<Sms xsi:type="ns2:Sms"><phoneNumber xsi:type="xsd:string">xxxxxx</phoneNumber>
<message xsi:type="xsd:string">test SMS </message>
<unicodeMessage xsi:type="xsd:string">test SMS</unicodeMessage>
<sms_type_id xsi:type="xsd:string">1</sms_type_id>
<notify xsi:type="xsd:string">0</notify>
<senderId xsi:type="xsd:string">SenderId</senderId>
<priority xsi:type="xsd:string">2</priority>
<vbApp xsi:type="xsd:string">SoapRequest</vbApp>
<vbIdTime xsi:type="xsd:string">20161130101112</vbIdTime>
<destinationPort xsi:type="xsd:string">-1</destinationPort></Sms>
</ns1:templateSMS>
</SOAP-ENV:Body>
Response:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:templateSMS" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<ns1:templateSMSResponse>
<templateSMSResponse xsi:type="xsd:string">0#!#200#!#http://198.101.210.203/Soap/service/elbarid|http://212.98.137.180/Soap/service/elbarid#!#SenderId
</templateSMSResponse>
</ns1:templateSMSResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

PHP soap xml request

I have tried to looking around the web and searching a lot but I can't figure out how to do this.
I want to make a SOAP request with a header and XML code.
This is how far I have come: created a variable that holds the header info:
$headers = array(
"POST /somefile.asmx HTTP/1.1",
"Host: a ip adress",
"Content-Type: application/soap+xml; charset=utf-8",
"Content-Length: ".strlen($xml_post_string)
);
and the xml I want to send looking like this:
<?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>
<GetViVaDataT xmlns="http://www.somesite.se/somefile.wsdl">
<PlatsId>int</PlatsId>
</GetViVaDataT>
</soap:Body>
</soap:Envelope>
But I have no idea how to go further. i'm using soap 1.2.
If other people have the same problem that I have, so here is what i did to get it to work.
$soapAction = 'http://www.somesite.com/path/to/file/file.wsdl/function';
$xml = '<?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>
<GetViVaDataT xmlns="http://www.somesite.se/somefile.wsdl">
<PlatsId>int</PlatsId>
</GetViVaDataT>
</soap:Body>
</soap:Envelope>';
$headers = array(
"POST /site.asmx HTTP/1.1",
"Host: a ip adress",
"Content-Type: application/soap+xml; charset=utf-8",
"Content-Length: ".strlen($xml),
'SOAPAction:' .$soapAction
);
$curlData = $xml;
$url='http://site/file.asmx';
$curl = curl_init();
curl_setopt ($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl,CURLOPT_TIMEOUT,120);
curl_setopt($curl,CURLOPT_ENCODING,'xml');
curl_setopt($curl,CURLOPT_HTTPHEADER,$headers);
curl_setopt ($curl, CURLOPT_POST, 1);
curl_setopt ($curl, CURLOPT_POSTFIELDS, $curlData);
$result = curl_exec($curl);

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