@@ -71,6 +71,14 @@ class RpcClient
7171 * @var int
7272 */
7373 protected $ timeout ;
74+ /**
75+ * @var string
76+ */
77+ protected $ username ;
78+ /**
79+ * @var string
80+ */
81+ protected $ password ;
7482
7583 public function __construct (array $ options )
7684 {
@@ -84,8 +92,17 @@ public function __construct(array $options)
8492
8593 $ this ->server = $ options ['server ' ];
8694 $ this ->host = $ options ['host ' ];
95+ $ this ->username = isset ($ options ['username ' ]) ? $ options ['username ' ] : '' ;
96+ $ this ->password = isset ($ options ['password ' ]) ? $ options ['password ' ] : '' ;
8797 $ this ->debug = isset ($ options ['debug ' ]) ? (bool )$ options ['debug ' ] : false ;
8898 $ this ->timeout = isset ($ options ['timeout ' ]) ? (int )$ options ['timeout ' ] : 5 ;
99+
100+ if ($ this ->username && !$ this ->password ) {
101+ throw new \InvalidArgumentException ("Password cannot be empty if username was defined " );
102+ }
103+ if (!$ this ->username && $ this ->password ) {
104+ throw new \InvalidArgumentException ("Username cannot be empty if password was defined " );
105+ }
89106 }
90107
91108 /**
@@ -110,6 +127,12 @@ public function getTimeout()
110127
111128 protected function sendRequest ($ command , array $ params )
112129 {
130+ if ($ this ->username && $ this ->password ) {
131+ $ params = [
132+ ['user ' => $ this ->username , 'server ' => $ this ->server , 'password ' => $ this ->password ], $ params
133+ ];
134+ }
135+
113136 $ request = xmlrpc_encode_request ($ command , $ params , ['encoding ' => 'utf-8 ' , 'escaping ' => 'markup ' ]);
114137
115138 $ ch = curl_init ();
@@ -122,6 +145,7 @@ protected function sendRequest($command, array $params)
122145 curl_setopt ($ ch , CURLOPT_POST , true );
123146 curl_setopt ($ ch , CURLOPT_POSTFIELDS , $ request );
124147 curl_setopt ($ ch , CURLOPT_HTTPHEADER , ['User-Agent: GameNet ' , 'Content-Type: text/xml ' ]);
148+
125149 $ response = curl_exec ($ ch );
126150 curl_close ($ ch );
127151
0 commit comments