php 5 apache 2.2 webservice 建立與配置
測試載入器:wsCaller.jar (此文是給java程式調用的webservice)
提示:要運行wsCaller.jar 要選安裝jdk 如果沒有安裝jdk 則wsCaller.jar 會以壓縮包的形式顯示
1 寫webservice 要用到的介面
複製代碼 代碼如下:
<?php
class service
{
function add($a,$b){
$c=$a+$b;
return $c;
}
function str($string){
return $string;
}
}
//require("cls_mysql.php");
//$db = new cls_mysql("localhost", "root", "root", test_db");
//$db = new cls_mysql("211.103.156.227:3306", "root", "txtx", test_db");
//"testwsdl.wsd需要在後面採用Zend Studio產生。
$server=new SoapServer("testwsdl.wsdl",array("uri" => "http://webserver.com/","encoding"=>"utf-8","soap_version" => SOAP_1_2 ));
$server->setClass("service");
$server->handle();
?>
2.產生wsdl檔案
菜單中
File--àExport---àphp 選中 “wsdl file” 然後下一步 (圖1)
File name 是wsdl 檔案所在的位置
Exported files 下面的檔案是你上面剛剛建立的php檔案(介面檔案)
Classes 下面的service 前面的勾要打上
URL 中填寫介面檔案的訪問地址 (如:http://webserver.com/rpcservice.php)
點擊global settings 出現下圖(圖3)
選擇RPC – Oriented
然後點擊 “ok” 在點擊 “Finish”
出現下圖(圖4)
最後一步就是給 每個參數一個合理的類型(下圖)
然後儲存
到此 wsdl檔案已經產生
訪問路徑 http://webserver.com/testwsdl.wsdl (會出現下面內容)
如果不出現 則要配置apache 使其支援xml
註: 不要用chrome 開啟wsdl檔案 ,可能會顯示不正常 (可以用ie firfox)
測試是否可以成功調用
測試載入器
wsCaller.jar
註:必需安裝JDK 才能運行wsCaller.jar 請不要解壓wsCaller.jar
如果不能正確返回資訊 可直接存取你的php 看看會不會報錯
(本例http://webserver.com/rpcservice.php)
Php 要支援 soap
如果php 版本是5.X 則 編輯 php.ini
extension=php_soap.dll (添加 如果存在則去掉注釋)
重啟一下apache
apache 要注意這個檔案 mime.types
函數添加新參數
找到message name=”函數名” 中直接複製一下並把參數改一下參數名
WSDL檔案
<!--[endif]-->
Php介面檔案
1 |
function add($a,$b,$e){ |
註:$e 就是wsdl檔案中的 c 參數
添加新的函數
WSDL複製代碼就行; PHP寫出對應的函數 (看一下選中的部分 其實就是複製了一下add 這個函數 並且直接粘貼在它下面就可以) 下面以 添加add_bak 函數為例
WSDL
1.複製message 標籤
2.複製 portType 中的標籤
3.複製binding 標籤內容
PHP
複製代碼 代碼如下:
function add_bak($a,$b,$e){
$c=$a+$b-$e;
return $c;
}
測試結果
add_bak 函數
add 函數
其它函數就不一一測試了截圖了
如果有不對的地方,請指出 謝謝