PHP的soap 之 nusoap 的使用

來源:互聯網
上載者:User

標籤:style   blog   http   java   color   使用   

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

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

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

先看一個基本的使用很簡單的
這是 soap_server.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

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);

上面是一個很基本的使用,我想不過多的說了

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

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);

我們現在看下 用戶端的使用

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的代碼做分析

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.