Trying to call webservice from SoapClient

Hi ,

I am trying to call a webservice via SoapClient…

webservice:http://www.earthtools.org/timezone/40.71417/-74.00639

dummy Class I am writing

class worldTimeZone extends SoapClient
{
	private $soapurl='http://www.earthtools.org/';
	private $long=0; //-74.00639
	private $latitude=0; //40.71417
	private $final_url=NULL;
	
	//timezone/
	function __construct($long,$latitude)
	{	
		$this->long=$long;
		$this->latitude=$latitude;
		$this->final_url=$this->soapurl;
		try{
		return new SoapClient(NULL,array('location'=>$this->final_url,'uri'=> "http://test-uri/","trace"=> 1,"exceptions" => 0));
		}catch(Exception $e)
		{
			echo '<pre>';
			var_dump($e);
		}
	}
	
	function WB_function()
	{
		echo '<pre>';
		return $this->_getFunctions();
	}
}

$l=-74.00639;
$la=40.71417;
$sobj=new worldTimeZone($l,$la);
$v=$sobj->__soapCall('timezone',array($l,$la));

Error:Fatal error: Uncaught SoapFault exception: [Client] Error finding “uri” property in C:\www\list.php:35

Any idea …whats wrong…! Or where I am making mistake in my class definition or method…

There are a few key issues with your code, I’ll outline them briefly.

You are not properly constructing the extended SoapClient. return new SoapClient(...) should be parent::__construct(...). Constructor methods should never return anything.

The next issue, and this is the big one, the EarthTools web services (docs) do not talk SOAP. They’re plain XML that you need to figure out how to a) construct the correct URLs for your needs (looks like you can do this already), and b) parse the response XML into PHP variables yourself.

For part b), there are lots of tools available including DOM, SimpleXML and XMLReader. See http://php.net/refs.xml

A very basic example would look like:


<?php

$lon =-74.00639;
$lat =40.71417;

$url = 'http://www.earthtools.org/timezone/'.urlencode($lat).'/'.urlencode($lon);
$xml = simplexml_load_file($url);

// Then access the values

$localtime = (string) $xml->isotime;
echo $localtime; // e.g. 2011-09-21 07:09:50 -0500