从php调用Web服务?

时间:2009-06-27 18:18:42

标签: php web-services

所有

Atlast让我们的管理员在我们的apache服务器上安装了PEAR SOAP模块。现在,当我尝试以下代码时 - 它给了我一个错误“HTTP错误请求”。有人可以帮忙吗?

<html>
<body>
<?php
/* Include PEAR::SOAP's SOAP_Client class: */
require_once('SOAP/Client.php');
$zip = $_REQUEST['zip'];
?>

<form action="wszip.php" method="post">
<table cellspacing="10" bgcolor="CadetBlue">
<tr>
<td><B>Enter Zip Code : </B><input type="text" name="zip" /></td>
<td></td>
<td><input type="Submit" value="Find It!"/></td>
</tr>
</table>
<BR><BR><BR><BR>
</form>

<?php
if($zip != "")
{
    $wsdl_url = "http://www.webservicex.net/uszip.asmx?WSDL";
    $wsdl     = new SOAP_WSDL($wsdl_url);
    $client   = $wsdl->getProxy();
    $params = array('USZip' => $zip);
    $response = $client->GetInfoByZIP($params);
    echo $response;
}
?>

</body>
</html>

感谢。

1 个答案:

答案 0 :(得分:2)

那将是

$client   = $wsdl->getProxy();
// don't wrap it into another array.
// $params = array('USZip' => $zip);
$response = $client->GetInfoByZIP($zip);
var_dump( $response );
但在看到任何结果之前,我的屏幕充满了“PHP弃用:”和“PHP严格标准:非静态方法......”消息。我宁愿使用soap extension
<?php
echo PHP_VERSION, ' ', PHP_OS, "\n";
$client = new SoapClient('http://www.webservicex.net/uszip.asmx?WSDL');
$response = $client->GetInfoByZIP(array('USZip'=>'10006'));
var_dump($response);
,不幸的是,响应被定义为
<s:element minOccurs="0" maxOccurs="1" name="GetInfoByZIPResult">
    <s:complexType mixed="true">
        <s:sequence>
            <s:any/>
        </s:sequence>
    </s:complexType>
</s:element>
,其粗略翻译意味着“响应将是...某事”,即你必须解析xml“手动”。
<?php
echo PHP_VERSION, ' ', PHP_OS, "\n";
$client = new SoapClient('http://www.webservicex.net/uszip.asmx?WSDL');
$response = $client->GetInfoByZIP(array('USZip'=>'10006'));

$doc = new SimpleXMLElement($response->GetInfoByZIPResult->any); echo 'City: ', $doc->Table->CITY[0];

打印
5.3.0RC4 WINNT
City: New York