Skip to content

Commit 7424c37

Browse files
committed
Fix getProducts request
1 parent e085453 commit 7424c37

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

bootstrap.php

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
require_once __DIR__ . '/vendor/autoload.php';
3+
4+
define('ZQUARE_USER', '');
5+
define('ZQUARE_PASSWORD', '');

src/Api.php

+17-9
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,9 @@ public function resume($subscriberId, $subscriptionId = '')
311311
*/
312312
public function getProducts()
313313
{
314-
$products = $this->request('getproducts');
314+
//INFO The GetProducts method do not work with POST. Also, using GET we should not use `Accept` header as in
315+
// post case.
316+
$products = $this->request('getproducts', [], true);
315317

316318
return array_map(
317319
function ($e) {
@@ -348,19 +350,26 @@ public function getInfo($subscriberId, $subscriptionId = '')
348350
);
349351
}
350352

351-
protected function request($method, $data = [])
353+
protected function request($method, $data = [], $forceGet = false)
352354
{
353355
$headers = [];
354356

355-
$curl = curl_init(self::BASE_URL . $method);
356-
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
357-
curl_setopt($curl, CURLOPT_POST, true);
358-
if (!empty($data)) {
359-
curl_setopt($curl, CURLOPT_POSTFIELDS, urldecode(http_build_query($data)));
357+
$url = self::BASE_URL . $method;
358+
if ($forceGet && !empty($data)) {
359+
$url .= '?' . http_build_query($data);
360+
};
361+
362+
$curl = curl_init($url);
363+
if (!$forceGet) {
364+
curl_setopt($curl, CURLOPT_POST, true);
365+
if (!empty($data)) {
366+
curl_setopt($curl, CURLOPT_POSTFIELDS, urldecode(http_build_query($data)));
367+
}
368+
curl_setopt($curl, CURLOPT_HTTPHEADER, ['Accept: application/json']);
360369
}
370+
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
361371
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
362372
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
363-
curl_setopt($curl, CURLOPT_HTTPHEADER, ['Accept: application/json']);
364373
curl_setopt($curl, CURLOPT_HEADER, 0);
365374
curl_setopt($curl, CURLOPT_USERPWD, $this->authToken);
366375
curl_setopt($curl, CURLOPT_HEADERFUNCTION , function($ch, $headerLine) use (&$headers) {
@@ -385,7 +394,6 @@ protected function request($method, $data = [])
385394
throw new Exception($error, self::ERROR_UNKNOWN_ERROR);
386395
}
387396
curl_close($curl);
388-
389397
if ($code !== 200) {
390398
throw new Exception($headers['ErrorMessage'], (int)$headers['ErrorCode']);
391399
}

0 commit comments

Comments
 (0)