您好,欢迎访问一九零五行业门户网

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

先上结果图——
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代码:
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提供的参数是否匹配及正确。
http://www.bkjia.com/phpjc/477901.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/477901.htmltecharticle先上结果图 c# 代码: using system; using system.collections.generic; using system.linq; using system.web; using system.web.services; using system.web.services.protocols; //...
其它类似信息

推荐信息