设为首页 收藏本站
查看: 923|回复: 0

[经验分享] JSP站点中集成Discuz论坛整体解决方案 -第二章 用PHP创建WebService服务端

[复制链接]

尚未签到

发表于 2017-4-14 09:03:25 | 显示全部楼层 |阅读模式
本章节将详细讲述如何用PHP创建WebService服务端。

开发工具:Zend Studio 7.2;
部署环境:WampServer2.0c(包括了Apache、MySql、PHP)。
 
1.    用Zend Studio 7.2构造项目工程。
由于我们采用的部署环境是wamp,所以dbgp-bbs项目是部署在www目录下,如下图所示:
 
DSC0000.jpg
 
大家都知道,用UCenter安装完Discus后,论坛dbgp-bbs是没有任何工程文件的,所以在这种情况下是不能直接使用Zend Studio工具来对其进行开发的,所以下面我们的第一步就是如何把已经安装好的dbgp-bbs项目导入到Zend Studio开发环境中。
第一种方法,你可以随意用Zend Studio新建一个PHP Project,然后把工程文件拷贝到bbs目录下,然后用记事本工具打开这些文件进行编辑,改成本项目的内容,如.project文件内容:
 

<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>dbgp_bbs</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.wst.validation.validationbuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.dltk.core.scriptbuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.php.core.PHPNature</nature>
</natures>
</projectDescription>
 
第二种方法,你可以把Zend Studio的默认WorkSpace设置为www\dbgp目录,然后新建一个PHP Project项目dbgp-bbs,把指向www\dbgp\bbs目录,创建完毕后在Zend Studio 中刷新一下整个项目,就可以看到所有的目录文件了。
2.    创建服务端PHP文件。
在工程中新建一个目录,命名为usersyncservice,然后在目录中新建一个PHP文件,在文件内编写服务端代码。本文不再讲解与PHP语法相关的事情,阁下可以参阅相关PHP技术文档。现在把代码贴出来,关键之处已经做了详细的注解。

<?php
require_once '../include/common.inc.php';
require_once DISCUZ_ROOT.'./forumdata/cache/cache_profilefields.php';
require_once DISCUZ_ROOT.'./uc_client/client.php';
/**
* 用户资料同步类。
* @author shangbingbing
*/
class UserSync
{
/**
* 同步保存用户。
* 当系统管理员在门户站点中注册新用户时,同时调用此接口方法,同步新增论坛系统的此用户。
* @param unknown_type $userName 用户名称
* @param unknown_type $password 登录密码(明码)
* @param unknown_type $email 信箱
*/
public function SyncSaveUser($userName,$password,$email)
{
//把用户信息保存到UC_MEMBER表中
$uid = uc_user_register($userName, $password, $email, '', '', '127.0.0.1');
//把用户信息保存到CDB_MEMBER表中,以激活用户
GLOBAL $db;
if($db->result_first("SELECT uid FROM cdb_members WHERE uid='$uid'"))
{
uc_user_delete($uid);
}
$idstring = random(6);
$password = md5(random(10));
$sql = "insert into cdb_members (uid,username,email,groupid,timeoffset) values ($uid,'$userName','$email','10','9999')";
$sql = $db->query($sql);
return "success";
}
/**
* 同步所有用户资料
*/
public function SyncAllUsers()
{
return "success";
}
/**
* 同步修改用户密码
* 当某用户在门户站点内修改密码时,同时调用此接口方法,同步修改论坛系统的此用户的密码。
* @param $userName 用户名称
* @param $newPassword 新密码
*/
public function SyncChangePassword($userName,$newPassword)
{
return "success";
}
/**
* 同步删除用户。
* 当系统管理员在门户站点中删除某用户时,同时调用此接口方法,同步删除论坛系统的此用户。
* @param unknown_type $userName
*/
public function SyncDeleteUser($userName)
{
return "success";
}
/**
* 同步失效(或禁用)用户。
* 当系统管理员在门户站点中禁用某用户时,同时调用此接口方法,同步禁用论坛系统的此用户。
* @param unknown_type $userName
*/
public function SyncInvalidationUser($userName)
{
return "success";
}
/**
* 测试接口方法,在程序调试期对本人有很大帮助,因此不忍心删除!
* @param unknown_type $userName
*/
public function Hello($userName)
{
return "Hello ".$userName.",Welcome to PHP WebService World!";
}
}
$server = new SoapServer('UserSyncSoap.wsdl');
$server->setClass("UserSync");
$server->handle();
?>
 
3.    生成WSDL文件。
不知道怎么回事,Zend Studio 7.2不支持自动将PHP文件转换为WSDL,所以必须手动生成WSDL文件。
在usersyncservice目录中新建WSDL文件,如下图所示:
 
DSC0001.jpg

 

  
DSC0002.jpg
 
  
DSC0003.jpg
 
  
DSC0004.jpg
 
 

完整的WSDL源代码如下:
 

