| 1 |
liveuser |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
|
|
|
5 |
QBPWCF, Quick Build PHP website Component base on Fedora Linux.
|
| 624 |
liveuser |
6 |
Copyright (C) 2015~2024 Min-Jhin,Chen
|
| 1 |
liveuser |
7 |
|
|
|
8 |
This file is part of QBPWCF.
|
|
|
9 |
|
|
|
10 |
QBPWCF is free software: you can redistribute it and/or modify
|
|
|
11 |
it under the terms of the GNU General Public License as published by
|
|
|
12 |
the Free Software Foundation, either version 3 of the License, or
|
|
|
13 |
(at your option) any later version.
|
|
|
14 |
|
|
|
15 |
QBPWCF is distributed in the hope that it will be useful,
|
|
|
16 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
17 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
18 |
GNU General Public License for more details.
|
|
|
19 |
|
|
|
20 |
You should have received a copy of the GNU General Public License
|
|
|
21 |
along with Foobar. If not, see <http://www.gnu.org/licenses/>.
|
|
|
22 |
|
|
|
23 |
*/
|
|
|
24 |
namespace qbpwcf;
|
|
|
25 |
|
| 243 |
liveuser |
26 |
/*
|
|
|
27 |
類別說明:
|
|
|
28 |
跟SOAP應用相關的類別.
|
|
|
29 |
備註:
|
|
|
30 |
無.
|
|
|
31 |
*/
|
| 1 |
liveuser |
32 |
class soap{
|
|
|
33 |
|
|
|
34 |
/*
|
|
|
35 |
#函式說明:
|
|
|
36 |
#當前類別被呼叫的靜態方法不存在時,將會執行該函數,回報該方法不存在.
|
|
|
37 |
#回傳結果:
|
|
|
38 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
39 |
#$reuslt["error"],執行不正常結束的錯訊息陣列.
|
|
|
40 |
#$result["function"],當前執行的函式名稱.
|
|
|
41 |
#必填參數:
|
|
|
42 |
#$method,物件,為物件實體或類別名稱,會自動置入該參數.
|
|
|
43 |
#$arguments,陣列,為呼叫方法時所用的參數.
|
| 210 |
liveuser |
44 |
#可省略參數:
|
|
|
45 |
#無.
|
| 1 |
liveuser |
46 |
#參考資料:
|
|
|
47 |
#__call=>http://php.net/manual/en/language.oop5.overloading.php#object.callstatic
|
| 210 |
liveuser |
48 |
#備註:
|
|
|
49 |
#無.
|
| 1 |
liveuser |
50 |
*/
|
|
|
51 |
public function __call($method,$arguments){
|
|
|
52 |
|
|
|
53 |
#取得當前執行的函式
|
|
|
54 |
$result["function"]=__FUNCTION__;
|
|
|
55 |
|
|
|
56 |
#設置執行不正常
|
|
|
57 |
$result["status"]="false";
|
|
|
58 |
|
|
|
59 |
#設置執行錯誤
|
|
|
60 |
$result["error"][]=__NAMESPACE__ ."/".$method."() 不存在!";
|
|
|
61 |
|
|
|
62 |
#設置所丟入的參數
|
|
|
63 |
$result["error"][]=$arguments;
|
|
|
64 |
|
|
|
65 |
#回傳結果
|
|
|
66 |
return $result;
|
|
|
67 |
|
|
|
68 |
}#function __call end
|
|
|
69 |
|
|
|
70 |
/*
|
|
|
71 |
#函式說明:
|
|
|
72 |
#當前類別被呼叫的靜態方法不存在時,將會執行該函數,回報該方法不存在.
|
|
|
73 |
#回傳結果:
|
|
|
74 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
75 |
#$reuslt["error"],執行不正常結束的錯訊息陣列.
|
|
|
76 |
#$result["function"],當前執行的函式名稱.
|
|
|
77 |
#必填參數:
|
|
|
78 |
#$method,物件,為物件實體或類別名稱,會自動置入該參數.
|
|
|
79 |
#$arguments,陣列,為呼叫方法時所用的參數.
|
| 210 |
liveuser |
80 |
#可省略參數:
|
|
|
81 |
#無.
|
| 1 |
liveuser |
82 |
#參考資料:
|
| 210 |
liveuser |
83 |
#__callStatic=>http://php.net/manual/en/language.oop5.overloading.php#object.callstatic
|
|
|
84 |
#備註:
|
|
|
85 |
#無.
|
| 1 |
liveuser |
86 |
*/
|
|
|
87 |
public static function __callStatic($method,$arguments){
|
|
|
88 |
|
|
|
89 |
#取得當前執行的函式
|
|
|
90 |
$result["function"]=__FUNCTION__;
|
|
|
91 |
|
|
|
92 |
#設置執行不正常
|
|
|
93 |
$result["status"]="false";
|
|
|
94 |
|
|
|
95 |
#設置執行錯誤
|
|
|
96 |
$result["error"][]="欲呼叫的". __NAMESPACE__ ."/".$method."() 不存在!";
|
|
|
97 |
|
|
|
98 |
#設置所丟入的參數
|
|
|
99 |
$result["error"][]=$arguments;
|
|
|
100 |
|
|
|
101 |
#回傳結果
|
|
|
102 |
return $result;
|
|
|
103 |
|
|
|
104 |
}#function __callStatic end
|
|
|
105 |
|
|
|
106 |
/*
|
| 212 |
liveuser |
107 |
#函式說明:
|
| 1 |
liveuser |
108 |
#demo用的服務內容範例
|
| 210 |
liveuser |
109 |
#回傳結果
|
| 1 |
liveuser |
110 |
#$result,$conf["argv"]的內容。
|
| 210 |
liveuser |
111 |
#必填參數:
|
| 1 |
liveuser |
112 |
$conf["argv"]="";#要回傳的內容
|
| 210 |
liveuser |
113 |
#可省略參數:
|
|
|
114 |
#無.
|
|
|
115 |
#參考資料:
|
|
|
116 |
#無.
|
|
|
117 |
#備註:
|
|
|
118 |
#怪怪的,建議做個測試.
|
| 1 |
liveuser |
119 |
*/
|
| 210 |
liveuser |
120 |
public static function demoService(&$conf){
|
| 1 |
liveuser |
121 |
|
| 212 |
liveuser |
122 |
#函式說明:
|
| 1 |
liveuser |
123 |
#檢查陣列裡面的特定元素是否存在以及其變數型態是否正確,如果沒有設定則回傳提示訊息。
|
|
|
124 |
#回傳的結果:
|
|
|
125 |
#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
|
|
|
126 |
#$result[$shouldBtCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
|
|
|
127 |
#$result[$shouldBtCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
|
|
|
128 |
#必填寫的參數:
|
|
|
129 |
$conf["variableCheck"]["isexistMuti"]["varInput"]=$conf;#要檢查的陣列變數
|
|
|
130 |
$conf["variableCheck"]["isexistMuti"]["variableCheck"]=array("argv1","argv2");#要檢查的變數名稱陣列,形態爲陣列變數,例如: $conf["variableCheck"] = array("id","account","password");
|
|
|
131 |
#可以省略的參數:
|
|
|
132 |
#$conf["variableType"]=array();#要檢查的陣列變數內的元素應該爲何種變數形態,形態爲陣列 例如: $conf[variableType] = array("string","int","double");
|
|
|
133 |
#$conf["canBeEmptyString"]="false";#變數內容如果是空字串就不能算是有設置的話,請設為"false",預設為也算是有設置。
|
|
|
134 |
$checkResult=variableCheck::isexistMulti($conf["variableCheck"]["isexistMuti"]);
|
|
|
135 |
unset($conf["variableCheck"]);
|
|
|
136 |
|
|
|
137 |
#如果檢查失敗
|
|
|
138 |
if($checkResult["status"]=="false"){
|
|
|
139 |
|
|
|
140 |
#設置執行不正常
|
|
|
141 |
$result["status"]="false";
|
|
|
142 |
|
|
|
143 |
#設置錯誤訊息
|
|
|
144 |
$result["error"]=$checkResult;
|
|
|
145 |
|
|
|
146 |
#回傳結果
|
|
|
147 |
return $result;
|
|
|
148 |
|
|
|
149 |
}#if end
|
|
|
150 |
|
|
|
151 |
#如果檢查不通過
|
|
|
152 |
if($checkResult["passed"]=="false"){
|
|
|
153 |
|
|
|
154 |
#設置執行不正常
|
|
|
155 |
$result["status"]="false";
|
|
|
156 |
|
|
|
157 |
#設置錯誤訊息
|
|
|
158 |
$result["error"]=$checkResult;
|
|
|
159 |
|
|
|
160 |
#回傳結果
|
|
|
161 |
return $result;
|
|
|
162 |
|
|
|
163 |
}#if end
|
|
|
164 |
|
|
|
165 |
#回傳結果
|
|
|
166 |
return $conf;
|
|
|
167 |
|
|
|
168 |
}#function demoService end
|
|
|
169 |
|
|
|
170 |
/*
|
| 212 |
liveuser |
171 |
#函式說明:
|
| 1 |
liveuser |
172 |
#demo用的服務內容範例
|
| 210 |
liveuser |
173 |
#回傳結果
|
|
|
174 |
#$result,$conf["argv"]的內容。
|
|
|
175 |
#必填參數:
|
| 1 |
liveuser |
176 |
$conf["argv"]="";#要回傳的內容
|
| 210 |
liveuser |
177 |
#可省略參數:
|
|
|
178 |
#無.
|
|
|
179 |
#參考資料:
|
|
|
180 |
#無.
|
|
|
181 |
#備註:
|
|
|
182 |
#怪怪的,建議做個測試.
|
| 1 |
liveuser |
183 |
*/
|
| 210 |
liveuser |
184 |
public static function demoServiceDumpArgu(&$conf){
|
| 1 |
liveuser |
185 |
|
|
|
186 |
#回傳結果
|
|
|
187 |
return $conf;
|
|
|
188 |
|
|
|
189 |
}#function demoServiceDumpArgu end
|
|
|
190 |
|
|
|
191 |
/*
|
| 212 |
liveuser |
192 |
#函式說明:
|
| 1 |
liveuser |
193 |
#建立webService
|
|
|
194 |
#回傳的結果
|
|
|
195 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
196 |
#$reuslt["error"],執行不正常結束的錯訊息陣列.
|
|
|
197 |
#$result["function"],當前執行的函式名稱.
|
|
|
198 |
#$result["info"],webService的要求資訊
|
| 211 |
liveuser |
199 |
#$result["listFunctions"],為目前定義可以使用的類別與函式,其他的結果為錯誤尋息。
|
| 213 |
liveuser |
200 |
#必填參數:
|
| 1 |
liveuser |
201 |
#$conf["servicePhpFile"],字串,提供服務內容的php檔案位置,裏面要有1個名爲 $conf["serviceClassName"] 類別
|
|
|
202 |
$conf["servicePhpFile"]="";
|
|
|
203 |
#$conf["serviceClassName"],字串.提供服務的類別名稱,需要在$conf["servicePhpFile"]檔案裏面有的類別,若有使用命名空間,請加上。ex: "yourNamespace\className"
|
|
|
204 |
$conf["serviceClassName"]=""
|
| 215 |
liveuser |
205 |
#可省略參數:
|
|
|
206 |
#無.
|
| 1 |
liveuser |
207 |
#參考資料:
|
|
|
208 |
#http://www.cnblogs.com/chance1/archive/2009/04/08/1431949.html
|
|
|
209 |
#http://www.php.net/manual/en/function.is-soap-fault.php
|
|
|
210 |
#http://www.php.net/manual/en/soapserver.soapserver.php
|
|
|
211 |
#http://www.php.net/manual/en/soapserver.setclass.php
|
|
|
212 |
#http://www.php.net/manual/en/soapserver.handle.php
|
| 210 |
liveuser |
213 |
#備註:
|
|
|
214 |
#webService的服務端如果出現任何非xml的輸出就會導致用戶端執行失敗
|
|
|
215 |
#一份php檔案裡面只能有一個createService函數執行.
|
|
|
216 |
#要建立為服務的類別必須要include進來後再使用本函數.
|
| 1 |
liveuser |
217 |
*/
|
| 94 |
liveuser |
218 |
public static function createService(&$conf){
|
| 1 |
liveuser |
219 |
|
|
|
220 |
#初始化要回傳的變數
|
|
|
221 |
$result=array();
|
|
|
222 |
|
|
|
223 |
#初始化當前執行的函數名稱
|
|
|
224 |
$result["funciton"]=__FUNCTION__;
|
|
|
225 |
|
|
|
226 |
#如果 $conf 不為陣列
|
|
|
227 |
if(gettype($conf)!="array"){
|
|
|
228 |
|
|
|
229 |
#設置執行失敗
|
|
|
230 |
$result["status"]="false";
|
|
|
231 |
|
|
|
232 |
#設置執行錯誤訊息
|
|
|
233 |
$result["error"][]="\$conf變數須為陣列形態";
|
|
|
234 |
|
|
|
235 |
#如果傳入的參數為 null
|
|
|
236 |
if($conf==null){
|
|
|
237 |
|
|
|
238 |
#設置執行錯誤訊息
|
|
|
239 |
$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
|
|
|
240 |
|
|
|
241 |
}#if end
|
|
|
242 |
|
|
|
243 |
#回傳結果
|
|
|
244 |
return $result;
|
|
|
245 |
|
|
|
246 |
}#if end
|
|
|
247 |
|
|
|
248 |
#檢查參數
|
| 212 |
liveuser |
249 |
#函式說明:
|
| 1 |
liveuser |
250 |
#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
|
|
|
251 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
252 |
#$reuslt["error"],執行不正常結束的錯訊息陣列.
|
|
|
253 |
#$result["function"],當前執行的函式名稱.
|
|
|
254 |
#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
|
|
|
255 |
#$result[$shouldBtCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
|
|
|
256 |
#$result[$shouldBtCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
|
|
|
257 |
#$result[$shouldBtCheckedVarName]["error"],每個參數設定的錯誤訊息
|
|
|
258 |
#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
|
|
|
259 |
#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
|
|
|
260 |
#$result["notNeedVar"],字串陣列,多餘的參數名稱.
|
|
|
261 |
#必填寫的參數:
|
|
|
262 |
#$conf["variableCheck::checkArguments"]["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
|
|
|
263 |
$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
|
|
|
264 |
#$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
|
|
|
265 |
$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("servicePhpFile","serviceClassName");
|
|
|
266 |
#$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列 例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double");
|
|
|
267 |
$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("string","string");
|
|
|
268 |
#$conf["variableCheck::checkArguments"]["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
|
|
|
269 |
$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
|
|
|
270 |
#可以省略的參數:
|
|
|
271 |
#$conf["variableCheck::checkArguments"]["canBeEmptyString"],必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true"。
|
|
|
272 |
#$conf["variableCheck::checkArguments"]["canBeEmptyString"]="false";
|
|
|
273 |
#$conf["variableCheck::checkArguments"]["skipableVariableName"],爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
|
|
|
274 |
#$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("ssl","serverIp","port");
|
|
|
275 |
#$conf["variableCheck::checkArguments"]["skipableVariableType"],爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
|
|
|
276 |
#$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string","string","string");
|
|
|
277 |
#$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是必填參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
|
|
|
278 |
#$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array("true","127.0.0.1",null);
|
|
|
279 |
#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
|
|
|
280 |
#$conf["arrayCountEqualCheck"][]=array();
|
|
|
281 |
#參考資料來源:
|
|
|
282 |
#array_keys=>http://php.net/manual/en/function.array-keys.php
|
|
|
283 |
$checkResult=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
|
|
|
284 |
unset($conf["variableCheck::checkArguments"]);
|
|
|
285 |
|
|
|
286 |
#如果檢查失敗
|
|
|
287 |
if($checkResult["status"]=="false"){
|
|
|
288 |
|
|
|
289 |
#設置執行不正常
|
|
|
290 |
$result["status"]="false";
|
|
|
291 |
|
|
|
292 |
#設置錯誤訊息
|
|
|
293 |
$result["error"]=$checkResult;
|
|
|
294 |
|
|
|
295 |
#回傳結果
|
|
|
296 |
return $result;
|
|
|
297 |
|
|
|
298 |
}#if end
|
|
|
299 |
|
|
|
300 |
#如果檢查不通過
|
|
|
301 |
if($checkResult["passed"]=="false"){
|
|
|
302 |
|
|
|
303 |
#設置執行不正常
|
|
|
304 |
$result["status"]="false";
|
|
|
305 |
|
|
|
306 |
#設置錯誤訊息
|
|
|
307 |
$result["error"]=$checkResult;
|
|
|
308 |
|
|
|
309 |
#回傳結果
|
|
|
310 |
return $result;
|
|
|
311 |
|
|
|
312 |
}#if end
|
|
|
313 |
|
|
|
314 |
#建立 soap 的服務
|
|
|
315 |
#因為是在命名空間裡面,所以必須加上絕對位置「\」
|
|
|
316 |
$array["uri"]=basename($conf["servicePhpFile"]);
|
|
|
317 |
$soapServer=new \SoapServer(null,$array);
|
|
|
318 |
|
|
|
319 |
#取得webService的要求資訊
|
|
|
320 |
$result["info"]=$array;
|
|
|
321 |
|
|
|
322 |
#檢查 $conf["serviceClassName"] 是否存在
|
|
|
323 |
if(!(class_exists($conf["serviceClassName"]))){
|
|
|
324 |
|
|
|
325 |
#設置執行不正常
|
|
|
326 |
$result["status"]="false";
|
|
|
327 |
|
|
|
328 |
#設置錯誤訊息
|
|
|
329 |
$result["error"][]="類別 ".$conf["serviceClassName"]." 不存在,請檢查有無在allInOne.php與allInOneForSOAP.php匯入!";
|
|
|
330 |
|
|
|
331 |
#回傳結果
|
|
|
332 |
return $result;
|
|
|
333 |
|
|
|
334 |
}#if end
|
|
|
335 |
|
|
|
336 |
#設置處理服務的類別
|
|
|
337 |
$soapServer->setClass($conf["serviceClassName"]);
|
|
|
338 |
|
|
|
339 |
#取得目前已經可以被存取的class與function
|
|
|
340 |
$result["listFunctions"]=$soapServer->getFunctions();
|
|
|
341 |
|
|
|
342 |
#設置可以接收服務的要求
|
|
|
343 |
$soapServer->handle();
|
|
|
344 |
|
|
|
345 |
#執行到這邊代表執行正常
|
|
|
346 |
$result["status"]="true";
|
|
|
347 |
|
|
|
348 |
#值行到這邊代表執行順利
|
|
|
349 |
return $result;
|
|
|
350 |
|
|
|
351 |
}#function createService end
|
|
|
352 |
|
|
|
353 |
/*
|
| 212 |
liveuser |
354 |
#函式說明:
|
| 1 |
liveuser |
355 |
#呼叫WebService
|
|
|
356 |
#回傳的結果
|
|
|
357 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
358 |
#$reuslt["error"],執行不正常結束的錯訊息陣列.
|
|
|
359 |
#$result["function"],當前執行的函式名稱.
|
|
|
360 |
#$result["content"],呼叫完WebService所得回傳結果.
|
|
|
361 |
#$result["info"],取得webService的要求資訊.
|
|
|
362 |
#$result["info"]["method"],呼叫的方法.
|
|
|
363 |
#$result["info"]["argv"],呼叫方法用的參數.
|
| 213 |
liveuser |
364 |
#必填參數:
|
| 1 |
liveuser |
365 |
#$conf["servicePhpFile"],字串,提供服務內容的php檔案的完整絕對位置,裏面要有1個名爲 $conf["serviceClassName"] 類別
|
|
|
366 |
$conf["servicePhpFile"]="";
|
|
|
367 |
#$conf["soapMethod"],字串,要呼叫的方法
|
|
|
368 |
$conf["soapMethod"]="";
|
|
|
369 |
#$conf["argv"],字串陣列,呼叫方法時所要用到的參數內容,每個元素代表一個參數.
|
|
|
370 |
$conf["argv"]=array("");
|
| 214 |
liveuser |
371 |
#可省略參數:
|
| 1 |
liveuser |
372 |
#$conf["ssl"],字串,是否使用ssl,"true"代表要使用;"false"代表不使用,預設爲"false"
|
|
|
373 |
#$conf["ssl"]="false";
|
|
|
374 |
#$conf["pem"],字串,用於tls的憑證位置.
|
|
|
375 |
#$conf["pem"]="";
|
|
|
376 |
#$conf["serverIp"],字串,服務提供的主機ip位置,預設爲本機"127.0.0.1",也可以用doman name
|
|
|
377 |
#$conf["serverIp"]="127.0.0.1";
|
|
|
378 |
#$conf["port"],port通常不用ssl時為80,用ssl時為443.
|
|
|
379 |
#$conf["port"]="443";
|
|
|
380 |
#備註:
|
|
|
381 |
#若$conf["ssl"]設為"true"將會呼叫失敗.
|
|
|
382 |
#目前無法用非80port呼叫主機的服務.
|
|
|
383 |
#參考資料:
|
|
|
384 |
#http://www.cnblogs.com/chance1/archive/2009/04/08/1431949.html
|
|
|
385 |
#http://www.php.net/manual/en/soapclient.soapcall.php
|
| 94 |
liveuser |
386 |
#https://www.php.net/manual/en/soapclient.getlastresponse.php
|
| 1 |
liveuser |
387 |
#備註:
|
|
|
388 |
#$conf["pem"]參數無法真正運作。
|
|
|
389 |
*/
|
| 210 |
liveuser |
390 |
public static function callService(&$conf){
|
| 1 |
liveuser |
391 |
|
|
|
392 |
#初始化要回傳的變數
|
|
|
393 |
$result=array();
|
|
|
394 |
|
|
|
395 |
#初始化當前執行的函數名稱
|
|
|
396 |
$result["funciton"]=__FUNCTION__;
|
|
|
397 |
|
|
|
398 |
#如果沒有參數
|
|
|
399 |
if(func_num_args()==0){
|
|
|
400 |
|
|
|
401 |
#設置執行失敗
|
|
|
402 |
$result["status"]="false";
|
|
|
403 |
|
|
|
404 |
#設置執行錯誤訊息
|
|
|
405 |
$result["error"]="函數".$result["function"]."需要參數";
|
|
|
406 |
|
|
|
407 |
#回傳結果
|
|
|
408 |
return $result;
|
|
|
409 |
|
|
|
410 |
}#if end
|
|
|
411 |
|
|
|
412 |
#取得參數
|
|
|
413 |
$result["argv"]=$conf;
|
|
|
414 |
|
|
|
415 |
#如果 $conf 不為陣列
|
|
|
416 |
if(gettype($conf)!="array"){
|
|
|
417 |
|
|
|
418 |
#設置執行失敗
|
|
|
419 |
$result["status"]="false";
|
|
|
420 |
|
|
|
421 |
#設置執行錯誤訊息
|
|
|
422 |
$result["error"][]="\$conf變數須為陣列形態";
|
|
|
423 |
|
|
|
424 |
#如果傳入的參數為 null
|
|
|
425 |
if($conf==null){
|
|
|
426 |
|
|
|
427 |
#設置執行錯誤訊息
|
|
|
428 |
$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
|
|
|
429 |
|
|
|
430 |
}#if end
|
|
|
431 |
|
|
|
432 |
#回傳結果
|
|
|
433 |
return $result;
|
|
|
434 |
|
|
|
435 |
}#if end
|
|
|
436 |
|
|
|
437 |
#檢查參數
|
| 212 |
liveuser |
438 |
#函式說明:
|
| 1 |
liveuser |
439 |
#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
|
|
|
440 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
441 |
#$reuslt["error"],執行不正常結束的錯訊息陣列.
|
|
|
442 |
#$result["function"],當前執行的函式名稱.
|
|
|
443 |
#$result["warning"],警告訊息陣列.
|
|
|
444 |
#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
|
|
|
445 |
#$result[$shouldBtCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
|
|
|
446 |
#$result[$shouldBtCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
|
|
|
447 |
#$result[$shouldBtCheckedVarName]["error"],每個參數設定的錯誤訊息
|
|
|
448 |
#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
|
|
|
449 |
#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
|
|
|
450 |
#$result["notNeedVar"],字串陣列,多餘的參數名稱.
|
|
|
451 |
#必填寫的參數:
|
|
|
452 |
#$conf["variableCheck::checkArguments"]["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
|
|
|
453 |
$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
|
|
|
454 |
#$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
|
|
|
455 |
$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("servicePhpFile","soapMethod","argv");
|
|
|
456 |
#$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列 例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double");
|
|
|
457 |
$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("string","string","array");
|
|
|
458 |
#$conf["variableCheck::checkArguments"]["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
|
|
|
459 |
$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
|
|
|
460 |
#可以省略的參數:
|
|
|
461 |
#$conf["variableCheck::checkArguments"]["canBeEmptyString"],必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true"。
|
|
|
462 |
$conf["variableCheck::checkArguments"]["canBeEmptyString"]="false";
|
|
|
463 |
#$conf["variableCheck::checkArguments"]["skipableVariableName"],爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
|
|
|
464 |
$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("ssl","serverIp","port","pem");
|
|
|
465 |
#$conf["variableCheck::checkArguments"]["skipableVariableType"],爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
|
|
|
466 |
$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string","string","string","string");
|
|
|
467 |
#$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是必填參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
|
|
|
468 |
$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array("false","127.0.0.1",null,null);
|
|
|
469 |
#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
|
|
|
470 |
#$conf["arrayCountEqualCheck"][]=array();
|
|
|
471 |
#參考資料來源:
|
|
|
472 |
#array_keys=>http://php.net/manual/en/function.array-keys.php
|
|
|
473 |
$checkResult=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
|
|
|
474 |
unset($conf["variableCheck::checkArguments"]);
|
|
|
475 |
|
|
|
476 |
#如果檢查失敗
|
|
|
477 |
if($checkResult["status"]=="false"){
|
|
|
478 |
|
|
|
479 |
#設置執行不正常
|
|
|
480 |
$result["status"]="false";
|
|
|
481 |
|
|
|
482 |
#設置錯誤訊息
|
|
|
483 |
$result["error"]=$checkResult;
|
|
|
484 |
|
|
|
485 |
#回傳結果
|
|
|
486 |
return $result;
|
|
|
487 |
|
|
|
488 |
}#if end
|
|
|
489 |
|
|
|
490 |
#如果檢查不通過
|
|
|
491 |
if($checkResult["passed"]=="false"){
|
|
|
492 |
|
|
|
493 |
#設置執行不正常
|
|
|
494 |
$result["status"]="false";
|
|
|
495 |
|
|
|
496 |
#設置錯誤訊息
|
|
|
497 |
$result["error"]=$checkResult;
|
|
|
498 |
|
|
|
499 |
#回傳結果
|
|
|
500 |
return $result;
|
|
|
501 |
|
|
|
502 |
}#if end
|
|
|
503 |
|
|
|
504 |
#如果爲要使用 ssl
|
|
|
505 |
if($conf["ssl"]=="true"){
|
|
|
506 |
|
|
|
507 |
#採用 https 協定
|
|
|
508 |
$conf["ssl"]="https";
|
|
|
509 |
|
|
|
510 |
}#if end
|
|
|
511 |
|
|
|
512 |
#如果爲不使用 ssl
|
|
|
513 |
else{
|
|
|
514 |
|
|
|
515 |
#則採用 http 協定
|
|
|
516 |
$conf["ssl"]="http";
|
|
|
517 |
|
|
|
518 |
}#else end
|
|
|
519 |
|
|
|
520 |
#如果沒有設定 $conf["port"]
|
|
|
521 |
if(!isset($conf["port"])){
|
|
|
522 |
|
|
|
523 |
#如果 $conf["ssl"] 為 "http"
|
|
|
524 |
if($conf["ssl"]=="http"){
|
|
|
525 |
|
|
|
526 |
#預設為 "80"
|
|
|
527 |
$conf["port"]="80";
|
|
|
528 |
|
|
|
529 |
}#if end
|
|
|
530 |
|
|
|
531 |
#如果 $conf["ssl"] 為 "true"
|
|
|
532 |
if($conf["ssl"]=="https"){
|
|
|
533 |
|
|
|
534 |
#預設為 "443"
|
|
|
535 |
$conf["port"]="443";
|
|
|
536 |
|
|
|
537 |
}#if end
|
|
|
538 |
|
|
|
539 |
}#if end
|
|
|
540 |
|
|
|
541 |
#與webServiceServer建立連結
|
|
|
542 |
#因為是在命名空間裡面,所以必須加上絕對位置「\」
|
|
|
543 |
|
|
|
544 |
#初始化儲存 SoapClient 用的參數.
|
|
|
545 |
$soapClientArray=array();
|
|
|
546 |
|
|
|
547 |
#被呼叫的服務其所在位置
|
|
|
548 |
$soapClientArray["location"]=$conf["ssl"]."://".$conf["serverIp"].":".$conf["port"]."/".$conf["servicePhpFile"];
|
|
|
549 |
|
|
|
550 |
#服務的命名空間
|
|
|
551 |
$soapClientArray["uri"]=basename($soapClientArray["location"]);
|
|
|
552 |
|
|
|
553 |
#如果是 https
|
|
|
554 |
if($conf["ssl"]==="https"){
|
|
|
555 |
|
|
|
556 |
#如果有設置 "pem"
|
|
|
557 |
if(isset($conf["pem"])){
|
|
|
558 |
|
|
|
559 |
#如果 "pem" 檔案存在
|
|
|
560 |
if(file_exists($conf["pem"])){
|
|
|
561 |
|
|
|
562 |
#設置 local_cert
|
|
|
563 |
#$soapClientArray["local_cert"]=$conf["pem"];
|
|
|
564 |
#$soapClientArray["passphrase"]="pwd";
|
|
|
565 |
|
|
|
566 |
}#if end
|
|
|
567 |
|
|
|
568 |
}#if end
|
|
|
569 |
|
|
|
570 |
/*
|
|
|
571 |
#allow_self_signed SSL
|
|
|
572 |
#https://bugs.php.net/bug.php?id=68855
|
|
|
573 |
$soapClientArray["stream_context"]=stream_context_create(
|
|
|
574 |
[
|
|
|
575 |
'ssl' =>
|
|
|
576 |
[
|
|
|
577 |
#不驗證 ssl
|
|
|
578 |
'verify_peer' => false,
|
|
|
579 |
'verify_peer_name' => false,
|
|
|
580 |
'allow_self_signed'=> true,
|
|
|
581 |
#'ciphers' => "SHA1",
|
|
|
582 |
]
|
|
|
583 |
]
|
|
|
584 |
);
|
|
|
585 |
*/
|
|
|
586 |
|
|
|
587 |
$soapClientArray["ssl_method"]='SOAP_SSL_METHOD_TLS';
|
|
|
588 |
$soapClientArray["trace"]=true;
|
|
|
589 |
$soapClientArray["exceptions"]=true;
|
|
|
590 |
|
|
|
591 |
}#if end
|
|
|
592 |
|
|
|
593 |
$soapClient=new \SoapClient(null,$soapClientArray);
|
|
|
594 |
|
|
|
595 |
#取得webService的要求資訊
|
|
|
596 |
$result["info"]=$soapClientArray;
|
|
|
597 |
$result["info"]["method"]=$conf["soapMethod"];
|
|
|
598 |
$result["info"]["argv"]=$conf["argv"];
|
|
|
599 |
|
|
|
600 |
/*
|
|
|
601 |
#抓取soap過程中的錯誤訊息
|
|
|
602 |
#http://php.net/manual/en/class.soapfault.php
|
|
|
603 |
*/
|
|
|
604 |
|
|
|
605 |
#嘗試執行 __soapCall
|
|
|
606 |
try{
|
|
|
607 |
|
|
|
608 |
#間接呼叫webService的方法
|
|
|
609 |
$soapCallresult=$soapClient->__soapCall($conf["soapMethod"],$conf["argv"]);
|
|
|
610 |
|
|
|
611 |
#儲存運行的XML要求
|
|
|
612 |
$result["requestXML"]="REQUEST:\n" .$soapClient->__getLastRequest() . "\n";
|
|
|
613 |
|
|
|
614 |
}#try end
|
|
|
615 |
|
|
|
616 |
#抓取例外狀況
|
|
|
617 |
#抓取SoapFault類別,結果儲存到$e
|
|
|
618 |
catch(\SoapFault $e){
|
|
|
619 |
|
|
|
620 |
#設置執行失敗
|
|
|
621 |
$result["status"]="false";
|
|
|
622 |
|
|
|
623 |
#設置錯誤訊息
|
|
|
624 |
$result["error"]["getCode"]=$e->getCode();
|
|
|
625 |
$result["error"]["getMessage"]=$e->getMessage();
|
|
|
626 |
$result["error"]["getFile"]=$e->getFile();
|
|
|
627 |
$result["error"]["getLine"]=$e->getLine();
|
|
|
628 |
$result["error"]["getTrace"]=$e->getTrace();
|
|
|
629 |
$result["error"]["getTraceAsString"]=$e->getTraceAsString();
|
| 94 |
liveuser |
630 |
|
|
|
631 |
#取得service的回應
|
|
|
632 |
$result["rawOutput"]=$soapClient->__getLastResponse();
|
| 1 |
liveuser |
633 |
|
|
|
634 |
#回傳結果
|
|
|
635 |
return $result;
|
|
|
636 |
|
|
|
637 |
}#catch end
|
|
|
638 |
|
|
|
639 |
#如果沒有回傳的內容
|
|
|
640 |
if($soapCallresult==NULL){
|
|
|
641 |
|
|
|
642 |
#設置警告訊息
|
|
|
643 |
$result["warning"][]="沒有接收到回傳的內容(可能執行過程遇到錯誤)";
|
|
|
644 |
|
|
|
645 |
}#if end
|
|
|
646 |
|
|
|
647 |
#取得soap執行後的回傳結果
|
|
|
648 |
$result["content"]=$soapCallresult;
|
|
|
649 |
|
| 94 |
liveuser |
650 |
#取得service的回應
|
|
|
651 |
$result["rawOutput"]=$soapClient->__getLastResponse();
|
|
|
652 |
|
| 1 |
liveuser |
653 |
#執行到這邊代表執行正常
|
|
|
654 |
$result["status"]="true";
|
|
|
655 |
|
|
|
656 |
#回傳執行解果
|
|
|
657 |
return $result;
|
|
|
658 |
|
|
|
659 |
}#function callService end
|
|
|
660 |
|
|
|
661 |
}#class soap end
|
|
|
662 |
|
| 94 |
liveuser |
663 |
?>
|