【问题标题】:SoapClient does not send parametersSoapClient 不发送参数
【发布时间】:2014-05-19 09:09:02
【问题描述】:

我正在拔头发。我尝试了许多不同的方法,但没有任何效果:

<?php

// Proxy is for Fiddler
$soap = new soapClient( 'http://testi.lemonsoft.eu:22000/CTP/lemonweb/userservices.svc?wsdl',     array(
    'proxy_host' => 'localhost',
    'proxy_port' => '8888'
)); 
try { 
    $test = new stdClass();
    $test->UserName = "foo";
    $test->Password = "bar";
    $test->CompanyDatabase = "baz";

    // This should work:
    print_r($soap->LogIn($test));

    /** The rest are alternative experiments, no avail: **/

    print_r($soap->LogIn(array($test)));

    print_r($soap->LogIn(array('parameters' => $test)));

    print_r($soap->login(array(
        'UserName' => 'foo',
        'Password' =>'bar',
        'CompanyDatabase' => 'baz'
    )));

    print_r($soap->__soapCall('LogIn', array('parameters' => $test)));

    print_r($soap->__soapCall('LogIn', array('parameters' => array(
        'UserName' => 'foo',
        'Password' =>'bar',
        'CompanyDatabase' => 'baz'
    ))));

    print_r($soap->LogIn(new SoapParam($test, "LogIn")));

    print_r($soap->LogIn(new SoapParam(array(
        'UserName' => 'foo',
        'Password' =>'bar',
        'CompanyDatabase' => 'baz'
    ), "LogIn")));

    print_r($soap->__soapCall('LogIn', array('parameters' => array(
        new SoapParam(array(
            'UserName' => 'foo',
            'Password' =>'bar',
            'CompanyDatabase' => 'baz'
        ), "LogIn")
    ))));

} catch (SoapFault $fault) { 
    print_r($fault);
}

?>

我用 fiddler 捕获了请求,响应总是如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://tempuri.org/">
    <SOAP-ENV:Body>
        <ns1:LogIn/>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

好像从未发送过 LogIn 参数。空的 ns1:LogIn 标签真的是这个意思吗?中间是否有一些我无法控制的实例,出于某种原因剥离参数?据我了解,LogIn 方法采用一个参数,根据文档,该参数应该是 PHP stdClass。

【问题讨论】:

    标签: php soap wsdl


    【解决方案1】:

    试试这个:

    class LogInInfo{
        public $UserName = '1';
        public $Password = '2';
        public $CompanyDatabase = '3';
    }
    
    ini_set('display_error', 1);
    error_reporting(E_ALL);
    
    $client = new SoapClient('http://testi.lemonsoft.eu:22000/CTP/LemonWeb/UserServices.svc?singleWsdl', array(
            'classmap'=>array('LogInInfo'=>'LogInInfo'),
            'debug'=>true,
            'trace'=>true
        ));
    
    try {
        $info = new LogInInfo();
        $resp = $client->LogIn($info);
    
    
    } catch(Exception $e) {
        var_dump($e);
    }
    
    print_r($client->__getLastRequest());
    

    结果:

    <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://schemas.datacontract.org/2004/07/Lemonsoft.LemonsoftServiceLibrary.User" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns2="http://tempuri.org/">
    <SOAP-ENV:Body>
        <ns2:LogIn xsi:type="ns1:LogInInfo">
            <ns1:CompanyDatabase>3</ns1:CompanyDatabase>
            <ns1:Password>2</ns1:Password>
            <ns1:UserName>1</ns1:UserName>
        </ns2:LogIn>
    </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>
    

    【讨论】:

    • 它有效,但它没有任何意义。 PHP 和 WSDL 服务之间的接口不应该关心原始数据类型,因为无论如何来回传输的信息都是 XML。我不需要为所有内容定义一个类,然后将它们映射到 SoapClient 实例。我在这里没有得到什么?
    • 如果您仔细观察 wsdl,您会发现登录消息需要具有特定结构的参数。不需要登录类 - 它可以是 stdClass - 但使用类映射将简化您的开发,例如编辑器中的代码竞争:)
    【解决方案2】:

    你能显示 WSDL 文件的内容吗?

    ns1:LogIn 标记表示方法 LogIn 来自 ns1 命名空间(如上定义)。

    【讨论】:

    【解决方案3】:

    我设法从 PHP 传递 SOAP 请求中的参数的唯一方法是这样的:

    // __setSoapHeaders here
    
    $listCriteriaXML = '<List xmlns="LINKHERE">
      <listCriteria>
        <ListCriterion>
          <Name>DateNeeded</Name>
          <SingleValue>' . date("Y-m-d", strtotime("+120 days")) . '</SingleValue>
        </ListCriterion>
        <ListCriterion>
          <Name>limitresults</Name>
          <SingleValue>false</SingleValue>
        </ListCriterion>
      </listCriteria>
    </List>';
    
    $listCriteria = new SoapVar($listCriteriaXML, XSD_ANYXML);
    
    $response = $client->List($listCriteria);
    
    echo $client->__getLastRequest();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-26
      • 2014-07-20
      • 1970-01-01
      • 1970-01-01
      • 2014-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多