Search code examples
phphtmlsoapwsdl

How to output the results of WSDL request in HTML?


I wrote simple client

<?php

$client = new SoapClient("http://www.webservicex.net/geoipservice.asmx?WSDL");
$result = $client->GetGeoIPContext();
var_dump($result);

print $result; // Issue: Catchable fatal error: Object of class stdClass could not be converted to string

?>

How i can output in html $result?

var_dump result:

object(stdClass)[2]
  public 'GetGeoIPContextResult' => 
    object(stdClass)[3]
      public 'ReturnCode' => int 1
      public 'IP' => string '62.122.245.38' (length=13)
      public 'ReturnCodeDetails' => string 'Success' (length=7)
      public 'CountryName' => string 'Russian Federation' (length=18)
      public 'CountryCode' => string 'RUS' (length=3)

Solution

  • Since your variable $result is of type stdClass and its property $GetGeoIPContextResult in which the data is stored (as strings) is also of type stdClass, you could do it in a straight forward manner, e.g.

    // the IP address in a div
    <div><?php echo $result->GetGeoIPContextResult->IP; ?></div>
    // the country name in a div
    <div><?php echo $result->GetGeoIPContextResult->CountryName; ?></div>
    // the country code in a div
    <div><?php echo $result->GetGeoIPContextResult->CountryCode; ?></div>
    

    Additionally you could first check whether it was a success:

    if ($result->GetGeoIPContextResult->ReturnCodeDetails == 'Success') {
        // insert here the code above
    }