du1462 2016-12-09 16:50
浏览 108
已采纳

无法将soapUI转换为PHP函数

I have been struggling for hours now on a SOAP connection and can't get it to work, this is what I have:

$url  = 'https://xxx/connector.svc?singleWsdl';
$user = 'user';
$pass = 'pass';

$options = array(
        'uri'=>'http://schemas.xmlsoap.org/soap/envelope/',
        'style'=>SOAP_RPC,
        'use'=>SOAP_ENCODED,
        'soap_version'=>SOAP_1,
        'cache_wsdl'=>WSDL_CACHE_NONE,
        'connection_timeout'=>15,
        'trace'=>true,
        'encoding'=>'UTF-8',
        'exceptions'=>true,
    );
try {
    $soapclient = new SoapClient($url, $options);
#   $soapclient = new SoapClient($url);
}
catch(Exception $e) {
    die($e->getMessage());
}

I tried '?wdsl' but then I get:

PHP Fatal error:  SOAP-ERROR: Parsing WSDL: <message> 'IConnector_GetProduct_ServiceFaultFault_FaultMessage' already defined

A request with no parameters works fine:

$result = $soapclient->GetVersionInfo();
$last_request  = $soapclient->__getLastRequest();
$last_response = $soapclient->__getLastResponse();
print "Request:  ".$last_request ."
";
print "Response: ".$last_response."
";
print_r($result);

Result:

Request:  <?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="Unit4.AgressoWholesale.Connectors"><SOAP-ENV:Body><ns1:GetVersionInfo/></SOAP-ENV:Body></SOAP-ENV:Envelope>

Response: <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><GetVersionInfoResponse xmlns="Unit4.AgressoWholesale.Connectors"><GetVersionInfoResult xmlns:a="http://schemas.datacontract.org/2004/07/Unit4.AgressoWholesale.Common.Contracts" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><a:FullVersion>14.4.18.0</a:FullVersion><a:Release>14</a:Release><a:ServicePack>4</a:ServicePack><a:Fix>18</a:Fix><a:Version>0</a:Version><a:CustomCode/><a:AWBuild>38</a:AWBuild><a:AWFix>003</a:AWFix><a:AWCustomBuild/><a:AWCustomFix/><a:AWCustomCustomerCode/></GetVersionInfoResult></GetVersionInfoResponse></s:Body></s:Envelope>
stdClass Object
(
    [GetVersionInfoResult] => stdClass Object
        (
            [FullVersion] => 14.4.18.0
            [Release] => 14
            [ServicePack] => 4
            [Fix] => 18
            [Version] => 0
            [CustomCode] =>
            [AWBuild] => 38
            [AWFix] => 003
            [AWCustomBuild] =>
            [AWCustomFix] =>
            [AWCustomCustomerCode] =>
        )

)

So far so good, but the trouble begins trying to log in, which is a must.. In SoapUI it works with:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:unit="Unit4.AgressoWholesale.Connectors" xmlns:unit1="http://schemas.datacontract.org/2004/07/Unit4.AgressoWholesale.Common.Contracts">
   <soapenv:Header/>
   <soapenv:Body>
      <unit:Login>
         <unit:SecurityContext>
            <unit1:SessionToken></unit1:SessionToken>
            <unit1:UserId>user</unit1:UserId>
            <unit1:Password>pass</unit1:Password>
         </unit:SecurityContext>
      </unit:Login>
   </soapenv:Body>
</soapenv:Envelope>

But converting this to PHP, I'm stranding here.. this is what I have tried:

$data = array( 'SecurityContext' => array( 'SessionToken' => ''
                                        ,'Password'     => $user
                                        ,'UserId'       => $pass)
                            );
$data = array( 'SessionToken' => ''
                            ,'Password'     => $user
                            ,'UserId'       => $pass
                            );

$data = new stdClass;
$data->SecurityContext = new stdClass;
$data->SecurityContext->SessionToken = '';
$data->SecurityContext->UserId       = $pass;
$data->SecurityContext->Password     = $user;

#$result = $soapclient->__call('Login',array($data));
#$result = $soapclient->Login($data);
$result = $soapclient->__soapCall('Login',array($data));

But no matter what I try, event without parameters or an empty array or stdClass, I get:

PHP Fatal error:  Uncaught SoapFault exception: [s:ServiceFault] An exception occurred

It's driving me nuts, I can't find anything on the internet about the fatal exeption '[s:ServiceFault]'

What am I doing wrong? Any help will be greatly appreciated!

  • 写回答

1条回答 默认 最新

  • dourang6423 2016-12-09 17:14
    关注

    OK, writing out the problem sometimes is enough to get to an anwer!

    The solution was two-fold:

    1) get better error reporting by using a try{} catch:

    try { 
        $result = $soapclient->__call('Login',array($data));
        #$result = $soapclient->Login($data);
        #$result = $soapclient->__soapCall('Login',array($data));
    } catch (SoapFault $e) {
        $error = $e->faultcode;
        echo str_replace('>',">
    ",$error) ;
    }
    $last_request = $soapclient->__getLastRequest();
    $last_response= $soapclient->__getLastResponse();
    print "
    Request: " .str_replace('>',">
    ",$last_request);
    print "
    Response: ".str_replace('>',">
    ",$last_response);
    

    That way I got just that litle more information I needed! to:

    2) enter password in the password field and the username in the user field

    It works!

    For those interested

    • Using the array structure and the 'new stdClass' both work
    • all three ways of parsing the call work (also the two commented out ones, use which one you like)
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

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