如何通过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);
?>

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...