XML response want to convert into PHP array it works fine in $response but when I convert into an array it will become empty. How can I convert this XML into an array to display it on the web page? Any information that would point me in the correct direction would be great.
$soapUrl = "http://api.rlcarriers.com/1.0.2/RateQuoteService.asmx"; // asmx URL of WSDL
$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>
<GetRateQuote xmlns="http://www.rlcarriers.com/">
<APIKey>UtN0YWEGRhOjNiNmItODRkMS00NzgyLThiYzNTViEzNjODNmC</APIKey>
<request>
<QuoteType>Domestic</QuoteType>
<CODAmount>0</CODAmount>
<Origin>
<City>New York</City>
<StateOrProvince>NY</StateOrProvince>
<ZipOrPostalCode>10001</ZipOrPostalCode>
<CountryCode>USA</CountryCode>
</Origin>
<Destination>
<City>New York</City>
<StateOrProvince>NY</StateOrProvince>
<ZipOrPostalCode>10009</ZipOrPostalCode>
<CountryCode>USA</CountryCode>
</Destination>
<Items>
<Item>
<Class>150.0</Class>
<Weight>2.5</Weight>
<Width>2.5</Width>
<Height>2.5</Height>
<Length>2.1</Length>
</Item>
</Items>
<DeclaredValue>120</DeclaredValue>
<Accessorials>
<Accessorial>ResidentialPickup</Accessorial>
<Accessorial>ResidentialDelivery</Accessorial>
</Accessorials>
<OverDimensionPcs>0</OverDimensionPcs>
</request>
</GetRateQuote>
</soap:Body>
</soap:Envelope>';
$headers = array(
"Content-type: text/xml;charset=\"utf-8\"",
"Accept: text/xml",
"Cache-Control: no-cache",
"Pragma: no-cache",
"SOAPAction: http://www.rlcarriers.com/GetRateQuote",
"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);
var_dump($response);exit;
$xml = simplexml_load_string($response);
$json = json_encode($xml);
$array = json_decode($json,TRUE);
print_r($array);
Below is the response received from the above code.
string(4037) "trueNEW YORKNY10001USANEW YORKNY10009USALISLong Island,
NY113781-888-575-2632MASPETHNYLISLong Island,
NY113781-888-575-2632MASPETHNY$420.352$442.90$8.86MINIMUM$510.35DISCNTFloor$420.35DISCNF$90.00FUEL23.1%$20.79ARBMH$65.00RPUCWT$132.30RCCWT$132.30NET$440.39STD71825391$839.95$440.39GSDS297780121$38.90$479.29GSAM382622281$77.70$518.09"
Those are SOAP XML, change it's heading by
...
// converting
$response = curl_exec($ch);
curl_close($ch);
$clean_xml = str_ireplace(['SOAP-ENV:', 'SOAP:'], '', $response);
$xml = simplexml_load_string($clean_xml);
$json = json_encode($xml);
$array = json_decode($json,TRUE);
print_r($array);
see: How to parse SOAP XML?
Related
im trying to parse a curl response for this request:
$soapUrl = "https://soap_example_url.com/log_in";
// xml post structure
$xml_post_string = '
<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:debisys-soap-services">
<soapenv:Body>
<urn:Login soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<version xsi:type="xsd:string">'.$version.'</version>
<userName xsi:type="xsd:string">'.$userName.'</userName>
<password xsi:type="xsd:string">'.$password.'</password>
<languageOption xsi:type="xsd:string">'.$language.'</languageOption>
</urn:Login>
</soapenv:Body>
</soapenv:Envelope>
';
$headers = array(
"Content-type: text/xml;charset=\"utf-8\"",
"Accept: text/xml",
"Cache-Control: no-cache",
"Pragma: no-cache",
"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_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);
But when i try to parse "$response2" i get nothing:
$xml = simplexml_load_string($response2);
$json = json_decode(json_encode($xml));
echo json_encode($xml, JSON_PRETTY_PRINT);
Here's what i get:
SimpleXMLElement Object ( )
{}
print_r: stdClass Object ( )
When i use print_r ($response2); function i get this in my browser:
<Login2Response><Version>01</Version><SiteID>xxxx</SiteID><ClerkId>xxxxx</ClerkId><ResponseCode>00</ResponseCode><ResponseMessage>user logged in</ResponseMessage></Login2Response>
And when i use "json_encode" i get this:
"\n \n \n <LoginResponse><Version>01<\/Version><SiteID>xxxxx<\/SiteID><ClerkId>xxxxx<\/ClerkId><ResponseCode>00<\/ResponseCode><ResponseMessage>user logged in<\/ResponseMessage><\/LoginResponse><\/return>\n <\/ns1:Login2Response>\n <\/soapenv:Body>\n<\/soapenv:Envelope>\n"
So i tried copy/paste the xml from my browser to a variable like this:
$returned_xml = '<LoginResponse><Version>01</Version><SiteID>xxxxx</SiteID><ClerkId>xxxxx</ClerkId><ResponseCode>00</ResponseCode><ResponseMessage>user logged in</ResponseMessage></LoginResponse>';
And it works, but i need to use $response2 to make it dynamically, thanks in advance.
I'm closer i did this:
$response2 = preg_replace('/(<\/?)(\w+):([^>]*>)/', '$1$2$3', $response2);
$xml = simplexml_load_string($response2);
$xml = var_dump($xml);
echo '<br><br>';
print_r($xml);
And now i'm getting this response:
object(SimpleXMLElement)#1 (1) { ["soapenvBody"]=> object(SimpleXMLElement)#2 (1) { ["ns1Login2Response"]=> object(SimpleXMLElement)#3 (1) { ["return"]=> string(194) "018821909123400USER LOGGED IN SUCCESSFULLY" } } }
How can i access to only the ["return"] response to parse it as json object?
Hi Can anyone explain me in implementing below SOAP XML in PHP, I saw some questions they were handled using CURL but I want to use SoapClient library in PHP Can anyone help me.
I saw some people used below code in PHP get the simple SOAP , How can I implement the same way in my code
<?php
//Create the client object
$soapclient = new SoapClient('http://www.example.com:8080/test/services/test?wsdl');
//Use the functions of the client, the params of the function are in
//the associative array
$params = array(
'locationID' => '19087525238255',
'custFirstName' => 'Seure',
'custLastName' => 'Install',
'customerType' => 'RESI'
);
$response = $soapclient->octService($params);
var_dump($response);
?>
SOAP XML
<soapenv:Envelope xmlns:soapenv = "http://schemas.xmlsoap.org/soap/envelope/" xmlns:com = "http://test.com/">
<soapenv:Header/>
<soapenv:Body>
<com:OCTService>
<locationID>19087525238255</locationID>
<customer>
<custFirstName>JOHN</custFirstName>
<custLastName>ADAM</custLastName>
<customerType>RESI</customerType>
</customer>
<order>
<orderScheduleType>NoSchedule</orderScheduleType>
<orderScheduledate/>
<reasonCode>NT</reasonCode>
<salesRep>0001</salesRep>
</order>
<Equipments>
<equipment>
<serialNumber>*</serialNumber>
<type>N</type>
</equipment>
<equipment>
<serialNumber>*</serialNumber>
<type>NH</type>
</equipment>
<equipment>
<serialNumber>*</serialNumber>
<type>NH</type>
</equipment>
</Equipments>
<csgServiceCodes>
<CSGServiceCode>
<rateCode>SR002</rateCode>
<packageCode/>
</CSGServiceCode>
<CSGServiceCode>
<rateCode>BA</rateCode>
<packageCode/>
</CSGServiceCode>
</csgServiceCodes>
<voiceFeatures>
<nativeNumbersCount>0</nativeNumbersCount>
<portedNmbers>?</portedNmbers>
</voiceFeatures>
<HuntGroup>
<huntGroupType>?</huntGroupType>
</HuntGroup>
</com:OCTService>
</soapenv:Body>
</soapenv:Envelope>
I not able to achieve using CURL POST.
Here important thing is SOAPAction which you can use in the WSDL document
$headers = array(
"Accept-Encoding: gzip,deflate",
"Content-Type: text",
"Cache-Control: no-cache",
"Username: yourusername",
"Password: password",
"SOAPAction: urn:OCTService",
"Content-length: ".strlen($xml_post_string),
); //SOAPAction: your op URL
$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);
echo $response;
curl_close($ch);
// converting
$response1 = str_replace("<soap:Body>","",$response);
$response2 = str_replace("</soap:Body>","",$response1);
// convertingc to XML
$parser = simplexml_load_string($response2);
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.
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);
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.
?>