2009-06-27 146 views
1

全部,从PHP调用Web服务?

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> 

谢谢。

+0

<形式行动= “wszip.php” 方法= “POST”> <表CELLSPACING = “10” BGCOLOR = “藏青”> ​​输入邮编: ​​ ​​ <?PHP 如果($拉链!= “”) { \t $ wsdl_url =“http://www.webservicex.net/uszip.asmx?WSDL”; \t $ wsdl = new SOAP_WSDL($ wsdl_url); \t $ client = $ wsdl-> getProxy(); \t $ params = array('USZip'=> $ zip); \t $ response = $ client-> GetInfoByZIP($ params); \t echo $ response; } ?> – thezone 2009-06-27 18:21:12

回答

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

+0

是啊..在元素一定是整个SOAP规范的最可笑的部分。 – troelskn 2009-06-27 23:14:07