简体   繁体   English

将SOAP转换为PHP调用函数

[英]Translating SOAP to PHP call function

I'm trying to learn how to use SoapUI to integrate Web Services to my website. 我正在尝试学习如何使用SoapUI将Web服务集成到我的网站。 I've been trying to follow PHP's documentation, but it is very confusing. 我一直在尝试遵循PHP的文档,但这非常令人困惑。 My question is: how can I translate this soap code to PHP, so I can call the SOAP function. 我的问题是:如何将该肥皂代码转换为PHP,因此可以调用SOAP函数。 This is what I got so far: 这是我到目前为止所得到的:

  $wsdl = "http://api.rlcarriers.com/1.0.2/ShipmentTracingService.asmx?wsdl";

  $request = [
'APIKey' => 'xxxxxxxxxxxxxxxxxxxxxx',
'traceNumbers' => $pro,
'TraceType' => 'PRO',
'FormatResults' => 'false',
'IncludeBlind' => 'false',
'OutputFormat' => 'Standard'
];

  $client = new SoapClient($wsdl);

  $result = $client->TraceShipment($request);  

  print_r($result); 

However, this is not working. 但是,这不起作用。 I don't know what I'm doing wrong. 我不知道我在做什么错。 I appreciate any help provided. 感谢提供的任何帮助。 I've spent hours trying to figure it out and it's driving me crazy. 我花了数小时试图找出答案,这让我发疯。 This is the soap request code that I get with SoapUI by following this wsdl file: http://api.rlcarriers.com/1.0.2/ShipmentTracingService.asmx?wsdl 这是通过以下wsdl文件通过SoapUI获得的肥皂请求代码: http : //api.rlcarriers.com/1.0.2/ShipmentTracingService.asmx? wsdl

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:rlc="http://www.rlcarriers.com/">
       <soap:Header/>
       <soap:Body>
          <rlc:TraceShipment>
             <!--Optional:-->
             <rlc:APIKey>******************</rlc:APIKey>
             <!--Optional:-->
             <rlc:request>
                <!--Optional:-->
                <rlc:TraceNumbers>
                   <!--Zero or more repetitions:-->
                   <rlc:string>143248716</rlc:string>
                </rlc:TraceNumbers>
                <rlc:TraceType>PRO</rlc:TraceType>
                <rlc:FormatResults>false</rlc:FormatResults>
                <rlc:IncludeBlind>false</rlc:IncludeBlind>
                <rlc:OutputFormat>Standard</rlc:OutputFormat>
                <!--Optional:-->
                <rlc:CustomerData></rlc:CustomerData>
             </rlc:request>
          </rlc:TraceShipment>
       </soap:Body>
    </soap:Envelope>

I don't know if anyone will ever need this, but I figured out the problem. 我不知道是否有人会需要这个,但我发现了问题所在。 I'm just learning about SOAP now, and realized that in order to translate the SOAP call to PHP one needs to treat all tags as arrays. 我现在只是在学习SOAP,并且意识到为了将SOAP调用转换为PHP,需要将所有标签视为数组。 Therefore, given the structure of the request call, the PHP request should look like this: 因此,根据请求调用的结构,PHP请求应如下所示:

$request = array(
    'APIKey' => '***********************',
    'request' => array(
        'TraceNumbers' => array(
            'string' => $pro
            ),
        'TraceType' => 'PRO',
        'FormatResults' => 'false',
        'IncludeBlind' => 'false',
        'OutputFormat' => 'Standard'
    )
);

First mistake is to use the function name as a method of the SoapClient . 第一个错误是将函数名称用作SoapClient的方法。

Correct is to use native method SoapClient::__soapCall() and the function name use as first parameter like this: 正确的是使用本机方法SoapClient :: __ soapCall() ,并且函数名称用作第一个参数,如下所示:

$client = new SoapClient($wsdl);
$result = $client->__call('TraceShipment', $request);

For easier debugging use the try...catch block that gave you access to messages returned from the server: 为了简化调试,请使用try ... catch块,该块使您可以访问服务器返回的消息:

try {
  $result = $client->__soapCall('TraceShipment', $request);
} catch (Exception $e) {
  print_r($e);
  print_r($client);
}

Second mistake 第二个错误
The arguments $request should be an array of associative array, ie two level array , to be accepted by SoapServer: $request参数应该是SoapServer接受的关联数组的数组,即两级数组

$request = [[
  //...
]];

Third mistake 第三个错误
The mandatory arguments are 强制参数是

<s:element minOccurs="0" maxOccurs="1" name="APIKey" type="s:string"/>
<s:element minOccurs="0" maxOccurs="1" name="request" type="tns:ShipmentTracingRequest"/>

So do update your $request array by the request key (updated based on Carlos post): 因此,请通过request密钥(根据Carlos post更新)来更新$request数组:

$request = [[
      'APIKey' => 'xxxxxxxxxxxxxxxxxxxxxx',
      'request' => [
          'TraceNumbers' => [
              'string' => $pro
          ],
          'TraceType' => 'PRO',
          'FormatResults' => 'false',
          'IncludeBlind' => 'false',
          'OutputFormat' => 'Standard'
      ]
]];

When fixed you could get response like: 修复后,您将获得如下响应:

stdClass Object
(
    [TraceShipmentResult] => stdClass Object
        (
            [WasSuccess] => 1
            [Messages] => stdClass Object
                (
                )

            [Result] => stdClass Object
                (
                )
        )
)

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

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