当前位置:编程学习 > php >>

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

先上结果图——

C#  代码:
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
/// <summary>
///ibmfashion 的摘要说明
/// </summary>
[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代码:
<?php
// 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 "<pre>".print_r($result,true)."</pre>";

//错误及debug信息
if ($client->fault) {
echo '<h2>Fault</h2><pre>';
print_r($result);
echo '</pre>';
} else {
// Check for errors
$err = $client->getError();
if ($err) {
// Display the error
echo '<h2>Error</h2><pre>' . $err . '</pre>';
} else {
// Display the result
echo '<h2>Result</h2><pre>';
print_r($result);
echo '</pre>';
}
}
// Display the debug messages
echo '<h2>Debug</h2>';
echo '<pre>' . htmlspecialchars($client->debug_str, ENT_QUOTES) . '</pre>';
 
 
?>
 
总结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提供的参数是否匹配及正确。

补充:Web开发 , php ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,