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条)

报告相同问题?

悬赏问题

  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历