|
31 | 31 | */
|
32 | 32 | namespace GameNet\Jabber;
|
33 | 33 |
|
| 34 | +use fXmlRpc\Client; |
| 35 | +use fXmlRpc\Serializer\NativeSerializer; |
| 36 | +use fXmlRpc\Transport\HttpAdapterTransport; |
| 37 | +use Ivory\HttpAdapter\CurlHttpAdapter; |
| 38 | +use Ivory\HttpAdapter\Configuration; |
| 39 | + |
34 | 40 | /**
|
35 | 41 | * Class RpcClient
|
36 | 42 | *
|
@@ -127,37 +133,29 @@ public function getTimeout()
|
127 | 133 |
|
128 | 134 | protected function sendRequest($command, array $params)
|
129 | 135 | {
|
| 136 | + $config = new Configuration(); |
| 137 | + $config->setTimeout($this->getTimeout()); |
| 138 | + $config->setUserAgent('GameNet'); |
| 139 | + |
| 140 | + $transport = new HttpAdapterTransport(new CurlHttpAdapter($config)); |
| 141 | + $client = new Client($this->server, $transport, null, new NativeSerializer()); |
| 142 | + |
130 | 143 | if ($this->username && $this->password) {
|
131 | 144 | $params = [
|
132 | 145 | ['user' => $this->username, 'server' => $this->server, 'password' => $this->password], $params
|
133 | 146 | ];
|
134 | 147 | }
|
135 | 148 |
|
136 |
| - $request = xmlrpc_encode_request($command, $params, ['encoding' => 'utf-8', 'escaping' => 'markup']); |
137 |
| - |
138 |
| - $ch = curl_init(); |
139 |
| - curl_setopt($ch, CURLOPT_URL, $this->server); |
140 |
| - curl_setopt($ch, CURLOPT_FAILONERROR, 1); |
141 |
| - curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); |
142 |
| - curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
143 |
| - curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout); |
144 |
| - curl_setopt($ch, CURLOPT_HEADER, false); |
145 |
| - curl_setopt($ch, CURLOPT_POST, true); |
146 |
| - curl_setopt($ch, CURLOPT_POSTFIELDS, $request); |
147 |
| - curl_setopt($ch, CURLOPT_HTTPHEADER, ['User-Agent: GameNet', 'Content-Type: text/xml']); |
148 |
| - |
149 |
| - $response = curl_exec($ch); |
150 |
| - curl_close($ch); |
151 |
| - |
152 |
| - $xml = xmlrpc_decode($response); |
153 |
| - if (!$xml || xmlrpc_is_fault($xml)) { |
154 |
| - throw new \RuntimeException("Error execution command '$command'' with parameters " . var_export($params, true) . ". Response: $response"); |
| 149 | + try { |
| 150 | + $result = $client->call($command, $params); |
| 151 | + } catch (\fXmlRpc\Exception\RuntimeException $e) { |
| 152 | + throw new \RuntimeException($e->getMessage(), $e->getCode(), $e); |
155 | 153 | }
|
156 | 154 |
|
157 | 155 | if ($this->debug) {
|
158 |
| - var_dump($command, $params, $response); |
| 156 | + var_dump($command, $client->getPrependParams(), $client->getAppendParams(), $result); |
159 | 157 | }
|
160 | 158 |
|
161 |
| - return $xml; |
| 159 | + return $result; |
162 | 160 | }
|
163 | 161 | }
|
0 commit comments