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

时间:2008-11-10 21:19:14

标签: c# php web-services nusoap

我使用ASP.NET编写了一个Web服务(在C#中),我正在尝试使用NuSOAP编写一个示例PHP客户端。我绊倒的地方是如何做到这一点的例子;一些节目soapval正在使用(我不太了解参数 - 例如将false传递为string类型等),而其他人只是直接使用{{1} }秒。假设array报告的我的Web服务的WSDL看起来像:

http://localhost:3333/Service.asmx?wsdl

我的第一次PHP尝试如下:

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>

现在除了Param类型是一个复杂的类型,我很确定我的简单<?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); ?> 尝试不会自动使用,我在我的Web服务中查找并看到我标记为的方法$array(没有重命名,字面意思WebMethod)并且看到参数都是默认值(DoSomethingint0是{{1等等。)。

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

3 个答案:

答案 0 :(得分: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);
?>

答案 1 :(得分:3)

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

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

这可能会更方便一点。

http://se.php.net/soap

答案 2 :(得分: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" />