@@ -28,20 +28,27 @@ class Client
2828 /**
2929 * Perform a request
3030 *
31- * @param string $method Method name
32- * @param array $params The parameters to use for the POST body
31+ * @param string $method HTTP method name (GET, POST, etc.)
32+ * @param string $path API endpoint path
33+ * @param array|null $payload Request payload data
3334 *
34- * @return object
35+ * @return array Response data
36+ * @throws \DetectLanguage\Error When API request fails, invalid response received, or authentication fails
3537 */
36- public static function request ($ method , $ params = null )
38+ public static function request ($ method , $ path , $ payload = null )
3739 {
38- $ url = self ::getUrl ($ method );
40+ $ url = self ::getUrl ($ path );
41+
42+ if ($ payload !== null )
43+ $ body = json_encode ($ payload );
44+ else
45+ $ body = null ;
3946
40- $ request_method = self ::getRequestMethodName ();
41- $ response_body = self ::$ request_method ( $ url , $ params );
47+ $ engine_method = self ::getEngineMethodName ();
48+ $ response_body = self ::$ engine_method ( $ method , $ url , $ body );
4249 $ response = json_decode ($ response_body );
4350
44- if (!is_object ($ response ))
51+ if (!is_object ($ response ) && ! is_array ( $ response ) )
4552 throw new Error ("Invalid server response: $ response_body " );
4653
4754 if (isset ($ response ->error ))
@@ -50,12 +57,7 @@ public static function request($method, $params = null)
5057 return $ response ;
5158 }
5259
53- /**
54- * Get request method name.
55- *
56- * @return string
57- */
58- protected static function getRequestMethodName ()
60+ protected static function getEngineMethodName ()
5961 {
6062 $ request_engine = self ::$ requestEngine ;
6163
@@ -79,23 +81,27 @@ protected static function getRequestMethodName()
7981 /**
8082 * Perform request using native PHP streams
8183 *
84+ * @param string $method HTTP method name
8285 * @param string $url Request URL
83- * @param array $params The parameters to use for the POST body
86+ * @param string|null $body Request body
8487 *
8588 * @return string Response body
8689 */
87- protected static function requestStream ($ url , $ params )
90+ protected static function requestStream ($ method , $ url , $ body )
8891 {
8992 $ opts = array ('http ' =>
9093 array (
91- 'method ' => ' POST ' ,
94+ 'method ' => $ method ,
9295 'header ' => implode ("\n" , self ::getHeaders ()),
93- 'content ' => json_encode ($ params ),
9496 'timeout ' => self ::$ requestTimeout ,
9597 'ignore_errors ' => true ,
9698 )
9799 );
98100
101+ if ($ body !== null ) {
102+ $ opts ['http ' ]['content ' ] = $ body ;
103+ }
104+
99105 $ context = stream_context_create ($ opts );
100106
101107 return file_get_contents ($ url , false , $ context );
@@ -104,25 +110,31 @@ protected static function requestStream($url, $params)
104110 /**
105111 * Perform request using CURL extension.
106112 *
113+ * @param string $method HTTP method name
107114 * @param string $url Request URL
108- * @param array $params The parameters to use for the POST body
115+ * @param string|null $body Request body
109116 *
110117 * @return string Response body
118+ * @throws \DetectLanguage\Error When CURL request fails, times out, or connection fails
111119 */
112- protected static function requestCurl ($ url , $ params )
120+ protected static function requestCurl ($ method , $ url , $ body )
113121 {
114122 $ ch = curl_init ();
115123
116124 $ options = array (
125+ CURLOPT_CUSTOMREQUEST => $ method ,
117126 CURLOPT_URL => $ url ,
118127 CURLOPT_HTTPHEADER => self ::getHeaders (),
119- CURLOPT_POSTFIELDS => json_encode ($ params ),
120128 CURLOPT_CONNECTTIMEOUT => self ::$ connectTimeout ,
121129 CURLOPT_TIMEOUT => self ::$ requestTimeout ,
122130 CURLOPT_USERAGENT => self ::getUserAgent (),
123131 CURLOPT_RETURNTRANSFER => true
124132 );
125133
134+ if ($ body !== null ) {
135+ $ options [CURLOPT_POSTFIELDS ] = $ body ;
136+ }
137+
126138 curl_setopt_array ($ ch , $ options );
127139
128140 $ result = curl_exec ($ ch );
@@ -142,27 +154,17 @@ protected static function requestCurl($url, $params)
142154 * Build URL for given method
143155 *
144156 * @param string $method Method name
145- * @return string
157+ * @return string Complete API URL
146158 */
147159 protected static function getUrl ($ method )
148160 {
149- return self ::getProtocol () . ':// ' . DetectLanguage::$ host . '/ ' . DetectLanguage::$ apiVersion . '/ ' . $ method ;
150- }
151-
152- /**
153- * Get protocol for request.
154- *
155- * @return string 'https' or 'http'
156- */
157- protected static function getProtocol ()
158- {
159- return DetectLanguage::$ secure ? 'https ' : 'http ' ;
161+ return 'https:// ' . DetectLanguage::$ host . '/ ' . DetectLanguage::$ apiVersion . '/ ' . $ method ;
160162 }
161163
162164 /**
163165 * Build request headers.
164166 *
165- * @return array
167+ * @return array Array of HTTP headers
166168 */
167169 protected static function getHeaders ()
168170 {
@@ -177,7 +179,7 @@ protected static function getHeaders()
177179 /**
178180 * Get User-Agent for the request.
179181 *
180- * @return string
182+ * @return string User-Agent string
181183 */
182184 protected static function getUserAgent ()
183185 {
0 commit comments