现在的位置: 首页 > 综合 > 正文

php编写及调用webservice

2018年03月31日 ⁄ 综合 ⁄ 共 4465字 ⁄ 字号 评论关闭

本文虽为译文,但是只是汲取了其它文章的部分,不过大部分是小编增添补充,为了让读者更清楚。如果自己调试不通过的话可能是使用的工具和soap版本造成的。可以到小编的资源中下载,资源名:php编写及调用webservice。

这两天一直在研究webservice soap是个什么东西,究竟怎么创建webservice,又如何用php调用webservice的接口,经过奋战,终于总算弄懂一点的。然后我做了一个webservice应用,然后使用php调用其接口,其功能只是简单的用户注册。

      WSDL文件可以用工具生成的,推荐zeng studio。我下的是10.6.1版的,搞了半天都不知道在哪里生成WSDL文件,不过现在知道了,很简单,下面我就来演示一下如何生成这个WSDL文件。

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

截图05 截图06 截图07

点击Finish就生成了。

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

截图08

 

本例中注册修改如下图所示:

 

登录的如下:

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

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

下面我们来填写应用程序的灵魂部分,也就是代码编写。

       首先,肯定是要创建PHP工程的。然后我们来创建webservice服务器端的文件,新建一个Class,代码如下:ServerClass.php

<?php
class ServerClass {
	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","root");
		
		if(!$conn) {
			die("连接数据库失败".mysql_error());
		}
		mysql_select_db("manage_system") or die("选择数据库有误".mysql_error());
	
		$db_username=mysql_query("select * from manage_member where username='$username'");
		$num_username=mysql_num_rows($db_username);
	
		$db_email=mysql_query("select * from manage_member where email='$email'");
		$num_email=mysql_num_rows($db_email);
	
		if($num_username==0 && $num_email==0) {
			mysql_query("insert into manage_member (username,email,password) values ('$username','$email','$password')");
			return true;
		} else {
			if($num_username!=0) {
				echo "该用户已注册,请更改用户名重新注册";
				return false;
			}
			if($num_email!=0) {
				echo "该邮箱已注册,请更改邮箱重新注册";
				return false;
			}
		}
	}
}


?>



接着是server文件:server.php

<?php
include ("../service/ServerClass.php");
$Service = new SoapServer('http://localhost:86/wsdl/mysoap.wsdl');
$Service->setClass('ServerClass');
$Service->handle();
?>

 

客户端测试文件client.php

<?php
ini_set("soap.wsdl_cache_enabled", "0");


$username="zhangsan";
$password="789456";
$email="zhagnsan@qq.com";


$client=new SoapClient("http://localhost:86/wsdl/mysoap.wsdl");
//$param =array('username' =>$username,'password' =>$password,'email' =>$email);
$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 "注册失败!";
}
?>



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

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://localhost:86/wsdl/mysoap/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="mysoap" targetNamespace="http://localhost:86/wsdl/mysoap/">
  <wsdl:types>
    <xsd:schema targetNamespace="http://localhost:86/wsdl/mysoap/">
      <xsd:element name="register">
        <xsd:complexType>
          <xsd:sequence>
          	<xsd:element name="username" type="xsd:string" />
          	<xsd:element name="password" type="xsd:string"></xsd:element>
          	<xsd:element name="email" type="xsd:string"></xsd:element>
          </xsd:sequence>
        </xsd:complexType>
      </xsd:element>
      <xsd:element name="registerResponse">
        <xsd:complexType>
          <xsd:sequence>
            <xsd:element name="registerResponse" type="xsd:boolean"/>
          </xsd:sequence>
        </xsd:complexType>
      </xsd:element>
    </xsd:schema>
  </wsdl:types>
  <wsdl:message name="registerRequest">
    <wsdl:part element="tns:register" name="parameters"/>
  </wsdl:message>
  <wsdl:message name="registerResponse">
    <wsdl:part element="tns:registerResponse" name="parameters"/>
  </wsdl:message>
  <wsdl:portType name="mysoap">
    <wsdl:operation name="register">
      <wsdl:input message="tns:registerRequest"/>
      <wsdl:output message="tns:registerResponse"/>
    </wsdl:operation>
  </wsdl:portType>
  <wsdl:binding name="mysoapSOAP" type="tns:mysoap">
  	<soap:binding style="document"
  		transport="http://schemas.xmlsoap.org/soap/http" />
  	<wsdl:operation name="register">
  		<soap:operation
  			soapAction="http://localhost:86/wsdl/mysoap/register" />
  		<wsdl:input>
  			<soap:body use="literal" />
  		</wsdl:input>
  		<wsdl:output>
  			<soap:body use="literal" />
  		</wsdl:output>
  	</wsdl:operation>
  </wsdl:binding>
  <wsdl:service name="mysoap">
    <wsdl:port binding="tns:mysoapSOAP" name="mysoapSOAP">
      <soap:address location="http://localhost:86/service/server.php"/>
    </wsdl:port>
  </wsdl:service>
</wsdl:definitions>

抱歉!评论已关闭.