<?php class MyWebService { public function SayHello($name) { return "Hello, " . $name . "!"; } } $options = array('uri' => 'http://localhost/'); $server = new SoapServer(null, $options); $server->setObject(new MyWebService()); $server->handle(); ?>
示例中,MyWebService类包含SayHello方法,SOAP服务器将此类作为服务对象启动,实现对外接口的暴露。
<?php $options = array('uri' => 'http://localhost/'); $client = new SoapClient(null, $options); $result = $client->SayHello('John'); echo $result; ?>
此代码通过SoapClient连接指定URI,调用SayHello方法并输出结果,实现了服务调用的完整流程。
<?php $xml = ' <book> <title>PHP and XML</title> <author>John Smith</author> </book>'; $dom = new DOMDocument(); $dom->loadXML($xml); $title = $dom->getElementsByTagName('title')->item(0)->nodeValue; $author = $dom->getElementsByTagName('author')->item(0)->nodeValue; echo "Title: " . $title . "<br>"; echo "Author: " . $author . "<br>"; ?>
示例中通过DOMDocument加载XML字符串,调用getElementsByTagName获取元素内容,完成数据的读取与解析。