我写了简单的客户端
<?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
?>
如何在 html $result 中输出?
var_dump结果:
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)
由于您的变量$result
是stdClass
类型,并且其存储数据的属性$GetGeoIPContextResult
(作为字符串)也是stdClass
类型,因此您可以直接执行此操作,例如
// 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>
此外,您可以先检查它是否成功:
if ($result->GetGeoIPContextResult->ReturnCodeDetails == 'Success') {
// insert here the code above
}
HTML 是什么意思?
如果您只需要能够读取值:则可以一次性将结果显示为JSON:
$readable_json = json_encode($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
echo '<pre>';
echo $readable_json;
echo '</pre>';
或者你可以使用var_export
$readable_dump = var_export($result, true);
echo '<pre>';
echo $readable_dump;
echo '</pre>';
> Silution很简单:
print $result->GetGeoIPContextResult->IP . '<br />';
print $result->GetGeoIPContextResult->ReturnCode . '<br />';
print $result->GetGeoIPContextResult->CountryName . '<br />';
print $result->GetGeoIPContextResult->CountryCode . '<br />';