WebService-php- 2(17),webservice-php-17
wsdl執行個體
'1.0' encoding ='UTF-8' ?><definitionstargetNamespace='http://localhost/00/'xmlns:tns='http://localhost/00/'xmlns:soap='http://schemas.xmlsoap.org/wsdl/soap/'xmlns:xsd='http://www.w3.org/2001/XMLSchema'xmlns:soapenc='http://schemas.xmlsoap.org/soap/encoding/'xmlns:wsdl='http://schemas.xmlsoap.org/wsdl/'xmlns='http://schemas.xmlsoap.org/wsdl/'>"http://www.w3.org/2001/XMLSchema"targetNamespace="http://localhost/00/">'testRequest'>"term" type="xsd:string"/>'testResponse'>"value" type="xsd:string"/>'oplist'>'test'>'tns:testRequest'/>'tns:testResponse'/>'cartSoap' type='tns:oplist'>'rpc'transport='http://schemas.xmlsoap.org/soap/http'/>'test'>'http://www.cwtservice.cn/newOperation/'/>'encoded' namespace='urn:xmethods-delayed-quotes'encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/>'encoded' namespace='urn:xmethods-delayed-quotes'encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/>'shopWS'>'cartSoap' binding='tns:cartSoap'>'http://localhost/00/wss.php'/>
Server端樣本:
function test($x) {return $x;}$ss = new SoapServer('http://localhost/00/wsdl.xml');$ss->addFunction('test');$ss->handle();
Client調用:
$soap = new soapClient('http://localhost/00/wsdl.xml',array('trace'=>true));var_dump($soap->test('10086'));
傳遞和返回數組參數
如果傳遞或返回的參數為數組,可以在message標籤中做說明.
'testRequest'>"term" type="xsd:ArrayOfString"/>'testResponse'>"value" type="xsd:ArrayOfString"/>
XML-RPC調用
XML-RPC可以理解為簡化版的soap,對資料的封裝相對簡潔.php.ini中,要開啟extension=php_xmlrpc.dll
/*求和函數注意,rpc伺服器在調用函數時,傳的參數是這樣的:array(0=>'函數名' , 1=>array(實參1,實參2,...實參N) , 2=>NULL)*/function hello() {return 'hello';}function sum($method , $args , $extra) {return array_sum($args);}// 建立RPC Server$server = xmlrpc_server_create ();xmlrpc_server_register_method ($server , 'hello' , 'hello');xmlrpc_server_register_method ($server , 'sum' , 'sum');// 收取請求$request = $HTTP_RAW_POST_DATA;//執行調用用戶端的XML請求後擷取執行結果$xmlrpc_response = xmlrpc_server_call_method($server, $request , null);//把函數處理後的結果XML進行輸出header('Content-Type: text/xml');echo $xmlrpc_response;//銷毀XML-RPC伺服器端資源xmlrpc_server_destroy($server);
用戶端:
class rpcclient {protected $url;public function __construct($url='' ) {$this->url = $url;}protected function query($request) {$context = stream_context_create(array('http' => array('method' => "POST",'header' => "Content-Type: text/xml",'content' => $request)));$xml = file_get_contents($this->url, false, $context);return xmlrpc_decode($xml);}public function __call($method , $args) {$request = xmlrpc_encode_request($method , $args);return $this->query($request);}}$rpc = new rpcclient('http://localhost/00/rpcs.php');var_dump($rpc->hello());var_dump($rpc->sum(4,5,6));
WebService與json Api的區別
WebService json API
資料封裝 XML json
複雜度 高 低
底層協議 不限 HTTP
資料類型 可嚴格定義 不可嚴格定義
自說明 性自說明 需額外API文檔
http://www.bkjia.com/PHPjc/993278.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/993278.htmlTechArticleWebService-php- 2(17),webservice-php-17 wsdl執行個體 ?xml version = ' 1.0 ' encoding = ' UTF-8 ' ? definitionstargetNamespace = ' http://localhost/00/ ' xmlns:tns = ' http://lo...