Heim php教程 PHP源码 soap 应用

soap 应用

May 25, 2016 pm 05:03 PM

php SOAP例子(web servers)
2010-06-23 14:08:24| 分类: php笔记 |字号 订阅
php 先要开启 php_soap模块
一。
方法1
服务器端 文件叫 server.php

<?php
$soap = new SoapServer(null,array(&#39;uri&#39;=>"http://10.10.10.24/"));//输入本台服务器的ip地址
$soap->addFunction(&#39;say&#39;); //添加输出函数
$soap->addFunction(SOAP_FUNCTIONS_ALL); //不要忘了这个
$soap->handle(); //注意
function say($sth){
 return $sth;
}
?>
Nach dem Login kopieren

客户端 输出的是 hello world

<?php
try {
 $client = new SoapClient(null,
 array(&#39;location&#39; =>"http://10.10.10.24/server.php",&#39;uri&#39; =>"http://10.10.10.24/")
);
 echo $client->say("hello world");
} catch (SoapFault $fault){
 echo"Error:",$fault->faultcode,", string:",$fault->faultstring;
}
?>
Nach dem Login kopieren

二。
服务器端文件server.php:

<?php
$classmap = array();
//注意和实例一 的不同
$soap = new SoapServer(null,array(&#39;uri&#39;=>"http://10.10.10.24/","classmap"=> $classmap));
$soap->setClass(&#39;Myclass&#39;);
$soap->handle();
class Myclass {
 function say($someword){
 return $someword;
}
}
?>
Nach dem Login kopieren

客 户端 输出的是 xyz world

<?
try {
 $client = new SoapClient(null,
 array(&#39;location&#39; =>"http://10.10.10.24/server.php",&#39;uri&#39; =>"http://10.10.10.24/")
);
var_dump($client);
 echo $client->say("xyz world");
} catch (SoapFault $fault){
 echo"Error:",$fault->faultcode,", string:",$fault->faultstring;
}
Nach dem Login kopieren
<?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(&#39;location&#39;=>"http://localhost/webserver/soapserver.php",&#39;uri&#39;=>&#39;http://127.0.0.1/&#39;));
 $soap = new SoapClient(null,array(&#39;location&#39;=>"http://localhost/webserver/soapserver.php",&#39;uri&#39;=>&#39;test&#39;));
//两种调用方式,直接调用方法,和用__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();
}





?>
Nach dem Login kopieren
<?php
Class PersonInfo
{
/**
 * 返回姓名
 * @return string 
*
*/
 public function getName(){
 return"My Name is Chance";
}
}


 //wsdl方式提供web service,如果生成了wsdl文件则可直接传递到//SoapServer的构造函数中
 //$s = new SoapServer(&#39;PersonInfo.wsdl&#39;);

 //doesn&#39;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();




?>
Nach dem Login kopieren
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn

Heiße KI -Werkzeuge

Undresser.AI Undress

Undresser.AI Undress

KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover

AI Clothes Remover

Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool

Undress AI Tool

Ausziehbilder kostenlos

Clothoff.io

Clothoff.io

KI-Kleiderentferner

AI Hentai Generator

AI Hentai Generator

Erstellen Sie kostenlos Ai Hentai.

Heißer Artikel

R.E.P.O. Energiekristalle erklärten und was sie tun (gelber Kristall)
2 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌
Repo: Wie man Teamkollegen wiederbelebt
4 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Abenteuer: Wie man riesige Samen bekommt
4 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌

Heiße Werkzeuge

Notepad++7.3.1

Notepad++7.3.1

Einfach zu bedienender und kostenloser Code-Editor

SublimeText3 chinesische Version

SublimeText3 chinesische Version

Chinesische Version, sehr einfach zu bedienen

Senden Sie Studio 13.0.1

Senden Sie Studio 13.0.1

Leistungsstarke integrierte PHP-Entwicklungsumgebung

Dreamweaver CS6

Dreamweaver CS6

Visuelle Webentwicklungstools

SublimeText3 Mac-Version

SublimeText3 Mac-Version

Codebearbeitungssoftware auf Gottesniveau (SublimeText3)