将“布尔数组”传递给NOAA的SOAP服务

时间:2011-08-10 19:29:25

标签: php api soap soap-client noaa

我正在尝试使用SOAPClient查询NOAA SOAP API以获取某些特定信息。根据{{​​3}}:

,对服务的典型查询是这样的
$client = new SoapClient('http://www.weather.gov/forecasts/xml/DWMLgen/wsdl/ndfdXML.wsdl#NDFDgen');
$result = $client->NDFDgen(40.7893,-96.6938,'glance','2007-04-20T00:00','2007-04-21T00:00',NULL);

很好,很容易。但是,看一下this blog post显示最后一个参数。可以获取一系列布尔值,这些布尔值被发送到服务器以打开响应中的特定内容。正确完成后,查询最终看起来像documentation

当然,我尝试类似......

$client = new SoapClient('http://www.weather.gov/forecasts/xml/DWMLgen/wsdl/ndfdXML.wsdl#NDFDgen');
$result = $client->NDFDgen(40.7893,-96.6938,'time-series','2007-04-20T00:00','2007-04-21T00:00', array('mint' => 1, 'maxt' => 1));

请注意,我还必须将param 3更改为'time-series',因为'glance'只是硬编码它返回的内容(完全忽略第五个参数)。在任何情况下,上述代码都会导致服务器发回空白响应。我在第六次参考中尝试了其他各种各样的事情而没有运气。

那么,最重要的是什么?如何给API提供一个像预期的“布尔数组”?

3 个答案:

答案 0 :(得分:3)

使用简单方法在请求中发送XML参数。对于布尔值,您可以直接使用“ true ”。

$client = new SoapClient('http://www.weather.gov/forecasts/xml/DWMLgen/wsdl/ndfdXML.wsdl#NDFDgen',array('trace' => 1));
$param1 = new SoapParam(40.7893, "latitude");;
$param2 = new SoapParam(-96.6938, "longitude");;
$xmlDocument = '<product xmlns:dwml="http://www.weather.gov/forecasts/xml/DWMLgen/schema/DWML.xsd">glance</product>';
$xmlvar = new SoapVar($xmlDocument,XSD_ANYXML);
$param3 = new SoapParam($xmlvar, "product");;
$param4 = new SoapParam("2011-09-06T00:00", "startTime");;
$param5 = new SoapParam("2011-09-07T00:00", "endTime");;
$xmlDocument = '<weatherParameters xmlns:dwml="http://www.weather.gov/forecasts/xml/DWMLgen/schema/DWML.xsd">
<maxt xsi:type="xsd:boolean">true</maxt>
<mint xsi:type="xsd:boolean">true</mint>
</weatherParameters>';
$xmlvar = new SoapVar($xmlDocument,XSD_ANYXML);
$param6 = new SoapParam($xmlvar, "weatherParameters");;
$result = $client->NDFDgen($param1,$param2,$param3,$param4,$param5,$param6);
echo "REQUEST:".$client->__getLastRequest()."<br>"; 
print_r($result);

答案 1 :(得分:1)

您似乎错过了unitType $Unit中所述的__getFunctions()。您只需将其设置为美国'e'或公制读数'm'即可。看起来它还要求您为所有返回值定义布尔值...而不仅仅是您想要的值。因此,您需要定义

$PARAMS = array('appt' => false,
'conhazo' => false,
'critfireo' => false,
'cumw34' => false,
'cumw50' => false,
'cumw64' => false,
'dew' => false,
'dryfireo' => false,
'iceaccum' => false,
'icons' => false,
'incw34' => false,
'incw50' => false,
'incw64' => false,
'maxrh' => false,
'maxt' => true,
'minrh' => false,
'mint' => true,
'phail' => false,
'pop12' => false,
'prcpabv14d' => false,
'prcpabv30d' => false,
'prcpabv90d' => false,
'prcpblw14d' => false,
'prcpblw30d' => false,
'prcpblw90d' => false,
'precipa_r' => false,
'ptornado' => false,
'ptotsvrtstm' => false,
'ptstmwinds' => false,
'pxhail' => false,
'pxtornado' => false,
'pxtotsvrtstm' => false,
'pxtstmwinds' => false,
'qpf' => false,
'rh' => false,
'rx' => false,
'sky' => false,
'sky_r' => false,
'snow' => false,
'td_r' => false,
'temp' => false,
'temp_r' => false,
'tmpabv14d' => false,
'tmpabv30d' => false,
'tmpabv90d' => false,
'tmpblw14d' => false,
'tmpblw30d' => false,
'tmpblw90d' => false,
'waveh' => false,
'wdir' => false,
'wdir_r' => false,
'wgust' => false,
'wspd' => false,
'wspd_r' => false,
'wwa' => false,
'wx' => false);

然后在声明unitType

后将其添加到您的通话中
$client = new SoapClient('http://www.weather.gov/forecasts/xml/DWMLgen/wsdl/ndfdXML.wsdl');
$result = $client->NDFDgen(40.7893,-96.6938,'time-series',NULL,NULL,'e',$PARAMS);

答案 2 :(得分:0)

如果查看服务的描述,您将看到最后一个参数是复杂数据类型,由NOAA描述。您可以创建weatherParametersType的实例,然后直接修改其成员。 (例如wParams.pop12=True