微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何通过PHP调用C#Web服务?

我使用ASP.NET编写了一个Web服务(在C#中),我正在尝试使用NuSOAP编写一个示例PHP客户端.我绊倒的地方是如何做到这一点的例子;一些显示soapval正在使用(我不太了解参数 – 例如将false作为字符串类型传递等),而其他人只是使用直接数组.假设http:// localhost:3333 / Service.asmx?wsdl报告的我的Web服务的WSDL看起来像:

POST /Service.asmx HTTP/1.1
Host: localhost
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/webservices/DoSomething"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <DoSomething xmlns="http://tempuri.org/webservices">
      <anId>int</anId>
      <action>string</action>
      <parameters>
        <Param>
          <Value>string</Value>
          <Name>string</Name>
        </Param>
        <Param>
          <Value>string</Value>
          <Name>string</Name>
        </Param>
      </parameters>
    </DoSomething>
  </soap:Body>
</soap:Envelope>

我的第一次PHP尝试看起来像:

<?PHP
require_once('lib/nusoap.PHP');
$client = new nusoap_client('http://localhost:3333/Service.asmx?wsdl');

$params = array(
    'anId' => 3, //new soapval('anId', 'int', 3),
    'action' => 'OMNOMNOMNOM',
    'parameters' => array(
        'firstName' => 'Scott',
        'lastName' => 'Smith'
    )
);
$result = $client->call('DoSomething', $params, 'http://tempuri.org/webservices/DoSomething', 'http://tempuri.org/webservices/DoSomething');
print_r($result);
?>

现在除了Param类型是一个复杂类型,我很确定我的简单$数组尝试不会自动使用,我正在我的Web服务中查找并看到我标记为WebMethod的方法(没有重命名,它的字面意思是DoSomething)并且看到参数都是认值(int为0,字符串为null等).

我的PHP语法应该是什么样的,以及如何正确传递Param类型?

解决方法:

你必须用大量的嵌套数组包装东西.

<?PHP
require_once('lib/nusoap.PHP');
$client = new nusoap_client('http://localhost:3333/Service.asmx?wsdl');

$params = array(
      'anId' => 3,
      'action' => 'OMNOMNOMNOM',
      'parameters' => array(
              'Param' => array(
                  array('Name' => 'firstName', 'Value' => 'Scott'),
                  array('Name' => 'lastName', 'Value' => 'Smith')
                       )
      )
);
$result = $client->call('DoSomething', array($params), 
                'http://tempuri.org/webservices/DoSomething', 
                'http://tempuri.org/webservices/DoSomething');
print_r($result);
?>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