您的位置:首页 > 编程语言 > PHP开发

使用zeng studio 创建的php调用webservice soap简单实例

2014-09-22 16:18 1006 查看
这两天一直在研究webservice soap是个什么东西,究竟怎么创建webservice,又如何用php调用webservice的借口,经过奋战,终于总算弄懂一点的。然后我做了一个webservice应用,然后使用php调用其借口,其功能只是简单的用户注册。
webservice究竟是个什么东东,自己百度吧。首先我来讲讲创建这个webservice的难点,难点就在于如何写出一个正确可用的WSDL文件,只要有了这个WSDL文件,那么其他的工作就一点都不新鲜了,就像你平时编程一样。
当你看看WSDL文件的时候,可能你头都大了,一大串代码,根本就不知道是什么。后来百度了一下,才知道这个东西只有牛人才可以写出来,我们这些一般的人物是可以用工具生成的,推荐zeng studio。我下的是7.1版的,发现和网上的教程根本不一样(他们可能使用的是旧版本),搞了半天都不知道在哪里生成WSDL文件,网上教程上说的大多数是使用export或者是tool,但你会发现较新版本的这两个都不管用。下面我就来演示一下如何生成这个WSDL文件。
首先,肯定是要创建PHP工程的。然后我们来创建webservice服务器端的文件,新建一个Class,代码如下:


<?php

class Action
{
private $username;
private $password;
private $email;

public function Register($username,$password,$email) {
$this->username=$username;
$this->password=$password;
$this->email=$email;

$conn=mysql_connect("localhost","root","");
if(!$conn) {
die("连接数据库失败".mysql.error());
}
mysql_select_db("android") or die("选择数据库有误".mysql_error());

$db_username=mysql_query("select * from users where username='$username'");
$num_username=mysql_num_rows($db_username);

$db_email=mysql_query("select * from users where email='$email'");
$num_email=mysql_num_rows($db_email);

if($num_username==0 && $num_email==0) {
mysql_query("insert into users (username,password,email) values ('$username','$password','$email')");
return true;
} else {
if($num_username!=0) {
echo "该用户已注册,请更改用户名重新注册";
return false;
}
if($num_email!=0) {
echo "该邮箱已注册,请更改邮箱重新注册";
return false;
}
}
}

}


接着是server文件:


<?php
ini_set("soap.wsdl_cache_enabled", "0");
include "Action.php";
$Server=new SoapServer('soap.wsdl',array('soap_version' => SOAP_1_2));   //创建SoapServer对象
$Server->setClass("Action");
$Server->handle();


客户端测试文件


<?php
ini_set("soap.wsdl_cache_enabled", "0");
$client=new SoapClient("soap.wsdl");
$username="lvye";
$password="777777";
$email="lvye@qq.com";
$result=$client->Register("$username","$password","$email");
if($result) {
echo "注册成功!用户名为:<font color='red'>{$username}</font>,密码为:<font color='red'>{$password}</font>,邮箱为:<font color='red'>{$email}</font>";
} else echo "注册失败!";


下面我们就来生成WSDL文件了,new—>othor—>web service—>WSDL,这样就可以新建一个WSDL文件了,如图。







然后我们就来修改WSDL文件,zeng studio为我们提供了可视化的操作,当然如果你牛的话,你当然是可以改文件代码的,其实也就几个东西,<portType><message><types><binding>弄懂了的话也不会太难。




本例中修改如下图所示:



做完这一步,这个WSDL文件就基本可用了,但又两个问题需要注意:

做到这一步,有可能会测试失败,可能会因为没有进行binding,这个东西有时是需要手动来完成的,在binding上右键选择Generate Binding Content就行了。
第二个要注意的是php的WSDL缓存,在做测试时,一般要将WSDL缓存关闭,否则你使用的有可能是原来的WSDL文件,而不是更新过的。关闭缓存有两种方法,第一种就是直接到php.ini中设置soap.wsdl_cache_enabled = 0;第二种就是在php文件中添加一条语句,ini_set("soap.wsdl_cache_enabled", "0");

做到这里,你就可以放心地测试,调用你的server程序了。顺便贴上WSDL的代码:


<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://localhost/soap/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="soap" targetNamespace="http://localhost/soap/">
<wsdl:types>
<xsd:schema targetNamespace="http://localhost/soap/">
<xsd:element name="NewOperation">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="in" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="NewOperationResponse">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="out" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
</wsdl:types>
<wsdl:message name="RegisterRequest">

<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="RegisterResponse">

<wsdl:part name="RegisterResponce" type="xsd:boolean"></wsdl:part>
</wsdl:message>
<wsdl:portType name="soap">
<wsdl:operation name="Register">
<wsdl:input message="tns:RegisterRequest"/>
<wsdl:output message="tns:RegisterResponse"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="soapSOAP" type="tns:soap">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="Register">
<soap:operation soapAction="http://localhost/soap/Register"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="soap">
<wsdl:port binding="tns:soapSOAP" name="soapSOAP">
<soap:address location="http://localhost/soap/server.php"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: