如何在HTML中输出WSDL请求的结果?

如何在HTML中输出WSDL请求的结果?

问题描述:

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)

我写了简单的客户端 p>

 &lt;?php 
  
 $ client = new SoapClient(“http://www.webservicex.net/geoipservice.asmx?WSDL”); 
 $ result = $ client-&gt; GetGeoIPContext(); 
var_dump($ result); 
  
print $ result;  //问题:可捕获的致命错误:类stdClass的对象无法转换为字符串
 
?&gt; 
  code>  pre> 
 
 

如何在html中输出 $ result strong>? p>

var_dump strong>结果: p>

  object(stdClass)[2]  
 public'GetGeoIPContextResult'=&gt;  
 object(stdClass)[3] 
 public'ReturnCode'=&gt;  int 1 
 public'IP'=&gt;  string '62 .122.245.38'(length = 13)
 public'ReturnCodeDetails'=&gt; 字符串'成功'(长度= 7)
公共'CountryName'=&gt; 字符串'俄罗斯联邦'(长度= 18)
 public'CountryCode'=&gt; 字符串'RUS'(长度= 3)
  code>  pre> 
  div>

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
}

What do you mean by HTML?

If you only need to be able to read the values : you can display results in one go as JSON:

 $readable_json = json_encode($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
 echo '<pre>';
 echo $readable_json;
 echo '</pre>';

or you could use var_export

 $readable_dump = var_export($result, true);
 echo '<pre>';
 echo $readable_dump;
 echo '</pre>';

Silution is simple:

print $result->GetGeoIPContextResult->IP . '<br />';
print $result->GetGeoIPContextResult->ReturnCode . '<br />';
print $result->GetGeoIPContextResult->CountryName . '<br />';
print $result->GetGeoIPContextResult->CountryCode . '<br />';