douhuai2015 2018-03-22 19:10
浏览 56
已采纳

将SOAP转换为PHP调用函数

I'm trying to learn how to use SoapUI to integrate Web Services to my website. I've been trying to follow PHP's documentation, but it is very confusing. My question is: how can I translate this soap code to PHP, so I can call the SOAP function. 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

<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>
  • 写回答

2条回答 默认 最新

  • douju2014 2018-03-22 19:30
    关注

    First mistake is to use the function name as a method of the SoapClient.

    Correct is to use native method SoapClient::__soapCall() and the function name use as first parameter like this:

    $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 {
      $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 = [[
      //...
    ]];
    

    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 = [[
          '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
                    (
                    )
            )
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果