@@ -33,7 +33,10 @@ public static function getBaseUrl()
33
33
public static function makeRequst ($ options )
34
34
{
35
35
$ method = isset ($ options ['method ' ]) ? $ options ['method ' ] : 'GET ' ;
36
- $ query = isset ($ options ['query ' ]) ? array_filter ($ options ['query ' ], function ($ x ) { return !is_null ($ x ); }) : [];
36
+ $ query = isset ($ options ['query ' ])
37
+ ? array_filter ($ options ['query ' ], function ($ x ) {
38
+ return !is_null ($ x );
39
+ }) : [];
37
40
38
41
$ body = isset ($ options ['body ' ]) ? $ options ['body ' ] : null ;
39
42
$ file = isset ($ options ['FILE ' ]) ? $ options ['FILE ' ] : null ;
@@ -53,9 +56,11 @@ public static function makeRequst($options)
53
56
54
57
$ ch = curl_init ();
55
58
56
- $ url = isset ($ options ['url ' ]) ? $ options ['url ' ].'? ' .http_build_query (array_merge (
57
- $ query , ['api_key ' => self ::getApiKey ()]
58
- )) : '' ;
59
+ $ url = isset ($ options ['url ' ])
60
+ ? $ options ['url ' ] . '? ' . http_build_query (array_merge (
61
+ $ query ,
62
+ ['api_key ' => self ::getApiKey ()]
63
+ )) : '' ;
59
64
60
65
$ baseUrl = self ::getBaseUrl ();
61
66
@@ -72,10 +77,10 @@ public static function makeRequst($options)
72
77
curl_setopt_array ($ ch , $ curlOpts );
73
78
74
79
if (null != $ file ) {
75
- $ cfile = new \CURLFile ($ file ,'' ,'' );
80
+ $ cfile = new \CURLFile ($ file , '' , '' );
76
81
$ body ['strFilename ' ]=$ cfile ;
77
82
curl_setopt ($ ch , CURLOPT_FOLLOWLOCATION , true );
78
- curl_setopt ($ ch , CURLOPT_POST ,true );
83
+ curl_setopt ($ ch , CURLOPT_POST , true );
79
84
curl_setopt ($ ch , CURLOPT_POSTFIELDS , $ body );
80
85
} else {
81
86
switch ($ method ) {
@@ -102,7 +107,8 @@ public static function makeRequst($options)
102
107
}
103
108
break ;
104
109
case 'ADD ' :
105
- curl_setopt ($ ch , CURLOPT_POSTFIELDS , json_encode ($ query )); break ;
110
+ curl_setopt ($ ch , CURLOPT_POSTFIELDS , json_encode ($ query ));
111
+ break ;
106
112
}
107
113
108
114
if (is_numeric (array_search ($ method , ['DELETE ' , 'PUT ' ]))) {
@@ -113,35 +119,47 @@ public static function makeRequst($options)
113
119
}
114
120
115
121
$ result = curl_exec ($ ch );
122
+ $ code = curl_getinfo ($ ch , CURLINFO_HTTP_CODE );
123
+ curl_close ($ ch );
116
124
117
- $ isxml = false ;
118
- $ jxml = '' ;
125
+ $ json = null ;
119
126
if (strpos ($ result , '<?xml ' ) > -1 ) {
120
127
$ xml = simplexml_load_string ($ result );
121
- //$jxml = json_encode ($xml);
122
- $ jxml = self :: object2array ( $ xml );
123
- $ isxml = true ;
128
+ $ json = self :: object2array ($ xml );
129
+ } else {
130
+ $ json = json_decode ( $ result , true ) ;
124
131
}
125
132
126
- $ code = curl_getinfo ($ ch , CURLINFO_HTTP_CODE );
127
- curl_close ($ ch );
128
-
129
133
if (200 == $ code ) {
130
- if ($ isxml ) {
131
- $ json = $ jxml ;
132
- } else {
133
- $ json = json_decode ($ result , true );
134
- }
135
-
136
134
if (isset ($ json ['errors ' ])) {
137
- throw new ApiError (implode (', ' , $ json ['errors ' ]));
135
+ throw new ApiError (implode (', ' , $ json ['errors ' ]), $ code , $ result );
138
136
} else {
139
137
return $ json ;
140
138
}
141
- } elseif (409 == $ code ) {
142
- throw new ApiError ('Wrong API key ' );
139
+ } elseif (isset ( $ code ) && (! isset ( $ result ) || ! $ result ) ) {
140
+ throw new ApiError ('' , $ code , $ result );
143
141
} else {
144
- throw new ApiError ('Something wrong ' );
142
+ if (isset ($ json ['messages ' ])) {
143
+ $ msg = '' ;
144
+ foreach ($ json ['messages ' ] as $ key => $ value ) {
145
+ if ($ msg !== '' ) {
146
+ $ msg .= PHP_EOL ;
147
+ }
148
+ $ msg .= $ key . ': ' . implode (', ' , $ value );
149
+ }
150
+ throw new ApiError ($ msg , $ code , $ result );
151
+ } elseif (isset ($ json ['errors ' ])) {
152
+ $ msg = '' ;
153
+ foreach ($ json ['errors ' ] as $ key => $ value ) {
154
+ if ($ msg !== '' ) {
155
+ $ msg .= PHP_EOL ;
156
+ }
157
+ $ msg .= $ value ;
158
+ }
159
+ throw new ApiError ($ msg , $ code , $ result );
160
+ } else {
161
+ throw new ApiError ($ result , $ code , $ result );
162
+ }
145
163
}
146
164
}
147
165
0 commit comments