nusoap 的使用

今天不知道爲什麼想起了soap 這個東西,然後就弄了下,在php上使用的是nusoap。 一些基本的使用,高深的麻煩您自己看手冊去
這個軟件的下載在 http://dietrich.ganx4.com/nusoap  下面我會加上附件。

說說這個東西,我曾經以爲能用這個東西完成跨域名登陸,但是今天發現好象實現不了,
到底在什麼地方可以應用上,也很茫然,希望知道在那裏應用的朋友指點下,謝謝了!

關於soap的具體的原理,什麼的我就不多說,想知道的你網上自己找下,基本上就是http協議與xml的應用。
先說下個這個東西的實現原理,基本上就向服務器發送請求實現一個服務器上的函數運行,然後得到返回值。
值,我感覺應該是string或者數組,或者int值,應該不能用對象的。

先看一個基本的使用很簡單的
這是 soap_server.php 文件  這個在網絡上能找的到

<?php
include_once("lib/nusoap.php"); //插入文件
$server=new soap_server();     //生成對象
$server->configureWSDL("test_wsdl""");
$server->wsdl->schemaTargetNamespace="urn:test_wsdl"
$server->register("hello"//方法名 
array( 
"name"=>"xsd:string"
"call"=>"xsd:string"
"tele"=>"xsd:string"
),
//輸入參數 
array( 
"return"=>"xsd:string"
),
//輸出參數 
"urn:test_wsdl",//名字空間
"urn:test_wsdl#hello",//名字空間#要操作的函數名 
"rpc",//style 
"encoded",//use
"This is test."//說明 
); 
//test方法實現 
function hello($name,$call,$tele) { 
if(
$name==""){ 
return new 
soap_fault("Client","","Must supply a valid name."); 

return 
"Hello, " $name." ".$call." ".$tele

//Use the request to (try to) invoke the service 
$HTTP_RAW_POST_DATA=isset($HTTP_RAW_POST_DATA)?$HTTP_RAW_POST_DATA:""
$server->service($HTTP_RAW_POST_DATA);
?>
在你寫完了以後,在瀏覽器裏輸入url,應該會看到一個頁面,根據那個頁面分析下,會有很大的收穫,
client端要根據上面的使用
這個是soap _client.php

<?php
include_once("lib/nusoap.php");  //插入文件
//設置參數數組
$para=array( 
"name"=>"sera1ph.Liu"
"call"=>"123456"
"phone"=>"12345678"
); 
//生成客戶端對象,注意下面的url  ?wsdl是必須加的前面爲服務端的文件
$client=new soapclient("http://localhost/nusoap/soap_server.php?WSDL");
//呼叫/使用 那個函數,注意名字空間的區分
$return=$client->call('hello',$para,"http://localhost/soap/test_wsdl","test_wsdl#hello"); 
print_r($return);
?>
上面是一個很基本的使用,我想不過多的說了

下面看一個比較複雜的使用

<?php
require_once('../../lib/nusoap.php');
//取得值返回 返回的是一個數組
function validate($userName,$randomNum)
{
    
$temp['userName'] = $userName;
    
$temp['randomNum'] =$randomNum;
    return 
$temp;
}
//本想做個set一個get方法,但是這個時候發現了一個問題,以後再說
function refresh($refresh_userName,$refresh_randomNum)
{
    
$temp['userName'] = $userName;
    
$temp['randomNum'] =$randomNum;
    return 
$temp;
}

function 
logout($logout_userName)//註銷用戶名(退出登錄)
{
$flag=$logout_userName;

    if(
$logout_userName=="" || $logout_userName!="叄石")
    {
    
$flag = -1;
    }

return 
$flag;
}

$soap = new soap_server();
//參數一,服務名字,也就是server文件
//參數二,名字空間
$soap->configureWSDL('AuthorityServicewsdl''urn:AuthorityServicewsdl');
//參數設置空間
$soap->wsdl->schemaTargetNamespace 'urn:AuthorityServicewsdl';
//上面跟原來的基本一樣,但是大家注意聲明的時候發生了變化
//下面是設置一個返回的數組類型
//參數一,爲你自己命名的數據類型
//參數二,混合類型,不用管
//參數三。爲結構體,或者數組(array)
//參數四,按照什麼排序,有三個選擇all(全部)|sequence(次序)|choice(選擇)
//參數五,基本約束
//注意:上面5個我們不用改變
//參數6,最重要的參數,也就是我們返回的類型想對應
//返回數組下標 =>array('name'=>"數組下標",'type'=>"xsd:對應的類型") 
//類型基本包括 string,int, date,boolean 這幾種形式
$soap->wsdl->addComplexType('userInfo','complexType','struct','all','',
        array(
            
'userName' => array('name'=>'userName''type'=>'xsd:string'),
       
'randomNum' => array('name'=>'randomNum''type'=>'xsd:string')
            )
        );

//這裏是註冊函數
//參數一,函數名
//參數二,是這個函數接受的參數,注意指定類型
//參數三,爲返回的值,不一定名字非叫return,叫其他的也可以,注意返回的類型,
//我這裏是返回我自己定義的類型 tns:userInfo 如果爲基本的類型爲 xsd:string 這個樣子
//其他的參數,只要統一就可以了,尤其是名字空間跟 soapaction
$soap->register('validate',                                                                                    // method name
    
array('userName' => 'xsd:string','randomNum' => 'xsd:string'),    // input parameters
    //array('return' => 'xsd:string'),            // output parameters    
    
array('return' => 'tns:userInfo'),    
    
'urn:AuthorityServicewsdl',                    // namespace
    
'urn:AuthorityServicewsdl#validate',        // soapaction
    
'rpc',                                        // style
    
'encoded',                                    // use
    
'驗證用戶名及隨機數'                        // documentation
);

$soap->register('refresh',                                                                                    // method name
    
array('refresh_userName' => 'xsd:string','refresh_randomNum' => 'xsd:string'),    // input parameters
    //array('return' => 'xsd:string'),// output parameters
    
array('return' => 'tns:userInfo'),    
    
'urn:AuthorityServicewsdl',                                                                            // namespace
    
'urn:AuthorityServicewsdl#refresh',                                                            // soapaction
    
'rpc',                                                                                                                    // style
    
'encoded',                                                                                                            // use
    
'按時地方'                                                                                                            // documentation
);

$soap->register('logout',                                                                                        // method name
    
array('logout_userName' => 'xsd:string'),                                                // input parameters
    
array('return' => 'xsd:int'),                                                                        // output parameters
    
'urn:AuthorityServicewsdl',                                                                            // namespace
    
'urn:AuthorityServicewsdl#logout',                                                            // soapaction
    
'rpc',                                                                                                                    // style
    
'encoded',                                                                                                            // use
    
'退出登錄'                                                                                                            // documentation
);
//大家可能對 $HTTP_RAW_POST_DATA 這個變量比較疑惑,我們沒有定義,怎麼出來的呢
//我也有這樣的想法,不過我看了下手冊解釋是 usually is the value of $HTTP_RAW_POST_DATA 
//所以大家就別管它了,就這樣使用吧
$soap->service($HTTP_RAW_POST_DATA);
?>
我們現在看下 客戶端的使用

<?php
require_once('../../lib/nusoap.php');

$client=new soapclient('http://localhost/nusoap/samples/server/AuthorityService.php?wsdl',true);
$err $client->getError();

$username "叄石";
$randomnum "d8A9";
$params1 = array('userName'=>$username,'randomNum'=>$randomnum);
$reversed $client->call('validate',$params1);//此爲返回值
//echo "註冊值<br>";
print_r ($reversed);
echo 
"<br>";

$refresh_username "叄石";
$refresh_randomnum "d8A9";
$params1 = array('refresh_userName'=>$refresh_username,'refresh_randomNum'=>$refresh_randomnum);
$reversed $client->call('refresh',$params1);
//echo "取得值<br>";
print_r ($reversed);
echo 
"<br>";

$logout_username "叄石";
$params1 = array('logout_userName'=>$logout_username);
$reversed $client->call('logout',$params1);
echo 
$reversed;
?>
這個就不用多說了,一看應該就明白了 是數組返回的我使用print_r輸出了
關於這個功能,我說下我做的實驗
首先我用set方法設置了下一個公共變量的值,當用get取得的時候,值爲空
說明,每次是建立一個新的訪問所以值沒有保留
測試2,我使用cookie 設置的值,在get方法的時候,沒有取得
說明:沒鬧明白原因
測試3,使用session  在get方法的時候,也沒有取得值
說明,沒鬧明白
但是取得文件或者數據庫連接取得值是沒有問題的,也就是可以取得固定介質中的值,文件或者數據庫

這個東西到底能幹什麼還真不清楚,我想是可以完成,2種不同語言的交接,比如 php與asp與jsp的交互
也許有人茫然了,我這裏丟了一個感念,soap返回的是一個流,所有可以當作文本流來對待,
使用socket取得。
關於nusoap的使用就到這裏了,我不大去弄它的具體原理了,能夠使用就夠了!所以沒有對nusoap的代碼做分析

作者:叄石
mail:[email protected]
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章