【问题标题】:Turning a SOAP response into an STD object with PHP使用 PHP 将 SOAP 响应转换为 STD 对象
【发布时间】:2013-01-23 10:46:05
【问题描述】:

我正在为一个项目学习 SOAP,现在我了解了它的基础知识。不敢相信我以前从未尝试过使用它。这很棒。但无论如何,我的问题。

我已成功将我的请求响应打印到网络浏览器,但我似乎无法将此响应转换为 STD 对象。

我一直在学习一个教程并将其调整为不同的 WDSL 文件,以完全理解我在做什么。

这是我的 PHP 文件。

<?

//////////////////////////////////////
//
//  ABOUT:      This file will send a request to the WSDL file and return a result in the browser window
//  AUTHOR:     Brad Bird
//  DATE:       07/02/2013
//
//////////////////////////////////////

// Setup the SOAP Client options
$wsdl = "http://www.mobilefish.com/services/web_service/countries.php?wsdl";
$options = array(
    "trace" => 1,
    "exception" => 0
);

// Creates new instance of the SOAP Client
$client = new SoapClient($wsdl, $options);

// Return a set of information using one function
$countryCode = "af";
$values = $client->countryInfoByIana($countryCode);

// Prints the details of the request and response to the browser
print "<h2>SOAP Details</h2>";
print "<pre>";
print "<h3>Request</h3> " . htmlspecialchars($client->__getLastRequest()) . "<br />";
print "<h3>Response</h3> " . htmlspecialchars($client->__getLastResponse());
print "</pre>";

// Prints the request in XML format
$xml = $values->countryInfoByIanaResponse;

print "<h2>stdClass Object</h2>";
print "<pre>";
print_r($xml);
print "</pre>";

我试图从中获取请求的这个 WSDL 文件就在这里。 http://www.mobilefish.com/services/web_service/countries.php?wsdl

由于某种原因,STD 对象部分没有显示任何内容。有什么想法吗?

【问题讨论】:

  • print_r($values); 会发生什么?
  • 我现在在手机上。但我确实尝试过,但它没有产生 std 对象。
  • +1 用于复制/粘贴准备好的代码。下次可能会缩短一点-提出问题的人可能是作者。
  • 如果 print_r($values); 什么也没产生,那么 $client-&gt;countryInfoByIana($countryCode); 什么也不返回。
  • @ExternalUse 很抱歉,我本可以在将其发布到此处之前将其缩短一点!我现在已经选择了我选择的答案:)。

标签: php soap wsdl stdclass


【解决方案1】:

嗯,$xml 不是 stdClass 的对象,而是null$values 然而是。 $values-&gt;countryInfoByIanaResponse 不存在。您在 $values 中拥有的只是

: object(stdClass) = 
  ianacode: string = "af"
  countryname: string = "Afghanistan"
  latitude: double = 33.93911
  longitude: double = 67.709953

不确定您到底要做什么 - 可能在 $client 上调用一个方法,而不是在您的结果上? 另外,请查看有关 error_reporting 的 PHP 手册,然后您会在 print_r 行中偶然发现此通知:Undefined property: stdClass::$countryInfoByIanaResponse。

【讨论】:

  • 对不起,我是个白痴。 $values 以前不起作用,但自从我上次测试以来,我可能更改了以前的代码中的某些内容。这正是我所需要的!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-15
  • 1970-01-01
  • 2022-10-05
  • 2012-04-24
相关资源
最近更新 更多