簡體   English   中英

PHP調用SOAP Web服務

[英]PHP To Call a SOAP Webservice

我想要一些幫助。

我正在嘗試使用PHP調用網絡服務,而我在處理不同類型的消息時遇到了問題。

WSDL是“ http://195.144.16.7/ElastrakEDI/ElastrakEDI.asmx?WSDL”,Web服務稱為GetPartMaster。 首先,用戶名和密碼均為TESTUID和TESTPWD(出於測試目的)。

下面的XML是請求

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:edi="http://elastrak.gr/edi/">
   <soap:Header>
      <edi:AuthHeader>
         <!--Optional:-->
         <edi:Username>TESTUID</edi:Username>
         <!--Optional:-->
         <edi:Password>TESTPWD</edi:Password>
      </edi:AuthHeader>
   </soap:Header>
   <soap:Body>
      <edi:GetPartMaster/>
   </soap:Body>
</soap:Envelope>

此XML是響應

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <soap:Header>
      <AuthHeader xmlns="http://elastrak.gr/edi/">
         <Username>TESTUID</Username>
         <Password>TESTPWD</Password>
      </AuthHeader>
   </soap:Header>
   <soap:Body>
      <GetPartMasterResponse xmlns="http://elastrak.gr/edi/">
         <GetPartMasterResult>
            <elastrakPartMasterFile xmlns="">
               <PartMasterURL>http://195.144.16.7/elastrakEDI/Temp/Parts/OTWKOJL4.txt</PartMasterURL>
               <ErrorCode/>
               <ErrorDescription/>
            </elastrakPartMasterFile>
         </GetPartMasterResult>
      </GetPartMasterResponse>
   </soap:Body>
</soap:Envelope>

我已經嘗試過下面的php代碼,但仍然無法正常工作

<?php

$wsdl = 'http://195.144.16.7/ElastrakEDI/ElastrakEDI.asmx?WSDL';

$trace = true;
$exceptions = true;

$xml_array['Username'] = 'TESTUID';
$xml_array['Password'] = 'TESTPWD';

$client = new SoapClient($wsdl, array('trace' => $trace, 'exceptions' => $exceptions));
$client = new SoapClient($wsdl);
$response = $client->GetPartMaster($xml_array);

try
{
   $client = new SoapClient($wsdl, array('trace' => $trace, 'exceptions' => $exceptions));
   $response = $client->GetPartMaster($xml_array);
}

catch (Exception $e)
{
   echo "Error!";
   echo $e -> getMessage ();
   echo 'Last response: '. $client->__getLastResponse();
}

$response = $response->GetPartMaster->PartMasterURL;

var_dump($response);

?>

再次感謝您的時間和幫助。

您的請求當前構造錯誤,因為您必須考慮到這些實際上是請求中的一部分:

  • SOAP標頭,包含用戶名和密碼
  • SOAP正文,包含GetPartMaster元素

看一下SoapClient類和__setSoapHeaders方法。

這就是為什么我強烈建議您使用WSDL to PHP生成器發送SOAP Request的原因,因為它可以使您輕松構造請求,然后在使用OOP方法的同時處理響應,而不用擔心如何發送參數。 使用生成的PHP SDK和良好的IDE(例如PhpStorm或具有自動補全功能的任何基於Eclipse的IDE),將很容易找到自己的方式。 嘗試PackageGenerator項目。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM