您的位置:首页 > 其它

4. 简单的webservices 例子

2016-05-22 00:22 197 查看
1. php 实现 soap

php有两个扩展可以实现web service,一个是NuSoap,一个是php 官方的soap扩展,由于soap是官方的,
所以我们这里以soap来实现web service.
由于默认是没有打开soap扩展的,所以自己先看一下soap扩展有没有打开。

在soap编写web service的过程中主要用到了SoapClient,SoapServer,SoapFault三个类。


SoapClient类:

这个类用来使用Web services。SoapClient类可以作为给定Web services的客户端。

它有两种操作形式:

WSDL 模式

Non-WSDL 模式

在WSDL模式中,构造器可以使用WSDL文件名作为参数,并从WSDL中提取服务所使用的信息。

non-WSDL模式中使用参数来传递要使用的信息。

SoapServer类:

这个类可以用来提供Web services。与SoapClient类似,SoapServer也有两种操作模式:
WSDL模式和non-WSDL模式。这两种模式的意义跟 SoapClient的
两种模式一样。在WSDL模式中,服务实现了WSDL提供的接口;
在non-WSDL模式中,参数被用来管理服务的行为。

在SoapServer类的众多方法中,有三个方法比较重要。它们是SoapServer::setClass(),SoapServer::addFunction()和SoapServer::handle()。


PersonInfo.php

<?php

class PersonInfo
{
public function getName($a)
{
return 'My name is wjh ::' . $a;
}
}


client.php

<?php

try {

// 1. wsdl 方式
//wsdl方式调用web service
//wsdl方式中由于wsdl文件写定了,如果发生添加删除函数等操作改动,不会反应到wsdl,相对non-wsdl方式,来说不够灵活
//$soap = new SoapClient("http://localhost/Test/MyService/PersonInfo.wsdl");

//2. no-wsdl 方式
//non-wsdl方式调用web service
//在non-wsdl方式中option location系必须提供的,而服务端的location是选择性的,可以不提供
$soap = new SoapClient(null,array('location'=>'http://studio.local/test/server.php','uri'=>'server.php'));
//直接调用,和间接调用(__soapCall)
$result1 = $soap->__soapCall('getName',array('a'=>'hahahah'));
$result2 = $soap->getName('wwwwwwwww');
echo $result1 . '<br/>';
echo $result2;
} catch (SoapFault $e) {
echo $e->getMessage();
}


server.php

<?php

require_once ('PersonInfo.php');

//1. wsdl 方式
//$soap = new SoapServer('PersonInfo.wsdl');

//2. no-wsdl 方式
//non-wsdl方式中服务端的location是选择性的,可以不提供,客户端 必须提供,即一下两种方式都可以
$soap = new SoapServer(null,array("uri"=>"server.php"));
//$soap = new SoapServer(null,array('location'=>'http://studio.local/test/server.php','uri'=>'server.php'));
$soap->setClass('PersonInfo');
$soap->handle();


服务器端代码:

mainservice.php

<?php

//其中uri相当于java中的命名空间,可以是任何不和别人重合的字符串,第一个参数是wsdl,如果不使用wsdl则使用null,第三个参数表示soap的版本号,目前就两个版本号

$wbServer= new SoapServer(null, array('uri'=>'textphpwebservice','soap_version'=>SOAP_1_2));
$wbServer->addFunction('sayhello');
$wbServer->addFunction('mymin');
$wbServer->handle();

function sayhello($name){
return "hello ".$name."you are great!";
}

function mymin($a,$b){
return $a>$b?$b:$a;
}
exit();
?>


客户端代码:

client.php

<?php

try{
//第一个参数是wsdl文件,不使用的话用null,location表示webservice服务文件的地址,uri要用要调用的webserivce中的uri一致
$client=new SoapClient(null,array('location'=>'http://studio.local/mainservice.php','uri'=>'textphpwebservice'));
echo $client->sayhello("lijh")."
";
echo $client->mymin(100,50)."ccccc";
}catch(SoapFault $fault){
echo "fail";
}

exit();


有 wsdl 情况:

client.php

<?php
ini_set("soap.wsdl_cache_enabled", "0");
header('content-type:text/html;charset=utf-8');
$client=new SoapClient("soap.wsdl");
$username="lvye";
$password="777777";
$email="lvye@qq.com";
$result=$client->Register("$username","$password","$email");
if($result) {
echo 'register ok';
} else {
echo 'register no';
}


server.php

<?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();


Action.php

<?php
class Action
{
private $username;
private $password;
private $email;

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

if ($this->username == 'aaa' and $this->email == 'bbb' and $this->password =='ccc') {
echo 'ok';
return true;
} else {
echo 'no';
return false;
}
}
}


soap.wsdl

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://studio.local/soap/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="soap" targetNamespace="http://studio.local/soap/">
<wsdl:types>
<xsd:schema targetNamespace="http://studio.local/soap/">
<xsd:element name="Register">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="in" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="RegisterResponse">
<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="RegisterResponse" 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://studio.local/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://studio.local/soap/server.php"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>


http://www.kushu.net/678.html

http://www.cnblogs.com/helloxyz/archive/2011/11/05/2237328.html%20%20%20RegisterResponce

/article/5199319.html

/article/1492400.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: