I'm using the integrated SOAP client in PHP 5.3 and I've tested with this web service: http://www.webservicex.net/globalweather.asmx
I'm calling this method 'GetWeather' and the XML request looks 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>
<GetWeather xmlns="http://www.webserviceX.NET">
<CityName>string</CityName>
<CountryName>string</CountryName>
</GetWeather>
</soap:Body>
</soap:Envelope>
and response XML is 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>
<GetWeatherResponse xmlns="http://www.webserviceX.NET">
<GetWeatherResult>string</GetWeatherResult>
</GetWeatherResponse>
</soap:Body>
</soap:Envelope>
So far I'm getting successfully the array with this code:
<?php
$client = new SoapClient('http://www.webservicex.net/globalweather.asmx?WSDL');
$result = $client->GetWeather(array('CityName' => 'Barcelona', 'CountryName' => 'Spain'));
print_r ($result);
How can I print specific element, let's say the value of the CityName?
So when I want to print the city like this:
echo '<div> ' . $city . '</div>';
How do I get the value of the CityName?
Related
I have a requirement to load the variable from txt file to the particular XML element. but some how I am not able to echoing properly.
Here is my output of txt file (userlist.txt)
abc
xyz
LMN
and below is code .
<?php
foreach(file('userlist.txt') as $line) {
$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>
<Getid xmlns="http://test_xyz/">
<uname>'. $line. '</uname>
<org>Test_GRP</org>
</Getid>
</soap:Body>
</soap:Envelope>';
echo $xml_post_string;
}
?>
and below is OUTPUT
<?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>
<Getid xmlns="http://test_xyz/">
<uname>abc
</uname>
<org>Test_GRP</org>
</Getid>
</soap:Body>
</soap:Envelope>
the uname element is not inline, any idea how I do that.
You need to flag file() to ignore the new lines at the end of each line...
file('userlist.txt', FILE_IGNORE_NEW_LINES)
I had used curl to call the soap server and i got the response like this:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<GenerateAuthPasswordResponse xmlns="NepalTelecom.AuthGateway">
<GenerateAuthPasswordResult>abcd-efgh</GenerateAuthPasswordResult>
<ResultCode>1</ResultCode>
</GenerateAuthPasswordResponse>
</soap:Body>
</soap:Envelope>
and when i tried to parse the soap xml as:
$response = $this->SoapModel->soapCall($xml , $this->vas_wsdl_url);
$obj = simplexml_load_string($response);
echo $obj;die();
[Note: where $response is the above soap response provided in soap xml]
and i get the $obj as some error like this:
Severity: Warning
Message: simplexml_load_string(): namespace warning : xmlns: URI NepalTelecom.AuthGateway is not absolute
please any body could help so fix this issue.
Thank you in advance.
Try this.
<?php
$xml = '<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<GenerateAuthPasswordResponse >
<GenerateAuthPasswordResult>abcd-efgh</GenerateAuthPasswordResult>
<ResultCode>1</ResultCode>
</GenerateAuthPasswordResponse>
<GenerateAuthPasswordResponse >
<GenerateAuthPasswordResult>abcd-efgh</GenerateAuthPasswordResult>
<ResultCode>1</ResultCode>
</GenerateAuthPasswordResponse>
</soap:Body>
</soap:Envelope>';
$xml = simplexml_load_string($xml, NULL, NULL, "http://schemas.xmlsoap.org/soap/envelope/");
$ns = $xml->getNamespaces(true);
$soap = $xml->children($ns['soap']);
$res = $soap->Body->children();
print_r($res);
I get different XML strings via SOAP.
But it is very difficult for me to get the value from XML with PHP.
XML examples:
<?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>
<GetUserInfoResponse xmlns="http://schemas.microsoft.com/sharepoint/soap/directory/">
<GetUserInfoResult>
<GetUserInfo>
<User ID="23" />
</GetUserInfo>
</GetUserInfoResult>
</GetUserInfoResponse>
</soap:Body>
</soap:Envelope>
<?xml version = "1.0" encoding = "utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<GetListItemsResponse xmlns="http://schemas.microsoft.com/sharepoint/soap/">
<GetListItemsResult>
<listitems xmlns:s='uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882'
xmlns:dt='uuid:C2F41010-65B3-11d1-A29F-00AA00C14882'
xmlns:rs='urn:schemas-microsoft-com:rowset'
xmlns:z='#RowsetSchema'>
<rs:data>
<z:row ows_ID="128" />
</rs:data>
</listitems>
</GetListItemsResult>
</GetListItemsResponse>
</soap:Body>
</soap:Envelope>
I would like to get the id.
I tried it like this:
$xml_element = simplexml_load_string($responseContent);
$name_spaces = $xml_element->getNamespaces(true);
$soap = $xml_element->children($name_spaces['soap'])
->Body
->children($name_spaces['rs'])
->GetListItemsResponse
->GetListItemsResult
->listitems
->{'rs:data'}
->{'z:row'}['ows_ID'][0];
But most time I dont know how to get my value.
Is it possible to display a whole array or how do I get the path to the value?
What you could do to get the value of 'ows_ID' is to use the SimpleXMLElement children method and also add the namespaces for the child elements.
You can use the attributes method to get the value for 'ows_ID';
For example:
$responseContent = <<<XML
<?xml version = "1.0" encoding = "utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<GetListItemsResponse xmlns="http://schemas.microsoft.com/sharepoint/soap/">
<GetListItemsResult>
<listitems xmlns:s='uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882'
xmlns:dt='uuid:C2F41010-65B3-11d1-A29F-00AA00C14882'
xmlns:rs='urn:schemas-microsoft-com:rowset'
xmlns:z='#RowsetSchema'>
<rs:data>
<z:row ows_ID="128" />
</rs:data>
</listitems>
</GetListItemsResult>
</GetListItemsResponse>
</soap:Body>
</soap:Envelope>
XML;
$xml_element = simplexml_load_string($responseContent);
$name_spaces = $xml_element->getNamespaces(true);
$rows = $xml_element
->children($name_spaces['soap'])
->Body
->children()
->GetListItemsResponse
->GetListItemsResult
->listitems
->children($name_spaces['rs'])
->children($name_spaces['z']);
foreach ($rows as $row) {
$ows_ID = $row->attributes()->ows_ID;
}
Demo
I got a soap xml response and I need the SessionID. I tried to use SimpleXml_load_string but got a empty object back. Has anybody any idea how to get the SessionID?
This is the xml response:
<?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:Header>
<Header xmlns="http://www.twinfield.com/">
<SessionID>ca363c52-c12f-4988-b06a-e0c41788e254</SessionID>
</Header>
</soap:Header>
<soap:Body>
<LogonResponse xmlns="http://www.twinfield.com/">
<LogonResult>Ok</LogonResult>
<nextAction>None</nextAction>
<cluster>https://c3.twinfield.com</cluster>
</LogonResponse>
</soap:Body>
</soap:Envelope>
Try using DOM. This is one way:
$s = '<?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:Header>
<Header xmlns="http://www.twinfield.com/">
<SessionID>ca363c52-c12f-4988-b06a-e0c41788e254</SessionID>
</Header>
</soap:Header>
<soap:Body>
<LogonResponse xmlns="http://www.twinfield.com/">
<LogonResult>Ok</LogonResult>
<nextAction>None</nextAction>
<cluster>https://c3.twinfield.com</cluster>
</LogonResponse>
</soap:Body>
</soap:Envelope>';
$doc = new DOMDocument();
$doc->loadXML( $s );
echo $doc->getElementsByTagName('SessionID')->item(0)->nodeValue;
I found some thing useful here.Read this https://groups.google.com/forum/#!topic/soap4r/-5IwhynDoEU:
and
https://scn.sap.com/thread/3543465
can some one give me the code for this ... i tried allot using nusoap but steel not getting the result
i gone through many php sites and forums but didnt find the solution . . . .
........................ this is what i want ............................
AvailabilityRequest:
<?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:Header>
<ns1:clsSecurity soap:mustUnderstand="false" xmlns:ns1="http://tempuri.org/
WsHermes/Service1">
<ns1:WebProviderLoginId>hermestest</ns1:WebProviderLoginId>
<ns1:WebProviderPassword>hermestest </ns1:WebProviderPassword>
<ns1:IsAgent>false</ns1:IsAgent>
</ns1:clsSecurity>
</soap:Header>
<soap:Body>
<AirGetAvailability xmlns="http://tempuri.org/HERMESAPI/AirInventory">
<pobjSecurity>
<WebProviderLoginId>arzoo</WebProviderLoginId>
<WebProviderPassword>arzoo123</WebProviderPassword>
<IsAgent>false</IsAgent>
</pobjSecurity>
<PstrInput><AvailabilityRequest><BookingType>O</BookingType>
<JryDetails><Item><Origin>BLR</Origin><Destination>
BOM</Destination><TravelDate>10/31/2013</TravelDate>
</Item></JryDetails><ClassType>Economy</ClassType>
<AirlineCode>6E,SG,</AirlineCode><AdultCount>1
</AdultCount><ChildCount>0</ChildCount><InfantCount>
0</InfantCount><ResidentofIndia>1</ResidentofIndia>
<DirectAccess>0</DirectAccess><TransFeeFlag>0
</TransF eeFlag></AvailabilityRequest>
</PstrInput>
<PstrFinalOutPut />
<pstrError />
</AirGetAvailability>
</soap:Body>
</soap:Envelope>
........................................................
AvailabilityResponse:
<?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>
<AirGetAvailabilityResponse xmlns="http://tempuri.org/HERMESAPI/AirInventory">
<AirGetAvailabilityResult xsi:type="xsd:boolean">true</AirGetAvailabilityResult>
<PstrFinalOutPut><?xml version="1.0" encoding="utf-8" standalone="no"?>
<AvailabilityResponse><UserTrackId>RMYOI97999869995927986939247264785431192
</UserTrackId><ResultCode><Status>1</Status></ResultCode><AvailableFlights><O
ngoingFlights><FlightSegments><SegmentDetails><FlightId>11</FlightId><AirlineCod
e>SG</AirlineCode><FlightNumber>344</FlightNumber><AirCraftType>737</AirCraft
Type><Origin>BLR</Origin><Destination>BOM</Destination><DepartureDateTime>
31/10/2013 10:15:00</DepartureDateTime><ArrivalDateTime>31/10/2013 11:55:00
</ArrivalDateTime><Duration>01Hrs 40 Mins</Duration><NumberofStops>0
</NumberofStops><Via /><CurrencyCode>INR</CurrencyCode>
<Currency_Conversion_Rate /><PaxFareDetails><ClassCode>F</ClassCode>
<ClassType>Economy</ClassType><FewSeatIndicator /><Adult>
<BasicAmount>450</BasicAmount><FareBasis />
<FareType>F Refundable</FareType><FuelSurcharge>2150</FuelSurcharge>
<GrossAmount>3214</GrossAmount><Commission /><Value1 /><Value2 />
</Adult></PaxFareDetails></SegmentDetails></FlightSegments></OngoingFlights>
<ReturnFlights /></AvailableFlights></AvailabilityResponse
</PstrFinalOutPut>
<pstrError />
</AirGetAvailabilityResponse>
</soap:Body>
</soap:Envelope>