Echo PHP Variable in XML and Post value to webservice - php

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

Related

Get a value from SOAP response from multiple xml data using 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);

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

PHP SOAP request with post method

I have a code here and can get only current oil price but I need to get a result the same as the picture below. I have no idea to do this.
<?php
$soapUrl = "http://www.pttplc.com/webservice/pttinfo.asmx?op=CurrentOilPrice";
$xml_post_string = '<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<CurrentOilPrice xmlns="http://www.pttplc.com/ptt_webservice/">
<Language>string</Language>
</CurrentOilPrice>
</soap12:Body>
</soap12:Envelope>';
$headers = array(
"POST /webservice/pttinfo.asmx HTTP/1.1",
"Host: www.pttplc.com",
"Content-Type: application/soap+xml; charset=utf-8",
"Content-Length: " . strlen($xml_post_string)
);
$url = $soapUrl;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
$response1 = str_replace("<soap:Body>", "", $response);
$response2 = str_replace("</soap:Body>", "", $response1);
$parser = simplexml_load_string($response2);
echo $parser->asXML();
You can use SOAP CLIENT is a good tool for use if you work with Web services. This is easy to use, automaticaly convert your response in SimpleXMLObject and you are able to use XPath to find easy your XML tag.

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