I have generated some XML which is saved to a file.
Dummy_Order.xml
In the following code I wanted to open the XML and use it in the method 'ImpOrders'
Here is my code
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
$proxy = new SoapClient('http://soapclient/wsdl/Web?wsdl', array ('trace' => 1));
if (file_exists('Dummy_Order.xml')) {
$xml = file_get_contents('Dummy_Order.xml');
} else {
exit('Failed to open XML.');
}
$xmlstring = new SimpleXMLElement($xml);
$result = $proxy->ImportOrders($xmlstring);
var_dump($result);
echo "REQUEST:\n" . $proxy->__getLastRequest() . "\n";
?>
I am getting a response in $result of 0 imported 0 skipped. So i then did getLastRequest() and it's adding the code from the Method but not adding my XML. It wants my XML in a string - which it current is in and isnt moaning about that (It does moan if i use it ->asXML).
I have tried
$result = $proxy->ImportOrders();
and
$result = $proxy->ImportOrders($xmlstring);
And both show the same result in _getLastRequest, which led me to believe that my string isn't being plugged in.
When I check the functions using _getFunctions it provides the information of this...
ImportResult ImportOrders(string $Orders)
Any help would be awesome!
Ok so i resolved it. All i needed to do was wrap my answer in <<
Like below.
$teststring = <<<XML
$xml
XML;
$result = $proxy->ImportOrders($teststring);
Hope this helps out anyone else using PHP, SoapClient and XML.
Related
I'm using Guzzle with Laravel to get an object from external API with a HTTP. The API return XML Object similar to this (https://www.w3schools.com/php/note.xml)
I need to check one value of the response body. Here is my code:
$client = new \GuzzleHttp\Client();
$res = $client->request('GET', 'https://www.w3schools.com/php/note.xml');
$stringBody = (string) $res->getBody()->getContents();
echo $stringBody;
which is working fine, I mean it display the body as below picture
but I couldn't get one value?
I tried different methods but non of them is working!
for example, this way:
$result = starts_with($stringBody, 'Tove');
or
splitName = explode(' ', $res->getBody());
$first_name = $splitName[0];
echo $first_name;
non is working? I think it consider the body text empty ?
I tried using json_decode but it doesn't work or not supported anymore.
Any idea?
Thanks
What you get from response is xml content. Due to browser compatibility with XML you see only text. You just need SimpleXML class of php for get content as per XML node. Here is sample code
$client = new \GuzzleHttp\Client();
$res = $client->request('GET', 'https://www.w3schools.com/php/note.xml');
$stringBody = (string) $res->getBody()->getContents();
$xml = simplexml_load_string($stringBody);
echo $xml->to;
Hope this help you.
I receive an XML repsonce from a GEO service (PDOK). The $responce->raw_body contains this XML structure:
<xls:GeocodeResponse xmlns:xls="http://www.opengis.net/xls" xmlns:gml="http://www.opengis.net/gml">
<xls:GeocodeResponseList numberOfGeocodedAddresses="1">
<xls:GeocodedAddress>
<gml:Point srsName="EPSG:28992">
<gml:pos dimension="2">121299.73296672151 487003.8972524117</gml:pos>
</gml:Point>
<xls:Address countryCode="NL">
<xls:StreetAddress>
<xls:Street>Rokin</xls:Street>
</xls:StreetAddress>
<xls:Place type="MunicipalitySubdivision">Amsterdam</xls:Place>
<xls:Place type="Municipality">Amsterdam</xls:Place>
<xls:Place type="CountrySubdivision">Noord-Holland</xls:Place>
</xls:Address>
</xls:GeocodedAddress>
</xls:GeocodeResponseList>
</xls:GeocodeResponse>
How can I access the elements in here. For example I want an PHP array to access the element 121299.73296672151 487003.8972524117
to grap the coordinates.
And also the other elements. I used SimpleXML parser but I recieve always null.
I think it has someting to do with the namespaces. But I have no clue how to solve this.
The responce is coming from:
$url = "http://geodata.nationaalgeoregister.nl/geocoder/Geocoder?zoekterm=xxxxx%20xx";
$response = \Httpful\Request::get($url)
->expectsXml()
->send();
$xml = new \SimpleXMLElement($response->raw_body);
print_r( $xml);
output:
SimpleXMLElement Object ( )
Any help is appriciated!
After some digging I found a solution for my problem. Its indeed by the namespaces and when using xpath after the namespace registration you can find the elements you need.
$xml = new \SimpleXMLElement($response);
$xml->registerXPathNamespace('xls', 'http://www.opengis.net/xls');
$xml->registerXPathNamespace('gml', 'http://www.opengis.net/gml');
foreach($xml->xpath('//xls:GeocodeResponseList') as $header)
{
$geocoordinates = $header->xpath('//gml:pos');
$street = (string) ($header->xpath('//xls:Street')[0]);
$place = (string) ($header->xpath('//xls:Place')[2]);
}
echo $geocoordinates[0]; // get the coordinates needs to split on space
echo "<br />";
echo $geocoordinates[0]['dimension']; // get the dimension attribute
echo "<br />";
echo $street;
echo "<br />";
echo $place;
Use "->expectsJson()" instead of "->expectsXml()". Then do this:
$array = json_decode($response->raw_body);
or if you use PHP 7.x: https://github.com/eddypouw/geodata-postal-api
I am confused on this and I appreciate if someone helps me.I am working for a project and I have to get data from a WSDL.this is my webservice url .
url.http://203.109.97.241/axis/services/searchhoteldetails?wsdl
i am first in xml integration.so i dont know how to get data fronm this url.i try to get country name by this way.
<?php
$client = new SoapClient("http://203.109.97.241/axis/services/searchhoteldetails?wsdl", array('soap_version' => SOAP_1_2));
$something = $client->getHotelDetailsXMLResponse(array("country"=>"india"));
echo "<pre>"; print_r($something);
die();
?>
but i cant get result..any one pls help me.
Your are using wrong method name.
Actual method name is getHotelDetailsXML
Request call should look like :
$something = $client->getHotelDetailsXML($xml_request);
$xml_request is request xml. (Please get this xml from document and assign to this variable.).
Modified code:
<?php
$xml_request = "<HotelSearchRequest> <clientinfo> <companycode>companycode</companycode> <username>username</username> <password>password</password> </clientinfo> <hotelinfo> <country>India</country> <city>Goa</city> <checkindate>20/04/2014</checkindate> <checkoutdate>21/04/2014</checkoutdate> <hotelname>thaj</hotelname> <norooms>2</norooms> <starrating/> <roomtype/> <responsetype>C</responsetype> <roominginfo> </hotelinfo> </HotelSearchRequest>";
$client = new SoapClient("http://203.109.97.241/axis/services/searchhoteldetails?wsdl", array('soap_version' => SOAP_1_2));
$something = $client->getHotelDetailsXML($xml_request);
var_dump($something);
die();
?>
I have a SQL query that returns a XML string. It works. However, I just moved to a windows php server(from a linux one) and I can no longer get the xml string from the database. It just comes out as one big string(not in xml format) so I cannot parse through all the data.
$query = "SELECT s FROM returnXML('id') FOR XML AUTO, TYPE;";
$stmt = sqlsrv_query($conn, $query);
if(sqlsrv_fetch($stmt) !== false) {
$xml = sqlsrv_get_field($stmt, 0);
echo stream_get_contents($xml);
var_dump($xml);
}
Any help would be appreciated. With the current code. It prints the data(all crossed out though..) and then 'resource(5) of type (stream)'
Thanks!
Replaced code with
if (sqlsrv_fetch($stmt) !== false) {
$xml_res = sqlsrv_get_field($stmt, 0);
$xml_string = stream_get_contents($xml_res);
$xml = simplexml_load_string($xml_string);
}
$xml is now a php variable that has the xml data in it as type SimpleXMLElement.
I'm trying to parse the xml response from a soap service. However, I can't get simplexml_load_string to work! Here is my code:
//make soap call
objClient = new SoapClient('my-wsdl',
array('trace' => true,'exceptions' => 0, 'encoding' => 'UTF-8'));
$soapvar = new SoapVar('my-xml', XSD_ANYXML);
$objResponse = $objClient->__soapCall($operation, array($soapvar));
//process result
$str_xml = $objClient->__getLastResponse();
$rs_xml = simplexml_load_string($str_xml);
...$rs_xml always has just one element with name Envelope.
However, if I use *"print var_export($objClient->__getLastResponse(),true);"* to dump the result to my browser, then cut and paste it into my code as a string variable, it work fine! This is what I mean:
$str_xml = 'my cut and pasted xml';
$rs_xml = simplexml_load_string($str_xml);
So it seems the problem is somehow related to something $objClient->__getLastResponse() is doing to the string it creates... but I'm at a loss as to what the problem is or how to fix it.
Do the following:
$str_xml = $objClient->__getLastResponse();
$str_xml = strstr($str_xml, '<');
$rs_xml = simplexml_load_string($str_xml);
As it's a quick and easy hack to strip off stuff before the first opening element.