如何通过 PHP 调用 C# Web 服务?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/279220/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to invoke a C# web service through PHP?
提问by cfeduke
I've written a web service using ASP.NET (in C#) and I'm attempting to write an example PHP client using NuSOAP. Where I'm tripped up on are examples of how to do this; some show soapval
being used (and I don't quite understand the parameters - for example passing false
as string
types, etc.), while others are just using straight array
s. Let's say the WSDL for my web service as reported by http://localhost:3333/Service.asmx?wsdl
looks something like:
我已经使用 ASP.NET(在 C# 中)编写了一个 Web 服务,并且我正在尝试使用 NuSOAP 编写一个示例 PHP 客户端。我被绊倒的地方是如何做到这一点的例子;一些soapval
正在使用的节目(我不太了解参数 - 例如false
作为string
类型传递等),而其他人只是使用直接的array
s。假设我所报告的 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>
My first PHP attempt looks like:
我的第一次 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);
?>
Now aside from the Param type being a complex type which I'm pretty sure my simple $array
attempt will not automagically work with, I'm breakpointing in my web service and seeing the method I've marked as WebMethod
(without renaming it, its literally DoSomething
) and seeing the arguments are all default values (the int
is 0
, the string
is null
, etc.).
现在,除了 Param 类型是一个复杂类型,我很确定我的简单$array
尝试不会自动使用,我正在我的 Web 服务中断点并看到我标记为的方法WebMethod
(没有重命名它,它的字面意思DoSomething
)并且看到参数都是默认值(int
is 0
, the string
isnull
等)。
What should my PHP syntax look like, and what do I have to do to pass the Param
type correctly?
我的 PHP 语法应该是什么样的,我必须做什么才能Param
正确传递类型?
采纳答案by John Lemp
You have to wrap things in tons of nested arrays.
您必须将事物包装在大量嵌套数组中。
<?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);
?>
回答by jishi
Sort of unrelated but since PHP5 you have native support for SOAP.
有点不相关,但从 PHP5 开始,您就拥有对 SOAP 的本机支持。
$client = new SoapClient("some.wsdl"); $client->DoSomething($params);
That might be a little more convenient.
这样可能方便一些。
回答by Vladislav
Here the sample with native SOAP support:
这是具有本机 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);
Where '.../IntegrationService/php' in SoapClient description is endpoint in WCF:
SoapClient 描述中的“.../IntegrationService/php”是 WCF 中的端点:
<endpoint
address="php"
binding="basicHttpBinding"
contract="Integration.Service.IDrupalIntegrationService" />