Web服务相关知识在此不叙述,还有保证PHP SOAP扩展已开启。
.NET发布Web服务相当容易。步骤简单叙述如下:
1)在Visual Studio环境(2010为例)的资源管理器,右键点击网站项目,然后选“添加web引用”,弹出如下图框:
2)因为本地测试,选择“此解决方案中的Web服务”,然后选择已经建立好的.asmx文件(在此以Service.asmx为例)。
3)到此便建立好Web Service服务。例:http://localhost:3395/WebSite2/Service.asmx
App_Code/Service.cs代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
///
///MyWebService 的摘要说明
///
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
// [System.Web.Script.Services.ScriptService]
public class Service : System.Web.Services.WebService {
public Service () {
//如果使用设计的组件,请取消注释以下行
//InitializeComponent();
}
[WebMethod(Description = "This is a test"]
public string getName(string name) {
return "This is " + name;
}
}
现在由PHP SOAP扩展使用该Web服务:
$wsdl = "http://localhost:3395/WebSite2/Service.asmx?wsdl";
$client = new SoapClient($wsdl, array('trace' => true));
// 传递函数
$params = array('name' => 'php');
$ret = $client->__soapCall('getName', array('parameters' => $params));
echo $ret->getNameResult // 最后打印: This is PHP因为不同的程序通信,这里必须用soapCall()方法调用一个SOAP操作。调用后可以在WSDL(http://localhost:3395/WebSite2/Service.asmx?wsdl)查找其自动产生的属性或方法。