<?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獲取元素內容,完成數據的讀取與解析。