douhuai2015
douhuai2015
2018-03-22 19:10

将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 douju2014 3年前

    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
                    (
                    )
            )
    )
    
    点赞 评论 复制链接分享
  • dr637349 dr637349 3年前

    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. Therefore, given the structure of the request call, the PHP request should look like this:

    $request = array(
        'APIKey' => '***********************',
        'request' => array(
            'TraceNumbers' => array(
                'string' => $pro
                ),
            'TraceType' => 'PRO',
            'FormatResults' => 'false',
            'IncludeBlind' => 'false',
            'OutputFormat' => 'Standard'
        )
    );
    
    点赞 评论 复制链接分享

相关推荐