原文: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:Envelope
    xmlns: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 above
    CURLOPT_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;

 

 

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部