<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://www.example.org/UserSyncSoap/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="UserSyncSoap" targetNamespace="http://www.example.org/UserSyncSoap/">
<wsdl:types>
<xsd:schema targetNamespace="http://www.example.org/UserSyncSoap/">
<xsd:element name="SyncSaveUser">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="in" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="SyncSaveUserResponse">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="out" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="SyncAllUsers">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="in" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="SyncAllUsersResponse">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="out" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="SyncChangePassword">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="in" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="SyncChangePasswordResponse">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="out" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="SyncDeleteUser">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="in" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="SyncDeleteUserResponse">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="out" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="SyncInvalidationUser">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="in" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="SyncInvalidationUserResponse">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="out" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="Hello">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="in" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="HelloResponse">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="out" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
</wsdl:types>
<wsdl:message name="SyncSaveUserRequest">
<wsdl:part name="userName" type="xsd:string"></wsdl:part>
<wsdl:part name="password" type="xsd:string"></wsdl:part>
<wsdl:part name="email" type="xsd:string"></wsdl:part>
</wsdl:message>
<wsdl:message name="SyncSaveUserResponse">
<wsdl:part name="SyncSaveUserReturn" type="xsd:string"/>
</wsdl:message>
<wsdl:message name="SyncAllUsersRequest">
</wsdl:message>
<wsdl:message name="SyncAllUsersResponse">
<wsdl:part name="SyncAllUsersReturn" type="xsd:string"/>
</wsdl:message>
<wsdl:message name="SyncChangePasswordRequest">
<wsdl:part name="userName" type="xsd:string"></wsdl:part>
<wsdl:part name="newPassword" type="xsd:string"></wsdl:part>
</wsdl:message>
<wsdl:message name="SyncChangePasswordResponse">
<wsdl:part name="SyncChangePasswordReturn" type="xsd:string"/>
</wsdl:message>
<wsdl:message name="SyncDeleteUserRequest">
<wsdl:part name="userName" type="xsd:string"></wsdl:part>
</wsdl:message>
<wsdl:message name="SyncDeleteUserResponse">
<wsdl:part name="SyncDeleteUserReturn" type="xsd:string"></wsdl:part>
</wsdl:message>
<wsdl:message name="SyncInvalidationUserRequest">
<wsdl:part name="userName" type="xsd:string"></wsdl:part>
</wsdl:message>
<wsdl:message name="SyncInvalidationUserResponse">
<wsdl:part name="SyncInvalidationUserReturn" type="xsd:string"></wsdl:part>
</wsdl:message>
<wsdl:message name="HelloRequest">
<wsdl:part name="userName" type="xsd:string"></wsdl:part>
</wsdl:message>
<wsdl:message name="HelloResponse">
<wsdl:part name="HelloReturn" type="xsd:string"></wsdl:part>
</wsdl:message>
<wsdl:portType name="UserSyncSoap">
<wsdl:operation name="SyncSaveUser">
<wsdl:input message="tns:SyncSaveUserRequest"/>
<wsdl:output message="tns:SyncSaveUserResponse"/>
</wsdl:operation>
<wsdl:operation name="SyncAllUsers">
<wsdl:input message="tns:SyncAllUsersRequest"/>
<wsdl:output message="tns:SyncAllUsersResponse"/>
</wsdl:operation>
<wsdl:operation name="SyncChangePassword">
<wsdl:input message="tns:SyncChangePasswordRequest"/>
<wsdl:output message="tns:SyncChangePasswordResponse"/>
</wsdl:operation>
<wsdl:operation name="SyncDeleteUser">
<wsdl:input message="tns:SyncDeleteUserRequest"></wsdl:input>
<wsdl:output message="tns:SyncDeleteUserResponse"></wsdl:output>
</wsdl:operation>
<wsdl:operation name="SyncInvalidationUser">
<wsdl:input message="tns:SyncInvalidationUserRequest"></wsdl:input>
<wsdl:output message="tns:SyncInvalidationUserResponse"></wsdl:output>
</wsdl:operation>
<wsdl:operation name="Hello">
<wsdl:input message="tns:HelloRequest"></wsdl:input>
<wsdl:output message="tns:HelloResponse"></wsdl:output>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="UserSyncSoapSOAP" type="tns:UserSyncSoap">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="SyncSaveUser">
<soap:operation soapAction="http://www.example.org/UserSyncSoap/SyncSaveUser"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="SyncAllUsers">
<soap:operation soapAction="http://www.example.org/UserSyncSoap/SyncAllUsers"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="SyncChangePassword">
<soap:operation soapAction="http://www.example.org/UserSyncSoap/SyncChangePassword"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="SyncDeleteUser">
<soap:operation soapAction="http://www.example.org/UserSyncSoap/SyncDeleteUser"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="SyncInvalidationUser">
<soap:operation soapAction="http://www.example.org/UserSyncSoap/SyncInvalidationUser"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="Hello">
<soap:operation soapAction="http://www.example.org/UserSyncSoap/Hello"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="UserSyncSoap">
<wsdl:port binding="tns:UserSyncSoapSOAP" name="UserSyncSoapSOAP">
<soap:address location="http://localhost/dbgp/bbs/usersyncservice/UserSyncWebService.php"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
4.    简单测试WebService。
在浏览器地址中输入
http://localhost/dbgp/bbs/usersyncservice/UserSyncSoap.wsdl,查看是否能够XML文档。
这只是进行最最初步和简单的测试,真正的测试,你必须编写代码调用此WebService测试才行。
5.    注意事项。
Soap通讯是基于XML的,并且要求WebService客户端和服务端的文件编码必须一致是UTF-8,所以在Zend Studio工程中必须把上面涉及到的两个文件的编码格式设置为UTF-8。
 
其他说明:
本章节主要是讲述利用Zend Studio编写WebService服务端的流程,中间涉及到的WebService开发技术、WSDL编写技术、PHP语法等只是,需要阁下自行学习和查找。本人也是在编写此文章的之前四天才开始接触PHP和Zend Studio的,几乎所有的东西都是从网络上查找的,然后本人一步一步的进行测试,解决了一个又一个讨厌的小问题,最后才总算把整个解决方案搞定。
如果阁下在学习过程中遇到与此相关的问题,可以告诉我,我会尽力帮助解决的。 

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-364714-1-1.html 上篇帖子: 超级好用的php文件上传类(上传、缩略图、水印) 下篇帖子: 刚在项目中应用到了java与php通过webservice进行数据交互。觉得挺有意思,贴出来,跟大家分享。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表