@@ -71,6 +71,14 @@ class RpcClient
71
71
* @var int
72
72
*/
73
73
protected $ timeout ;
74
+ /**
75
+ * @var string
76
+ */
77
+ protected $ username ;
78
+ /**
79
+ * @var string
80
+ */
81
+ protected $ password ;
74
82
75
83
public function __construct (array $ options )
76
84
{
@@ -84,8 +92,17 @@ public function __construct(array $options)
84
92
85
93
$ this ->server = $ options ['server ' ];
86
94
$ this ->host = $ options ['host ' ];
95
+ $ this ->username = isset ($ options ['username ' ]) ? $ options ['username ' ] : '' ;
96
+ $ this ->password = isset ($ options ['password ' ]) ? $ options ['password ' ] : '' ;
87
97
$ this ->debug = isset ($ options ['debug ' ]) ? (bool )$ options ['debug ' ] : false ;
88
98
$ 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
+ }
89
106
}
90
107
91
108
/**
@@ -110,6 +127,12 @@ public function getTimeout()
110
127
111
128
protected function sendRequest ($ command , array $ params )
112
129
{
130
+ if ($ this ->username && $ this ->password ) {
131
+ $ params = [
132
+ ['user ' => $ this ->username , 'server ' => $ this ->server , 'password ' => $ this ->password ], $ params
133
+ ];
134
+ }
135
+
113
136
$ request = xmlrpc_encode_request ($ command , $ params , ['encoding ' => 'utf-8 ' , 'escaping ' => 'markup ' ]);
114
137
115
138
$ ch = curl_init ();
@@ -122,6 +145,7 @@ protected function sendRequest($command, array $params)
122
145
curl_setopt ($ ch , CURLOPT_POST , true );
123
146
curl_setopt ($ ch , CURLOPT_POSTFIELDS , $ request );
124
147
curl_setopt ($ ch , CURLOPT_HTTPHEADER , ['User-Agent: GameNet ' , 'Content-Type: text/xml ' ]);
148
+
125
149
$ response = curl_exec ($ ch );
126
150
curl_close ($ ch );
127
151
0 commit comments