SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Evangelist priti's Avatar
    Join Date
    Aug 2006
    Location
    India
    Posts
    488
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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

    PHP Code:
    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...

  2. #2
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    63 Post(s)
    Tagged
    0 Thread(s)
    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 Code:
    <?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
    Salathe
    Software Developer and PHP Manual Author.


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •