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 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效