php soap例子(web servers)
2010-06-23 14:08:24| 分类: php笔记 |字号 订阅
php 先要开启 php_soap模块
一。
方法1
服务器端 文件叫 server.php
<?php
$soap = new soapserver(null,array('uri'=>"http://10.10.10.24/"));//输入本台服务器的ip地址
$soap->addfunction('say'); //添加输出函数
$soap->addfunction(soap_functions_all); //不要忘了这个
$soap->handle(); //注意
function say($sth){
return $sth;
}
?>
客户端 输出的是 hello world
<?php
try {
$client = new soapclient(null,
array('location' =>"http://10.10.10.24/server.php",'uri' =>"http://10.10.10.24/")
);
echo $client->say("hello world");
} catch (soapfault $fault){
echo"error:",$fault->faultcode,", string:",$fault->faultstring;
}
?>
二。
服务器端文件server.php:
<?php
$classmap = array();
//注意和实例一 的不同
$soap = new soapserver(null,array('uri'=>"http://10.10.10.24/","classmap"=> $classmap));
$soap->setclass('myclass');
$soap->handle();
class myclass {
function say($someword){
return $someword;
}
}
?>
客 户端 输出的是 xyz world
<?
try {
$client = new soapclient(null,
array('location' =>"http://10.10.10.24/server.php",'uri' =>"http://10.10.10.24/")
);
var_dump($client);
echo $client->say("xyz world");
} catch (soapfault $fault){
echo"error:",$fault->faultcode,", string:",$fault->faultstring;
}
<?php
try{
//wsdl方式调用web service
//wsdl方式中由于wsdl文件写定了,如果发生添加删除函数等操作改动,不会反应到wsdl,相对non-wsdl方式
//来说不够灵活
//$soap = new soapclient("http://localhost/test/myservice/personinfo.wsdl");
//non-wsdl方式调用web service
//在non-wsdl方式中option location系必须提供的,而服务端的location是选择性的,可以不提供
// $soap = new soapclient(null,array('location'=>"http://localhost/webserver/soapserver.php",'uri'=>'http://127.0.0.1/'));
$soap = new soapclient(null,array('location'=>"http://localhost/webserver/soapserver.php",'uri'=>'test'));
//两种调用方式,直接调用方法,和用__soapcall简接调用
$result1 = $soap->getname();
$result2 = $soap->__soapcall("getname",array());
echo $result1."<br/>";
echo $result2;
}catch(soapfault $e){
echo $e->getmessage();
}catch(exception $e){
echo $e->getmessage();
}
?>
<?php
class personinfo
{
/**
* 返回姓名
* @return string
*
*/
public function getname(){
return"my name is chance";
}
}
//wsdl方式提供web service,如果生成了wsdl文件则可直接传递到//soapserver的构造函数中
//$s = new soapserver('personinfo.wsdl');
//doesn't work 只有location不能提供web service
//output:looks like we got no xml document
//$s = new soapserver(null,array(""=>"http://localhost/test/myservice/server.php"));
//下面两种方式均可以工作,只要指定了相应的uri
//$s = new soapserver(null,array("uri"=>"http://127.0.0.1/"));
$s = new soapserver(null,array("uri"=>"test"));
$s -> setclass("personinfo");
$s -> handle();
?>