3

我已经使用 ASP.NET(在 C# 中)编写了一个 Web 服务,并且我正在尝试使用 NuSOAP 编写一个示例 PHP 客户端。我被绊倒的地方是如何做到这一点的例子;一些显示soapval正在使用(而且我不太了解参数 - 例如false作为string类型传递等),而另一些只是使用直接arrays。假设我报告的 Web 服务的 WSDLhttp://localhost:3333/Service.asmx?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 类型是一种复杂类型,我很确定我的简单$array尝试不会自动使用,我在我的 Web 服务中设置断点并看到我标记为的方法WebMethod(不重命名它,它的字面意思DoSomething)并且看到参数都是默认值(intis 0stringisnull等)。

我的 PHP 语法应该是什么样的,我必须做什么才能Param正确传递类型?

4

3 回答 3

6

您必须将内容包装在大量嵌套数组中。

<?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);
?>
于 2008-11-10T22:30:36.970 回答
3

有点不相关,但从 PHP5 开始,您对 SOAP 有本机支持。

$client = new SoapClient("some.wsdl");
$client->DoSomething($params);

那可能会方便一点。

http://se.php.net/soap

于 2008-11-10T22:34:51.483 回答
1

这里是具有本机 SOAP 支持的示例:

    // Create a new soap client based on the service's metadata (WSDL)
    $client = new SoapClient("http://some.wsdl",
        array('location' => 'http://127.0.0.100:80/IntegrationService/php'));

    $params = array();
    $params['lead']['Firstname']    = $user->firstname;
    $params['lead']['Lastname']     = $user->lastname;
    $params['lead']['Product']      = $product;
    $params['lead']['JobTitle']     = $user->job_title;
    $params['lead']['Email']        = $user->mail;
    $params['lead']['Phone']        = $user->phone;
    $params['lead']['CompanyName']  = $user->company_name;
    $params['lead']['City']         = $user->city;
    $params['lead']['Industry']     = $user->industry;

    $client->SubmitLead($params);

SoapClient 描述中的“.../IntegrationService/php”是 WCF 中的端点:

<endpoint
            address="php"
            binding="basicHttpBinding"
            contract="Integration.Service.IDrupalIntegrationService" />
于 2010-07-28T10:42:30.137 回答