PHP 调用 C#编写的webservice
C# 服务端代码
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Services; using System.Data; using System.Xml; using System.Web.Services.Protocols; using System.Data.SqlClient; /// <summary> ///GetCRMDB 的摘要说明 /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] //若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。 // [System.Web.Script.Services.ScriptService] public class GetCRMDB : System.Web.Services.WebService { public MySoapHeader myHeader = new MySoapHeader(); public GetCRMDB() { //如果使用设计的组件,请取消注释以下行 //InitializeComponent(); } [SoapHeader("myHeader")] [WebMethod(Description = "--获取客户信息 <BR>UserName : 加密的用户姓名<br>DeptID : 加密的部门编号 <Br> OfficeID : 加密的用户所属分公司 <br> CheckSum : 验证数字 ")] public XmlDocument GetCompanyMaster(string UserName, string DeptID, string OfficeID, string CheckSum) { XmlDocument doc = new XmlDocument(); string UserID=""; if (Vail(UserName, DeptID, OfficeID, CheckSum, out UserID, out doc)) { return GetCompanyMaster(UserID); } else { return doc; } } }
MySoapHeader 类文件
using System; using System.Collections.Generic; using System.Linq; using System.Web; /// <summary> ///MySoapHeader 的摘要说明 /// </summary> public class MySoapHeader : System.Web.Services.Protocols.SoapHeader { private string userID = string.Empty; private string userPW = string.Empty; public string UserId { get { return userID; } set { userID = value; } } public string UserPW { get { return userPW; } set { userPW = value; } } public MySoapHeader() { } public MySoapHeader(string name, string password) { userID = name; userPW = password; } private bool IsValid(string nUserId, string nPassWord, out string nMsg) { nMsg = ""; try { if (nUserId == "test" && nPassWord == "testtest") { return true; } else { nMsg = "对不起,你无权调用Web服务"; return false; } } catch { nMsg = "---对不起,你无权调用Web服务"; return false; } } public bool IsValid(out string nMsg) { return IsValid(userID, userPW, out nMsg); } }
PHP 呼叫webservice 方法,仅供参考
<?php $c = new SoapClient(‘******?wsdl‘); $v = array(‘UserId‘=>‘test‘, ‘UserPW‘=>‘testtest‘); $headers = new SoapHeader(‘http://tempuri.org/‘, ‘MySoapHeader‘,$v); //soaphead head 验证 $c->__setSoapHeaders(array($headers));//添加soapheader $obj = $c->GetCompanyMaster(array(‘UserName‘=>‘****‘, ‘DeptID‘=>‘*****‘, ‘OfficeID‘=>‘*****‘, ‘CheckSum‘=>‘****‘)); //呼叫webservice 具体方法 $GetCompanyMasterResult = $obj->GetCompanyMasterResult; if($GetCompanyMasterResult){ var_dump($GetCompanyMasterResult); }else{ print "Fail"; } echo ""; ?>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。