標籤:blog http io ar os 使用 sp 檔案 資料
nusoap是php寫的一個功能檔案,:http://pan.baidu.com/s/1i3mUQJr
一、不使用wsdl
服務端 server.php
<?php //包函nusoap.php require_once(‘./lib/nusoap.php‘); //建立服務端 $server = new soap_server; //定義用戶端調用方法 $server->register(‘hello‘); //調用方法以及參數 function hello($name) { return ‘Hello, ‘ . $name; } $HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : ‘‘; $server->service($HTTP_RAW_POST_DATA); ?>
用戶端 client.php
<?php //包函nusoap.php require_once(‘./lib/nusoap.php‘); //建立一個soap用戶端,調用服務端提供的wsdl $client = new soapclient(‘http://localhost/test/server.php‘); //查看一下是不是報錯 $err = $client->getError(); if ($err) { //顯示錯誤 echo ‘<h2>Constructor error</h2><pre>‘ . $err . ‘</pre>‘; } //調用服務端的方法 $result = $client->call(‘hello‘, array(‘person‘ => "this is a test")); echo ‘<h2>Result</h2><pre>‘; print_r($result); echo ‘</pre>‘; ?>
二、使用wsld
服務端 serve.php
<?php //包函nusoap.php require_once(‘./lib/nusoap.php‘); //建立一個soap服務 $server = new soap_server(); $server->soap_defencoding = ‘UTF-8‘;//$ser->decode_utf8 = false;$server->xml_encoding = ‘UTF-8‘;//初始化支援wsdl $server->configureWSDL(‘hellowsdl2‘, ‘hellowsdl2‘); //定義資料結構來接收資料 $server->wsdl->addComplexType( ‘Person‘, ‘complexType‘, ‘struct‘, ‘all‘, ‘‘, array( ‘firstname‘ => array(‘name‘ => ‘firstname‘, ‘type‘ => ‘xsd:string‘),//後面的type定義資料的類型,這個是string ‘age‘ => array(‘name‘ => ‘age‘, ‘type‘ => ‘xsd:int‘),//後面的type定義資料的類型,這個是int ‘gender‘ => array(‘name‘ => ‘gender‘, ‘type‘ => ‘xsd:string‘)//後面的type定義資料的類型,這個是string ) ); $server->wsdl->addComplexType( ‘SweepstakesGreeting‘, ‘complexType‘, ‘struct‘, ‘all‘, ‘‘, array( ‘greeting‘ => array(‘name‘ => ‘greeting‘, ‘type‘ => ‘xsd:string‘), ‘winner‘ => array(‘name‘ => ‘winner‘, ‘type‘ => ‘xsd:string‘) ) ); //伺服器定義的soap調用方法 $server->register(‘hello‘, // 方法名字hello,方法就在下面 array(‘person‘ => ‘tns:Person‘), // 用戶端傳來的變數 array(‘return‘ => ‘tns:SweepstakesGreeting‘), //返回參數 ‘urn:hellowsdl2‘, // soap名 ‘urn:hellowsdl2#hello‘, // soap的方法名 ‘rpc‘, // 使用的方式 ‘encoded‘, // 編碼 ‘test‘ // 存檔 ); //定義上面註冊過的函數hello function hello($person) { $greeting = ‘Hello, ‘ . $person[‘firstname‘] .‘. It is nice to meet a ‘ . $person[‘age‘] . ‘ year old ‘ . $person[‘gender‘] . ‘.‘; $winner = ‘Scott‘; //要返回的資料 return array( ‘greeting‘ => $greeting, ‘winner‘ => $winner ); } // 請求時(試圖)調用服務 $HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : ‘‘; $server->service($HTTP_RAW_POST_DATA); ?>
服務端 client.php
<?php //包函nusoap.php require_once(‘./lib/nusoap.php‘); //建立一個soap用戶端,調用服務端提供的wsdl //$client = new soapclient(‘http://localhost/test/hellowsdl2.php?wsdl‘, true); $client = new soapclient(‘http://localhost/test/helloworld2.php‘); //查看一下是不是報錯 $err = $client->getError(); if ($err) { //顯示錯誤 echo ‘<h2>Constructor error</h2><pre>‘ . $err . ‘</pre>‘; } //要向服務端要傳的參數 $person = array(‘firstname‘ => ‘Willi‘, ‘age‘ => 22, ‘gender‘ => ‘male‘); //調用服務端的方法 $result = $client->call(‘hello‘, array(‘person‘ => $person)); //錯誤審核 if ($client->fault) { echo ‘<h2>Fault</h2><pre>‘; print_r($result); echo ‘</pre>‘; } else { $err = $client->getError(); if ($err) { echo ‘<h2>Error</h2><pre>‘ . $err . ‘</pre>‘; } else { echo ‘<h2>Result</h2><pre>‘; print_r($result); echo ‘</pre>‘; } } //顯示請求資訊 echo ‘<h2>Request</h2>‘; echo ‘<pre>‘ . htmlspecialchars($client->request, ENT_QUOTES) . ‘</pre>‘; //顯示返回資訊 echo ‘<h2>Response</h2>‘; echo ‘<pre>‘ . htmlspecialchars($client->response, ENT_QUOTES) . ‘</pre>‘; //顯示調試資訊 echo ‘<h2>Debug</h2>‘; echo ‘<pre>‘ . htmlspecialchars($client->debug_str, ENT_QUOTES) . ‘</pre>‘; ?>
php soap實現WebService介面