I have been working with some code for many hours today, wondering if someone could point me in a better direction than I'm going now.
I have PHP code that is fetching an array of data by sending XML via send_request_via_curl($host,$path,$content).
My function:
function send_request_via_curl($host,$path,$content)
{
$posturl = "https://" . $host . $path;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $posturl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: application/x-www-form-urlencoded"));
curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: text/xml;charset=utf-8"));
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $content);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
// curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
return $response;
}
$headers = array(
"Content-type: text/xml;charset=utf-8",
"Accept: application/xml",
"Cache-Control: no-cache",
"Pragma: no-cache",
"Content-length: " . strlen($content),
);
$content = '<?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>
<GetLocations xmlns="http://tempuri.org/">
<AuthLogin>ClientName</AuthLogin>
<UserName>MyUser</UserName>
<Password>SomePassword</Password>
</GetLocations>
</soap:Body>
</soap:Envelope>';
$response = send_request_via_curl($host,$path,$content);
EDIT:
I'm getting closer, I implemented Baba's code and now getting a different error:
Warning: Unknown: Node no longer exists in....
I have the same code from above, with this now processing the response:
if ($response)
{
$xml = new SimpleXMLElement($response);
$xml->registerXPathNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/");
$path = $xml->xpath("//soap:Body");
$path = $path[0] ;
foreach($path->GetLocationsResponse->GetLocationsResult->SearchLocations->children() as $table)
{
echo $table->Table->LocationName . "<br>";
}
Here's the true XML returned - I noticed Firebug made all the XML lowercase, whereas the source code has capitals mixed in which I think might matter.
<?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>
<GetLocationsResponse xmlns="http://tempuri.org/">
<GetLocationsResult>
<SearchLocations>
<Table>
<LocationName>
</Table>
<Table>
<LocationId>103501</LocationId>
<LocationName>Albuquerque, New Mexico, USA</LocationName>
</Table>
<Table>
<LocationId>101600</LocationId>
<LocationName>Atlanta, Georgia, USA</LocationName>
</Table>
</SearchLocations>
</GetLocationsResponse>
</GetLocationsResult>
</soap:Body>
</soap:Envelope>
THANKS!!!
The response you got is not HTML but XML but it looks like Your XML is wrong there is an invalid tag or you must have made mistake ... see
<LocationName>Hawaii</Location>
^--- it should be LocationName
It should be 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>
<getlocationsresponse xmlns="http://tempuri.org">
<getlocationsresult>
<SearchLocations>
<Table>
<LocationID>10322</LocationID>
<LocationName>Hawaii</LocationName>
</Table>
</SearchLocations>
</getlocationsresult>
</getlocationsresponse>
</soap:Body>
</soap:Envelope>
Read This XML
$xml = new SimpleXMLElement($data);
$xml->registerXPathNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/");
$path = $xml->xpath("//soap:Body");
$path = $path[0] ;
foreach($path->getlocationsresponse->getlocationsresult->SearchLocations->children() as $table)
{
echo $table->LocationName . "<br>";
}
Output
Hawaii
------ Edit --------
Your new XML is wrong again .. this is what it should look like http://codepad.org/JgJfqnrA
Baba is correct, the built in functions for parsing XML are the best way to do this but you could also use regex if the XML is really sent back with mismatching tags.
function regex_all( $capture, $haystack, $return=1 ) {
preg_match_all( "#$capture#", $haystack, $match );
return $match[ $return ];
}
foreach( regex_all('<LocationName>(.*?)<\/', $data) as $locationName ) {
echo "$locationName<br>";
}
This would not be the preferred method because the regex isn't as reliable.
Related
I am a beginner at SOAP and have been trying to figure how to get get a proper response when I call the SOAP API. I have tried diff method since last 3 days but still have no idea. I have already tried similar questions like this on stackoverflow but somehow they are not working for me.
I really appreciated your help or response how I may be able to get this done.
Here's the sample request that I should send:
POST /wsror/Form.asmx HTTP/1.1
Host: 10.159.159.1
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://apps.in/wsror/GetAllVill"
<?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>
<GetAllVill xmlns="http://apps.in/wsror">
<strTaluka>string</strTaluka>
<Username>string</Username>
<Password>string</Password>
</GetAllVill>
</soap:Body>
</soap:Envelope>
here's the response:
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length
<?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>
<GetAllVillResponse xmlns="http://apps.in/wsror">
<GetAllVillResult>dataset</GetAllVillResult>
</GetAllVillResponse>
</soap:Body>
</soap:Envelope>
I have tried fetching response by using below method:
$soapclient = new SoapClient('sensitive info');
$param=array('sensitive info');
$response =$soapclient->GetAllVill($param);
var_dump($response);
and I get below response:
object(stdClass)#2 (1) { ["GetAllVillResult"]=> object(stdClass)#3 (1) { ["any"]=> string(6596) "19200Adco 24000Bandora 3001000600020700Beto 30010tqui 001900Bma 300100060Borim 600019Capar " } }
I have used curls to finally solve this issue.
$url = "http://apps.in/wsror?wsdl";
$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>
<GetAllVill xmlns="http://apps.in/wsror">
<strTaluka>code</strTaluka>
<Username>Username</Username>
<Password>Password</Password>
</GetAllVill>
</soap:Body>
</soap:Envelope>';
$headers = array(
"Content-type: text/xml",
"Content-length: " . strlen($xml),
"Connection: close",
);
$ch = curl_init();
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);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$data = curl_exec($ch);
//echo $data;
$doc = new DOMDocument();
$doc->loadXML($data);
and then looped through the required tags.
$VILLAGES = $doc->getElementsByTagName('VILLAGES');
foreach ($VILLAGES as $VILLAGE) {
$VillageName = $VILLAGE->getElementsByTagName('VillageName')->item(0)->nodeValue;
$Village = $VILLAGE->getElementsByTagName('Village')->item(0)->nodeValue;
// $id = $VILLAGE->getElementsByTagName('taluka')->item(0)->nodeValue;
echo '<option value="'.htmlentities($Village, ENT_QUOTES, "UTF-8").'" >'.htmlentities($VillageName, ENT_QUOTES, "UTF-8").'</option> ';
}
I have a soap request. I am able to send the request and get a response in a string. Now I want to convert it into XML and get the required data from it.
Response
string(1383) "
<?xml version="1.0" encoding="UTF-8"?>
<ResponseMessage xmlns:ns="http://iec.ch/TC57/2011/schema/message" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://iec.ch/TC57/2011/schema/message Message.xsd">
<Header>
<Verb>reply</Verb>
<Noun>EndDeviceControls</Noun>
<Revision>2.0</Revision>
<Timestamp>2019-05-04T10:39:11+04:30</Timestamp>
<Source>HES-BSTC</Source>
<AsyncReplyFlag>true</AsyncReplyFlag>
<ReplyAddress>http://ip:port/AmiWeb/services/Metering</ReplyAddress>
<User>
<UserID>user</UserID>
</User>
<MessageID>6C3F761B-A1EC-4EBE-BB49-67B720C5AE62</MessageID>
<CorrelationID>1001</CorrelationID>
<Property>
<Name>password</Name>
<Value>password</Value>
</Property>
<Property>
<Name>timeout(m)</Name>
<Value>30</Value>
</Property>
</Header>
<Reply>
<Result>OK</Result>
<Error>
<code>0.3</code>
</Error>
</Reply>
</ResponseMessage>"
Code
$xml_post_string = /** #lang text */
'<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:soap="http://soap.inf.hexing.cn">
<soapenv:Header/>
<soapenv:Body>
<soap:doCommand>
<!--Optional:-->
<arg0><![CDATA[<?xml version="1.0" encoding="utf-8"?>
<RequestMessage
xmlns="http://iec.ch/TC57/2011/schema/message"
xmlns:m="http://iec.ch/TC57/2011/EndDeviceControls#"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://iec.ch/TC57/2011/schema/message Message.xsd">
<Header>
<Verb>create</Verb>
<Noun>EndDeviceControls</Noun>
<Revision>2.0</Revision>
<Timestamp>2016-01-01T00:00:00+04:30</Timestamp>
<Source>MDM</Source>
<AsyncReplyFlag>true</AsyncReplyFlag>
<ReplyAddress>http://ip:port/AmiWeb/services/Metering</ReplyAddress>
<AckRequired>true</AckRequired>
<User>
<UserID>'.$userName.'</UserID>
</User>
<MessageID>83c643e6-85c5-43c0-9e0a-fa1deb469b72</MessageID>
<CorrelationID>1001</CorrelationID>
<Property>
<Name>password</Name>
<Value>'.$password.'</Value>
</Property>
<Property>
<Name>timeout(m)</Name>
<Value>30</Value>
</Property>
</Header>
<Payload>
<m:EndDeviceControls>
<m:EndDeviceControl>
<m:reason>Disconnect/Reconnect</m:reason>
<m:EndDeviceControlType ref="3.0.211.23"/>
<m:EndDevices>
<m:mRID>'.$msn.'</m:mRID>
<m:Names>
<m:name>Disconnect</m:name>
<m:NameType>
<m:name>ControlType</m:name>
</m:NameType>
</m:Names>
</m:EndDevices>
</m:EndDeviceControl>
</m:EndDeviceControls>
</Payload>
</RequestMessage>
]]></arg0>
</soap:doCommand>
</soapenv:Body>
</soapenv:Envelope>';
$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
$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_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);
$xml = simplexml_load_string($response);
var_dump($xml);
die();
Output
object(SimpleXMLElement)#121 (0) { }
The output is empty. Now I want two things
To convert string to XML
From the returned XML I want to get the value of <AsyncReplyFlag>true</AsyncReplyFlag> from the response
Update 1
As per suggestion, I have added below code
$xml = simplexml_load_string($response);
echo $xml->asXML();
Output
<?xml version="1.0" encoding="UTF-8"?>
<ResponseMessage xmlns:ns="http://iec.ch/TC57/2011/schema/message" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://iec.ch/TC57/2011/schema/message Message.xsd">
<Header>
<Verb>reply</Verb>
<Noun>EndDeviceControls</Noun>
<Revision>2.0</Revision>
<Timestamp>2019-05-04T14:01:48+04:30</Timestamp>
<Source>HES-BSTC</Source>
<AsyncReplyFlag>true</AsyncReplyFlag>
<ReplyAddress>http://ip:port/AmiWeb/services/Metering</ReplyAddress>
<User>
<UserID>user</UserID>
</User>
<MessageID>F4E15012-D009-4CBC-A610-E937F2620193</MessageID>
<CorrelationID>1001</CorrelationID>
<Property>
<Name>password</Name>
<Value>password</Value>
</Property>
<Property>
<Name>timeout(m)</Name>
<Value>30</Value>
</Property>
</Header>
<Reply>
<Result>OK</Result>
<Error>
<code>0.3</code>
</Error>
</Reply>
</ResponseMessage>
What I have tried?
$xml = new SimpleXMLElement($response);
print_r($xml);
die();
It gives me an empty result
$doc = new DOMDocument('1.0', 'utf-8');
$doc->loadXML($response);
$XMLresults = $doc->getElementsByTagName("AsyncReplyFlag")->item(0)->textContent;
echo $XMLresults;
die();
It gives me an error
Trying to get property of non-object
$XMLresults = $doc->getElementsByTagName("AsyncReplyFlag")->item(0)->textContent;
Still, I am unable to get the value of <AsyncReplyFlag>true</AsyncReplyFlag>
How can I achieve it?
Any help would be highly appreciated.
DOMDocument should have no problem extracting it, as a quick one-liner:
echo (#DOMDocument::loadXML($response))->getElementsByTagName("AsyncReplyFlag")->item(0)->textContent;
... or if you want to meticulously check for errors every step of the way,
$xml_errors=[];
set_error_handler(function(int $errno, string $errstr, string $errfile, int $errline, array $errcontext) use(&$xml_errors){
ob_start();
call_user_func_array('var_dump',func_get_args());
$xml_errors[]=ob_get_clean();
});
$domd=new DOMDocument();
$loaded=$domd->loadXML($response);
restore_error_handler();
if(!$loaded){
if(defined('STDERR')){
fprintf(STDERR,"%s",$response);
}
throw new \RuntimeException("errors parsing XML! xml printed in stderr, parsing errors: ".print_r($xml_errors,true));
}
$ele=$domd->getElementsByTagName("AsyncReplyFlag");
if($ele->length<1){
if(defined('STDERR')){
fprintf(STDERR,"%s",$response);
}
throw new \RuntimeException("did not get AsyncReplyFlag in response! (xml printed in stderr)");
}
echo $ele->item(0)->textContent;
Trying to output a SimpleXMLElement using var_dump() isn't a good idea and as you have seen - doesn't give much.
If you just want to see the XML it has loaded, instead use...
echo $xml->asXML();
which will show you the XML has loaded OK, so then to output the field your after is just
$xml = simplexml_load_string($response);
echo $xml->Header->AsyncReplyFlag;
Using the XPath query
$xml = new SimpleXMLElement($xmlData);
echo $xml->xpath('//AsyncReplyFlag')[0];
OR
You can use xml_parser_create
$p = xml_parser_create();
xml_parse_into_struct($p, $xml, $values, $indexes);// $xml containing the XML
xml_parser_free($p);
echo $values[12]['value'];
For other details, you can print_r($values)
I have been working on a SOAP request / response using CURL with PHP, however I can't seem to work out how to extract the Data for the life of me.
Script for SOAP Request
$soapUrl = "https://xml.proveid.experian.com/IDSearch.cfc?wdsl";
// xml post structure
$xml_post_string = "<?xml version='1.0' encoding='utf-8'?>
<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:cor='http://corpwsdl.oneninetwo'>
<soapenv:Header/>
<soapenv:Body>
<cor:search soapenv:encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'>
<xml xsi:type='xsd:string'><![CDATA[
<Search xmlns:xsd='https://xml.proveid.experian.com/xsd/Search.xsd'>
<Authentication>
<Username>".$user."</Username>
<Password>".$pw."</Password>
</Authentication>
<CountryCode>GBR</CountryCode>
<Person>
<Name>
<Forename>".$firstName."</Forename>
<Surname>".$secondName."</Surname>
</Name>
<DateOfBirth>".$dob."</DateOfBirth>
</Person>
<Addresses>
<Address Current='1'>
<Premise>".$premise."</Premise>
<Street>".$streetName."</Street>
<PostTown>".$postTown."</PostTown>
<Region/>
<Postcode>".$postCode."</Postcode>
<CountryCode>GBR</CountryCode>
</Address>
</Addresses>
<Telephones>
<Telephone>
<Number>".$telephone."</Number>
</Telephone>
</Telephones>
<IPAddress>127.0.0.1</IPAddress>
<Emails>
<Email>".$email."</Email>
</Emails>
<SearchOptions>
<ProductCode>".$product."</ProductCode>
</SearchOptions>
</Search>]]>
</xml>
</cor:search>
</soapenv:Body>
</soapenv:Envelope>";
$headers = array(
"Content-type: text/xml;charset=\"utf-8\"",
"Accept: text/xml",
"Cache-Control: no-cache",
"Pragma: no-cache",
"SOAPAction: https://xml.proveid.experian.com/IDSearch.cfc",
"Content-length: ".strlen($xml_post_string),
);
$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_TIMEOUT, 100);
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);`
Response
<Search xmlns:xsd="https://xml.proveid.experian.com/xsd/Search.xsd">
<CountryCode>GBR</CountryCode>
<Person>
<Name><Forename>Baken</Forename>
<Surname>Jorure</Surname>
</Name>
<DateOfBirth>1989-09-09</DateOfBirth>
<Age>26</Age>
</Person>
<Addresses>
<Address Current="1">
<Premise>410</Premise>
<Street>Beuno Terrace</Street>
<PostTown>Beuno</PostTown>
<Region/>
<Postcode>LL545BT</Postcode>
<CountryCode>GBR</CountryCode>
</Address>
</Addresses>
<Telephones>
<Telephone>
<Number>01154567892</Number>
</Telephone>
</Telephones>
<IPAddress>127.0.0.1</IPAddress>
<Emails>
<Email>bakenjorure#www.com</Email>
</Emails>
<SearchOptions>
<ProductCode>ProveID</ProductCode>
</SearchOptions>
<OurReference>B3C369C0-F001-4FB1-80D3-801CB9D872FE</OurReference>
<SearchDate>2015-10-09T23:46:54</SearchDate>
<Result>
<Summary>
<ReportSummary>
<DatablocksSummary>
<DatablockSummary>
<Name>CreditReference</Name>
<Decision/>
</DatablockSummary>
</DatablocksSummary>
</ReportSummary>
<DecisionMatrix Code="ECIGSUAT" Name="Electronic Cigarettes">
<Decision>
<Outcome Type="Primary">0</Outcome>
<Reason>Individuals DOB has not matched to active CAIS
ER or Citizen Card therefore application has been
referred
</Reason>
</Decision>
</DecisionMatrix>
</Summary>
</Result>
</Search>
Now all I really need it the number in the Outcome tags. I've tried so many ways but I just can't seem to get it.
Any help would be much appreciated.
Thanks in advance.
For simplier usage of SOAP, you can use PHP builtin SOAP client.
http://php.net/manual/en/class.soapclient.php
With SoapClient class you could do something like this:
<?php
$soapUrl = "https://xml.proveid.experian.com/IDSearch.cfc?wdsl"
$soapClient = new SoapClient($soapUrl);
$parameters = array();
$result = $soapClient->Search($parameters);
?>
In result you will get native PHP data structure, not plain text XML as in here.
I am completly new to SOAP operations.
I have been provided with an XML document (SOAP) to get some collection points for a shipping method.
From the manual located here:
http://privpakservices.schenker.nu/package/package_1.3/packageservices.asmx?op=SearchCollectionPoint
I can see that I need to use the following SOAP request:
POST /package/package_1.3/packageservices.asmx HTTP/1.1
Host: privpakservices.schenker.nu
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://privpakservices.schenker.nu/SearchCollectionPoint"
<?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>
<SearchCollectionPoint xmlns="http://privpakservices.schenker.nu/">
<customerID>long</customerID>
<key>string</key>
<serviceID>string</serviceID>
<paramID>int</paramID>
<address>string</address>
<postcode>string</postcode>
<city>string</city>
<maxhits>int</maxhits>
</SearchCollectionPoint>
</soap:Body>
</soap:Envelope>
The thing is that i don't know how to send this as a request using PHP, and how to get the response.
Any help to pinpoint me in the right direction, is much appreciated.
UPDATE
I can read the response data with var_dump. However, I am not able to read individual element data.
I need to read data as below
foreach($parser as $row) {
echo $row->customerID;
echo $row->key;
echo $row->serviceID;
}
If anyone should be interested, i have provided the correct answer:
$soapUrl = "http://privpakservices.schenker.nu/package/package_1.3/packageservices.asmx?op=SearchCollectionPoint";
$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><SearchCollectionPoint xmlns="http://privpakservices.schenker.nu/"><customerID>XXX</customerID><key>XXXXXX-XXXXXX</key><serviceID></serviceID><paramID>0</paramID><address>RiksvÅ gen 5</address><postcode>59018</postcode><city>Mantorp</city><maxhits>10</maxhits></SearchCollectionPoint></soap12:Body></soap12:Envelope>';
$headers = array(
"POST /package/package_1.3/packageservices.asmx HTTP/1.1",
"Host: privpakservices.schenker.nu",
"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);
following example might help you.
SOAP XML schema
<x:Envelope xmlns:x="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://www.dataaccess.com/webservicesserver/">
<x:Header/>
<x:Body>
<web:NumberToDollars>
<web:dNum>10</web:dNum>
</web:NumberToDollars>
</x:Body>
</x:Envelope>
PHP code
$wsdl = 'http://www.dataaccess.com/webservicesserver/numberconversion.wso?WSDL';
try{
$clinet=new SoapClient($wsdl);
$ver =array("dNum"=>"2002");
$quates=$clinet->NumberToDollars($ver);
var_dump($quates);
}
catch(SoapFault $e)
{
echo $e->getMessage();
}
Actually I create a Soap Proxy in which I get the client request and I need to post the request further to another SOAP server (with c_url).
The response is successfully obtained (as xml with <SOAP-ENV and all others).
The problem is that in my SOAP PROXY I want to return exactly the response and if my server is returning the xml the SOAP Server actually return the XML file wrap up
<SOAP-ENV:Envelope ...>
<SOAP-ENV:Body>
<ns1:loginResponse>
my xml that already contains <soap:Envelope, <soap:Body> and <namesp1:loginResponse>
</ns1:loginResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
The question is: How can I make the soap server to return exactly the response that i want without wrapping up with soap envelope and others?
Thanks.
UPDATED:
My soap server:
$server = new SoapServer($myOwnWsdlPath);
$this->load->library('SoapProxy');
$server->setClass('SoapProxy', $params );
$server->handle();
My soap Porxy with c_url:
public function __call($actionName, $inputArgs)
{
//some logic
$target = ...
$url = ..
$soapBody =..
$headers = ..
$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_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_TIMEOUT, 100);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $soapBody); // the SOAP request
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch); //soap xml response
curl_close($ch);
file_put_contents('/tmp/SoapCurl.txt', var_export($response, true));
return $response;
}
The response from /tmp/SoapCurl.txt is the correct one:
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope ...>
<soap:Body>
<namesp1:loginResponse>
<session_id xsi:type="xsd:string">data</session_id>
</namesp1:loginResponse>
</soap:Body>
</soap:Envelope>
My soap server response is wrong:
<SOAP-ENV:Envelope ...>
<SOAP-ENV:Body>
<ns1:loginResponse>
<soap:Envelope ...>
<soap:Body>
<namesp1:loginResponse>
<session_id xsi:type="xsd:string">correct data</session_id>
</namesp1:loginResponse>
</soap:Body>
</soap:Envelope>
</ns1:loginResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
The fix I found was to extend the 'handle' function of SoapServer
Discard the output of SoapServer (with ob_end_clean) and replace it with my data
class MySoapServer extends SoapServer
{
public function handle($soap_request = null)
{
parent::handle();
ob_end_clean();
ob_start();
echo $_SESSION['data'];
}
}