在 PHP 中解析 Web 服务响应时出错

Error when parsing the webservice response in PHP

我正处于PHP的学习阶段。我试图从单个 PHP 脚本调用示例 Web 服务。 我从 StockQuote 服务开始。下面是我写的脚本。

<?php
   echo "Stock Quote service check";
   require_once 'nusoap.php';
   $wsdl="http://www.webservicex.net/stockquote.asmx?wsdl";
   $client=new SoapClient($wsdl);

   $param=array('symbol'=>'GOOG');  
   $response = $client->__soapCall('GetQuote', array($params));
   $quotes = simplexml_load_string($response->GetQuoteResult);

   echo $quotes;
   //->Stock[0];

   ?>

下面是我收到的警告:

Deprecated: Assigning the return value of new by reference is deprecated in C:\wamp\www\nusoap.php on line 7384

Warning: Creating default object from empty value in C:\wamp\www\nusoap.php on line 75

Notice: Undefined variable: params in C:\wamp\www\URLExample.php on line 8

Warning: simplexml_load_string(): Entity: line 1: parser error : Start tag expected, '<' not found in C:\wamp\www\URLExample.php on line 9

Warning: simplexml_load_string(): exception in C:\wamp\www\URLExample.php on line 9

Warning: simplexml_load_string(): ^ in C:\wamp\www\URLExample.php on line 9

Notice: Trying to get property of non-object in C:\wamp\www\URLExample.php on line 11

当我手动调用该函数时,我得到了以下输出。

请帮助解析响应 XML 以获取 值。 我发现了类似类型的问题 (Question No: #22060990),但对我的情况没有任何帮助。

您的代码有误,“$param”应该是“$params”,

此外,echo $quotes 将不起作用,因为 $quotes 是一个 SimpleXML 对象,因此您需要将其作为对象访问(例如 $quotes->Stock->Last)

<?php
echo "Stock Quote service check";
require_once 'nusoap.php';
$wsdl="http://www.webservicex.net/stockquote.asmx?wsdl";
$client=new SoapClient($wsdl);
$params=array('symbol'=>'GOOG');  
$response = $client->__soapCall('GetQuote', array($params));
$quotes = simplexml_load_string($response->GetQuoteResult);
echo $quotes->Stock->Last;
?>

您可以从 $quotes->Stock 对象中获取以下数据:

["Symbol"]=>
string(4) "GOOG"
["Last"]=>
string(6) "528.48"
["Date"]=>
string(8) "2/2/2015"
["Time"]=>
string(6) "4:00pm"
["Change"]=>
string(5) "-6.04"
["Open"]=>
string(6) "531.44"
["High"]=>
string(6) "533.00"
["Low"]=>
string(6) "518.55"
["Volume"]=>
string(7) "2842249"
["MktCap"]=>
string(6) "359.5B"
["PreviousClose"]=>
string(6) "534.52"
["PercentageChange"]=>
string(6) "-1.13%"
["AnnRange"]=>
string(15) "487.56 - 604.83"
["Earns"]=>
string(6) "21.021"
["P-E"]=>
string(5) "25.43"
["Name"]=>
string(11) "Google Inc."