@@ -63,26 +63,22 @@ public function get_sig_and_expires() {
63
63
* @throws \Exception
64
64
* @return bool|mixed
65
65
*/
66
- public function call ($ method , $ params = array () , $ httpMethod = self ::HTTP_METHOD_POST ) {
66
+ public function call ($ method , $ params = [] , $ httpMethod = self ::HTTP_METHOD_POST ) {
67
67
if (!in_array ($ httpMethod , $ this ->allowedHttpMethods )) {
68
68
throw new \Exception ('Invalid HTTP method specified. ' );
69
69
}
70
70
$ method = str_replace ('/seo-tools/api ' , '' , $ method );
71
71
// some methods only require api key but there's no harm in also sending
72
72
// sig and expires to those methods
73
73
list ($ sig , $ expires ) = $ this ->get_sig_and_expires ();
74
- $ params = array_merge (array (
74
+ $ params = array_merge ([
75
75
'api-key ' => $ this ->apiKey ,
76
76
'sig ' => $ sig ,
77
77
'expires ' => $ expires
78
- ) , $ params );
78
+ ] , $ params );
79
79
$ client = new Client ;
80
80
try {
81
- if ($ httpMethod === static ::HTTP_METHOD_GET ) {
82
- $ result = $ client ->get ($ this ->endpoint . '/ ' . ltrim ($ method , '/ ' ), array ('query ' => $ params ));
83
- } else {
84
- $ result = $ client ->$ httpMethod ($ this ->endpoint . '/ ' . ltrim ($ method , '/ ' ), array ('form_params ' => $ params ));
85
- }
81
+ $ result = $ client ->$ httpMethod ($ this ->endpoint . '/ ' . ltrim ($ method , '/ ' ), $ this ->get_options ($ httpMethod , $ params ));
86
82
} catch (RequestException $ e ) {
87
83
$ result = $ e ->getResponse ();
88
84
}
@@ -134,4 +130,36 @@ public function delete($method, $params = array()) {
134
130
public function get_last_http_code () {
135
131
return $ this ->lastHttpCode ;
136
132
}
133
+
134
+ /**
135
+ * @param $httpMethod
136
+ * @param array $params
137
+ * @return array
138
+ */
139
+ private function get_options ($ httpMethod , $ params ) {
140
+ if ($ httpMethod === static ::HTTP_METHOD_GET ) {
141
+ return ['query ' => $ params ];
142
+ }
143
+ foreach ($ params as $ param ) {
144
+ if (is_resource ($ param )) {
145
+ return ['multipart ' => $ this ->convert_to_multipart ($ params )];
146
+ }
147
+ }
148
+ return ['form_params ' => $ params ];
149
+ }
150
+
151
+ /**
152
+ * @param array $params
153
+ * @return array
154
+ */
155
+ private function convert_to_multipart ($ params ) {
156
+ $ multipart = [];
157
+ foreach ($ params as $ key => $ value ) {
158
+ $ multipart [] = [
159
+ 'name ' => $ key ,
160
+ 'contents ' => $ value ,
161
+ ];
162
+ }
163
+ return $ multipart ;
164
+ }
137
165
}
0 commit comments