Sunday, January 25, 2009

Parsing string into XMLReader in PHP5

Problem: How to parse string result into XML parser in PHP5
Solution: Use one of several PHP5 extensions such as XMLReader

$reader = new XMLReader();
$reader->XML($resultInString);

Below is the sample code that calls PTT Web Services and extracting only the price of GASOHOL

<?php

$wsdl = 'http://www.pttplc.com/pttinfo.asmx?WSDL';

$client = new SoapClient($wsdl);
//array('proxy_host' => "202.12.97.116",
//'proxy_port' => 8088));


$methodName = 'CurrentOilPrice';

$params = array('Language'=>'EN');

$soapAction = 'http://www.pttplc.com/ptt_webservice/CurrentOilPrice';

$objectResult = $client->__soapCall($methodName,
array('parameters' => $params), array('soapaction' => $soapAction));


// echo $objectResult->CurrentOilPriceResult;
$result = $objectResult->CurrentOilPriceResult;
$reader = new XMLReader();
$reader->XML($result);
$found = false;
while ($reader->read()) {
$tagName = $reader->name;
$reader->read();
$tagValue = $reader->value;
if ($tagName == "PRODUCT" &&
$tagValue == "GASOHOL") {
$found = true;
}
if ($found == true && $tagName == "PRICE") {
$price = $reader->value;
break;
}
}
echo "GASOHOL price is $price";
$reader->close();
?>


Then, you should get the result as shown in the below picture.





Saturday, January 24, 2009

Calling Web Services using PHP5

Problem: how to call .NET Web services operation with multiple parameters using PHP5.

Solution:
Below is the sample code to call .NET Web service using PHP5

Note that you need to enable soap client first by uncommenting or adding the below line in file php.ini
extension=php_soap.dll

// file GlobalWeather.php

<?php

// specify WSDL Web service that we want to call
$wsdl = 'http://webservicex.com/globalweather.asmx?WSDL';

// create SoapClient object
$client = new SoapClient($wsdl);

// in case if we call from KKU network which needs to go through KKU proxy
//,array('proxy_host' => "202.12.97.116",'proxy_port' => 8088));

// initialize method name
$methodName = 'GetWeather';

// initialize method parameters
$params = array('CityName'=>'Khon Kaen','CountryName'=>'Thailand');

// initialize soapAction
$soapAction = 'http://www.webserviceX.NET/GetWeather';

// calling WS operation using soapCall
$objectResult = $client->__soapCall($methodName, array('parameters'=> $params), array('soapaction' => $soapAction));

// save the response
$response = $objectResult->GetWeatherResult;

// display response
echo $response;

?>


This is the result from using file GlobalWeather.php

Saturday, May 19, 2007

How to Change soap:address when Using NetBeans 5.5

Problem:
In developing web service with netbeans and use Sun application server, how to change soap address location
from
uri =http://100.22.111.111:8081/testws/testService?wsdl
to
soap:address location="http://111.222.33.444:8081/testws/testService"

Solution:

1. Go to directory {local glassfish instance}/domains/domain1/config such as C:\Sun\AppServer\domains\domain1\config
2 modify file domains.xml by modifying element http-listener
from
<http-listener acceptor-threads="1" address="0.0.0.0" blocking-enabled="false" default-virtual-server="server" enabled="true" family="inet" id="http-listener-1" port="8080" security-enabled="false" server-name="" xpowered-by="true">
to
<http-listener acceptor-threads="1" address="0.0.0.0" blocking-enabled="false" default-virtual-server="server" enabled="true" family="inet" id="http-listener-1" port="8081"server-name="111.22.33.444" security-enabled="false" xpowered-by="true">
3. Restart Sun Application Server
4. Deploy Web service and view its WSDL
Now we have soap:address change to 111.22.33.444:8081 as we wish.