2009-06-27 177 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