php 调用c# .NET 写的webservice(亲测通过)

原创
2016-06-13 10:53:05 710浏览

先上结果图——

C# 代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
///


///ibmfashion 的摘要说明
///

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[SoapDocumentService(RoutingStyle = SoapServiceRoutingStyle.RequestElement)]

//若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
// [System.Web.Script.Services.ScriptService]
public class ibmfashion : System.Web.Services.WebService {
public ibmfashion () {
//如果使用设计的组件,请取消注释以下行
//InitializeComponent();
}
[WebMethod]
public string HelloWorld() {
return "Hello World";
}
[WebMethod]
public int multiplication(int a, int b)
{
return a*b;
}
}

php调用c# webservice代码:
// Pull in the NuSOAP code
ob_start();
require_once('lib/nusoap.php');

$url ="http://localhost:8787/wcf/ibmfashion.asmx?wsdl";
$client = new nusoap_client($url, 'wsdl','','','','');
$client->soap_defencoding='utf-8';
$client->decode_utf8=false;
$client->xml_encoding='utf-8';
//参数转换为数组传递
$ary = array('a' => 11, 'b' => 22);
$result = $client->call('multiplication',$ary);
echo "

".print_r($result,true)."
";

//错误及debug信息
if ($client->fault) {
echo '

Fault

';
print_r($result);
echo '
';
} else {
// Check for errors
$err = $client->getError();
if ($err) {
// Display the error
echo '

Error

' . $err . '
';
} else {
// Display the result
echo '

Result

';
print_r($result);
echo '
';
}
}
// Display the debug messages
echo '

Debug

';
echo '
' . htmlspecialchars($client->debug_str, ENT_QUOTES) . '
';


?>

总结php调用c# .NET webservice常用的几种方法:
法1:
检查System32目录是否有php_soap.dll,如果没有网上下载放到这个目录下。

找到配置文件php.ini 文件, 打开以下扩展
extension = php_soap.dll
extension = php_curl.dll
extension = php_openssl.dll
PHP调用代码如下:
方法1:
$url ="http://www.ws.cn/1/Healthgrow/Service.asmx?wsdl";
$client = new SoapClient($url);

$client->soap_defencoding='utf-8';
$client->decode_utf8=false;
$client->xml_encoding='utf-8'

$result = $client->__soapCall("UserLogin",array("UserLogin"=>array(
'str' => '{"userName":"3","password":"222"}')));
if (is_soap_fault($result)) {
trigger_error("SOAP Fault: (faultcode: {$result->faultcode}, faultstring: {$result->faultstring})", E_USER_ERROR);
}
else
{
echo print_r("return:".$result->UserLoginResult,true);
}

方法2:
同样用php_soap.dll,只是代码略有不同:

$url ="http://www.ws.cn/1/Healthgrow/Service.asmx?wsdl";
$client = new SoapClient($url);

$client->soap_defencoding='utf-8';
$client->decode_utf8=false;
$client->xml_encoding='utf-8';

$result = $client->UserLogin(array('str' => '{"userName":"3","password":"222"}'));

if (is_soap_fault($result)) {
trigger_error("SOAP Fault: (faultcode: {$result->faultcode}, faultstring: {$result->faultstring})", E_USER_ERROR);
}
else
{
echo print_r("return:".$result->UserLoginResult,true);
}
方法3:
使用NuSoap是PHP环境下的WebService编程工具,用于创建或调用WebService。它是一个开源软件,是完全采用PHP语言编写的、通过HTTP收发SOAP消息的一系列PHP类,由NuSphere Corporation(http://dietrich.ganx4.com/nusoap/ )开发。NuSOAP的一个优势是不需要扩展库的支持,这种特性使得NuSoap可以用于所有的PHP环境,不受服务器安全设置的影响。

在处理过程中一定要注意WebService提供的参数是否匹配及正确。

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。