原文:SOAP with TrinityCore | TrinityCore MMo Project Wiki
?
如何使用SOAP與TC交互
SOAP代表簡單對象訪問協議,是一種類似于REST的基于標準的web服務訪問協議的舊形式。只要必要的配置到位,您就可以利用SOAP向TrinityCore服務器發送命令。
理解SOAP的一個好方法是將其與當代REST進行比較。以下文章很好地解釋了這一點——https://smartbear.com/blog/soap-vs-rest-whats-the-difference/.
兩者之間的主要區別在于,SOAP完全依賴于XML來提供響應和接受有效載荷。PHP提供了一些使此過程更容易的方法,但根據您的使用情況,您可能需要熟悉XML。
配置
worldserver.conf
確保配置文件中的設置設置正確。
# SOAP.Enable
# Description: Enable soap service.
# Default: 0 - (Disabled)
# 1 - (Enabled)
SOAP.Enabled = 1# SOAP.IP
# Description: Bind SOAP service to IP/hostname.
# Default: "127.0.0.1" - (Bind to localhost)
SOAP.IP = "127.0.0.1"# SOAP.Port
# Description: TCP port to reach the SOAP service.
# Default: 7878
SOAP.Port = 7878
考慮到您的特定RBAC訪問配置,您還需要一個有權使用GM命令的用戶帳戶。專門為此目的創建一個受限訪問帳戶,而不是一個人使用的帳戶,這可能是一個好主意。
注意:在撰寫本文時,TC 335a僅支持HTTP,因此請注意不要以這種方式發送機密(密碼等)。假設傳遞的任何內容都是明文形式,任何人都可以閱讀。
如果您計劃通過SOAP遠程連接,您絕對應該采取措施確保安全連接。一種潛在的方法是通過apache或nginx通過反向SSL代理。但是,這不在本指南的范圍內,將不包括在內。
用于原型制作的HTTP客戶端
有一些客戶端可以快速建立連接并測試控制臺命令:
postman:https://www.postman.com/(網絡、桌面代理/客戶端)
insomnia:https://insomnia.rest/
夜鶯nightingale:https://nightingale.rest/
它們都提供了各種各樣的細節,但最終的工作原理大致相同。感謝Jackpoz為Postman提供的具體步驟-https://www.postman.com/.
Postman有兩種風格:web界面(以及可以安裝以執行localhost請求的代理)和完全客戶端桌面應用程序。這兩種情況下的說明是相同的。
在“我的工作區”下,找到“導入”按鈕。您將使用原始文本選項。
將以下JSON復制并粘貼到文本框中。請確保將item.request.auth.basic下的憑據更新為前面提到的GM用戶。
{"info": {"name": "TC SOAP","schema": "https://schema.getpostman.com/json/collection/v2.0.0/collection.json"},"item": [{"name": "server info","request": {"auth": {"type": "basic","basic": {"username": "CHANGEME","password": "CHANGEME","showPassword": false}},"method": "POST","header": [],"body": {"mode": "raw","raw": "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ns1=\"urn:TC\">\r\n<SOAP-ENV:Body>\r\n<ns1:executeCommand>\r\n<command>server info</command>\r\n</ns1:executeCommand>\r\n</SOAP-ENV:Body>\r\n</SOAP-ENV:Envelope>","options": {"raw": {"language": "xml"}}},"url": "http://127.0.0.1:7878"},"response": []}]
}
您應該將TC SOAP視為要導入的集合。單擊“導入”。
這將使用正確的HTTP方法(POST)以及“授權”和“正文”選項卡下的詳細信息填充新集合。
在Body選項卡下,注意XML負載和為您預先填充的服務器信息命令。
單擊“發送”按鈕將提交請求,并提供XML響應。
在Postman界面的右側,一個</>符號將打開代碼片段,可以將請求轉換為您選擇的語言。
?使用PHP
要使用PHP與TrinityCore交互,您需要確保安裝了PHP-soap擴展。還要確保您使用的是仍在積極支持的PHP版本。代碼示例在PHP7.4到PHP8.1上進行了測試。
在所有這些示例中,urn:TC URI都是必需的參數,因為我們沒有提供WSDL文檔。
SoapClient-https://www.php.net/manual/en/class.soapclient.php
$command = 'server info';$opts = ['http' => ['header' => "Authorization: Basic " . base64_encode("USERNAME:PASSWORD")]];$client = new SoapClient($wsdl = null, ['stream_context' => stream_context_create($opts),'location' => 'http://127.0.0.1:7878','uri' => 'urn:TC',
]);try {$result = $client->executeCommand(new SoapParam($command, 'command'));
} catch (\Exception $e) {die($e->getMessage());
}echo $result;
Note that we are passing a HTTP basic authorization header with base64 encoded username and password (separated by a colon). Alternatively, you could omit the?stream_context
?parameter, and instead include a (login) username and password in your SoapClient configuration.
$command = 'server info';$client = new SoapClient($wsdl = null, ['location' => 'http://127.0.0.1:7878','uri' => 'urn:TC','login' => 'USERNAME','password' => 'PASSWORD',
]);try {$result = $client->executeCommand(new SoapParam($command, 'command'));
} catch (Exception $e) {die($e->getMessage());
}echo $result;
Either approach is fine - but don't be fooled! Base 64 encoding does not inherently make it more secure.
Remember that the SOAP client can only recognize failures to connect, or misconfigurations.?It will not know if you've provided an invalid command. So it's up to you to parse the results and decide if the intended result was a success or not. Output will be just as if you performed the command on the console.
Lastly, if you'd rather not rely on the SOAP extension or client, you can form the XML payload and parse the resulting XML response yourself. You'll still need the?cURL extension, but this is usually available if not enabled by default.
<?xml version="1.0" encoding="utf-8"?>
<SOAP-ENV:Envelopexmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"xmlns:ns1="urn:TC"><SOAP-ENV:Body><ns1:executeCommand><command>server info</command></ns1:executeCommand></SOAP-ENV:Body>
</SOAP-ENV:Envelope>
$curl = curl_init();curl_setopt_array($curl, [CURLOPT_POSTFIELDS => $payload, // $payload is the XML provided aboveCURLOPT_URL => 'http://127.0.0.1:7878',CURLOPT_TIMEOUT => 0,CURLOPT_CUSTOMREQUEST => 'POST',CURLOPT_HTTPHEADER => ["Authorization: Basic " . base64_encode("{$user}:{$pass}"),'Content-Type: application/xml',],
]);$response = curl_exec($curl);
curl_close($curl);
echo $response;
?
?