茫茫網海中的冷日
         
茫茫網海中的冷日
發生過的事,不可能遺忘,只是想不起來而已!
 恭喜您是本站第 1729062 位訪客!  登入  | 註冊
主選單

Google 自訂搜尋

Goole 廣告

隨機相片
IMG_60D_00317.jpg

授權條款

使用者登入
使用者名稱:

密碼:


忘了密碼?

現在就註冊!

PHP特區 : [轉貼]PHP and Web Services 學習筆記

發表者 討論內容
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[轉貼]使用nusoap建立web service-範例




首先 分別撰寫cliet與server端程式,放在nusoap資料夾下



server端部分程式碼撰寫原則:



  1. 撰寫欲被client調用的函式connectandsave($content)

  2. 引入nusoap require_once("lib/nusoap.php");

  3. 建立soap_server實體 new soap_server()

  4. 配置WSDL文件敘述 configureWSDL

  5. 為服務註冊register方法函式(connectandsave)

  6. 使用service方法執行方法並回傳值 $server->service($POST_DATA);



server端-WSservertest.php:



<?php

//定義service處理函式

function connectandsave($content)

{

$xx = main::toStore($content);



return "get \r\n" . main::$accuracy2 . "\r\n" ;

//return "get \r\n" . $content . "\r\n" ."dd";



}

date_default_timezone_set("Asia/Taipei");

require_once("lib/nusoap.php");





$server = new soap_server();// 初始化一個service物件實體

$server->soap_defencoding='UTF-8';

$server->decode_utf8=false;

$server->configureWSDL("SimpleService");// 配置 WSDL 描述,叫用時以此為類別名稱

$server->wsdl->schemaTargetNamespace = $namespace; // set namespace

// register service's WebMethod,註冊過的程序才能被訪問到

// $server->register('answerHello',array('name' => 'xsd:string'),array('return' => 'xsd:string'));

$server->register(

// method name:

'connectandsave',

// parameter list:

array('name'=>'xsd:string'),

// return value(s):

array('return'=>'xsd:string'),

// namespace:

$namespace,

// soapaction: (use default)

false/*,

// style: rpc or document

'document',

// use: encoded or literal

'encoded',

// description: documentation for the method

'Anser Hello to World '*/);





//把client端 post 過來的資料,傳給server物件的 service 方法處理。

//service 方法處理的資料會借調用相應的函式回傳給叫用端

$POST_DATA = isset($GLOBALS['HTTP_RAW_POST_DATA'])

? $GLOBALS['HTTP_RAW_POST_DATA'] : '';



$server->service($POST_DATA);

exit();

?>



其中main是自定類別,裡面就是自己寫的一些程序



接下來示範如何調用剛剛寫好的函式connectandsave

client端部分程式碼撰寫原則:



  1. 引入nusoap require_once("lib/nusoap.php");

  2. 建立soapclient()實體 new soapclient()

  3. 將要傳送的資料陣列化(array)

  4. 使用call方法叫用欲呼叫的方法call(函式名)

  5. 印出call完後的值;





client端-WSclienttest.php :



<?php

require_once("lib/nusoap.php");

//初始化 client 物件,是class soapclient 的一個實體

//把服務程序的 URL 地址傳遞给soapclient類別的構造函式

$client = new soapclient('http://xxxx/nusoap/WSservertest.php'); //後面利用client物件的 call 方法調用 WEB 服务的程序'GPSconnectandsave'



$myname = 'clark' ;

// $Content = '25.07433874,121.52545941,2009-05-23,17:53:56,1.2,5,2.3,2.6,8.3,xxx' ; //中原國小門口

$Content = '24.956846,121.242451,2009-05-23,17:53:56,1.2,5,2.3,2.6,8.3,xxx' ; //中原大草皮



$parameters=array('24.956846,121.242451,2000-01-01,17:53:56,1.2,5,2.3,2.6,8.3,xxx');

$str=$client->call('connectandsave',$parameters); //call('函式名',匹配的參數) 函式名為在WSservertest.php中所註冊的函式



//$str=$client->call('hello'); //客户端对象的 getError() 方法可以用来检查调用过程是否出现错误。

//如果没有错误, getError() 方法返回 false ;如果有错误, getError()方法返回错误信息。

if (!$err=$client->getError()) {

echo " return:",htmlentities($str,ENT_QUOTES);
//the optional second quote_style parameter lets you define what will be done with
'single' and "double" quotes. It takes on one of three constants with the default
being ENT_COMPAT: ,ENT_QUOTES Will convert both double and single quotes.

} else {

echo " error :",htmlentities($err,ENT_QUOTES);

}



//觀察request 和 response響應

echo '<p/>';

echo 'Request:';

echo '<pre>',htmlspecialchars($client->request,ENT_QUOTES),'</pre>';

echo 'Response:';

echo '<pre>',htmlspecialchars($client->response,ENT_QUOTES ),'</pre>';



?>








原文出處:
研究生了沒: 使用nusoap建立web service-範例
前一個主題 | 下一個主題 | | | |

討論串




Powered by XOOPS 2.0 © 2001-2008 The XOOPS Project|