简体   繁体   English

PHP调用SOAP Web服务

[英]PHP To Call a SOAP Webservice

i would like some help please. 我想要一些帮助。

I am trying to call a webservice using PHP and i am having problems with different kind of messages. 我正在尝试使用PHP调用网络服务,而我在处理不同类型的消息时遇到了问题。

The WSDL is " http://195.144.16.7/ElastrakEDI/ElastrakEDI.asmx?WSDL " and the Web Service is Called GetPartMaster. WSDL是“ http://195.144.16.7/ElastrakEDI/ElastrakEDI.asmx?WSDL”,Web服务称为GetPartMaster。 The username and password at first are both TESTUID and TESTPWD ( for testing purposes ). 首先,用户名和密码均为TESTUID和TESTPWD(出于测试目的)。

The XML Below is the Request 下面的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>

This XML is the response 此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>

I have tried the php code below but still cant get it to work 我已经尝试过下面的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);

?>

Thank you again for your time and Help. 再次感谢您的时间和帮助。

Your request is currently wrongly constructed as you must take into account that therse is actually thow parts in the request: 您的请求当前构造错误,因为您必须考虑到这些实际上是请求中的一部分:

  • the SOAP header, containing the username and password SOAP标头,包含用户名和密码
  • the SOAP body, containing the GetPartMaster element SOAP正文,包含GetPartMaster元素

Take a look to the SoapClient class and the __setSoapHeaders method. 看一下SoapClient类和__setSoapHeaders方法。

This is why I strongly advise you to use a WSDL to PHP generator to send SOAP Request as it will allow you to easily construct the request and then handle the response while using the OOP approach and not wondering how to send the parameters. 这就是为什么我强烈建议您使用WSDL to PHP生成器发送SOAP Request的原因,因为它可以使您轻松构造请求,然后在使用OOP方法的同时处理响应,而不用担心如何发送参数。 Using the generated PHP SDK and a good IDE such as PhpStorm or any Eclipse based IDE with autocompletion, it'll be easy to find your way. 使用生成的PHP SDK和良好的IDE(例如PhpStorm或具有自动补全功能的任何基于Eclipse的IDE),将很容易找到自己的方式。 Try the PackageGenerator project. 尝试PackageGenerator项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM