2021-10-18 18:24:38 +09:00
|
|
|
<?php
|
|
|
|
|
|
2021-10-21 09:55:32 +09:00
|
|
|
namespace gullevek\AmazonIncentives\Client;
|
2021-10-18 18:24:38 +09:00
|
|
|
|
2021-10-21 09:55:32 +09:00
|
|
|
use gullevek\AmazonIncentives\Exceptions\AmazonErrors;
|
|
|
|
|
use gullevek\AmazonIncentives\Debug\AmazonDebug;
|
2021-10-18 18:24:38 +09:00
|
|
|
|
|
|
|
|
class Client implements ClientInterface
|
|
|
|
|
{
|
2022-06-09 16:02:25 +09:00
|
|
|
/** @var int instead of JsonResponse::HTTP_OK */
|
2021-10-18 18:24:38 +09:00
|
|
|
private const HTTP_OK = 200;
|
|
|
|
|
|
|
|
|
|
/**
|
2022-06-09 16:02:25 +09:00
|
|
|
* Makes an request to the target url via curl
|
|
|
|
|
* Returns result as string (json)
|
2021-10-18 18:24:38 +09:00
|
|
|
*
|
2022-06-09 16:02:25 +09:00
|
|
|
* @param string $url The URL being requested,
|
|
|
|
|
* including domain and protocol
|
|
|
|
|
* @param array<mixed> $headers Headers to be used in the request
|
|
|
|
|
* @param array<mixed>|string $params Can be nested for arrays and hashes
|
|
|
|
|
* @return string Result as json string
|
2021-10-18 18:24:38 +09:00
|
|
|
*/
|
|
|
|
|
public function request(string $url, array $headers, $params): string
|
|
|
|
|
{
|
|
|
|
|
$handle = curl_init($url);
|
2021-10-21 13:29:50 +09:00
|
|
|
if ($handle === false) {
|
|
|
|
|
// throw Error here with all codes
|
|
|
|
|
throw AmazonErrors::getError(
|
|
|
|
|
'FAILURE',
|
|
|
|
|
'C001',
|
|
|
|
|
'CurlInitError',
|
|
|
|
|
'Failed to init curl with url: ' . $url,
|
|
|
|
|
0
|
|
|
|
|
);
|
|
|
|
|
}
|
2021-10-18 18:24:38 +09:00
|
|
|
curl_setopt($handle, CURLOPT_POST, true);
|
|
|
|
|
curl_setopt($handle, CURLOPT_HTTPHEADER, $headers);
|
|
|
|
|
// curl_setopt($handle, CURLOPT_FAILONERROR, true);
|
|
|
|
|
curl_setopt($handle, CURLOPT_POSTFIELDS, $params);
|
|
|
|
|
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
|
|
|
|
|
$result = curl_exec($handle);
|
|
|
|
|
|
|
|
|
|
if ($result === false) {
|
|
|
|
|
$err = curl_errno($handle);
|
|
|
|
|
$message = curl_error($handle);
|
|
|
|
|
$this->handleCurlError($url, $err, $message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (curl_getinfo($handle, CURLINFO_HTTP_CODE) !== self::HTTP_OK) {
|
|
|
|
|
$err = curl_errno($handle);
|
2021-10-19 09:39:14 +09:00
|
|
|
AmazonDebug::writeLog(['CURL_REQUEST_RESULT' => $result]);
|
2021-10-18 18:24:38 +09:00
|
|
|
// extract all the error codes from Amazon
|
2021-10-21 13:29:50 +09:00
|
|
|
$result_ar = json_decode((string)$result, true);
|
2021-10-19 10:52:04 +09:00
|
|
|
// if message is 'Rate exceeded', set different error
|
|
|
|
|
if (($result_ar['message'] ?? '') == 'Rate exceeded') {
|
|
|
|
|
$error_status = 'RESEND';
|
|
|
|
|
$error_code = 'T001';
|
|
|
|
|
$error_type = 'RateExceeded';
|
2021-10-21 13:37:59 +09:00
|
|
|
$message = $result_ar['message'] ?? 'Rate exceeded';
|
2021-10-19 10:52:04 +09:00
|
|
|
} else {
|
|
|
|
|
// for all other error messages
|
|
|
|
|
$error_status = $result_ar['agcodResponse']['status'] ?? 'FAILURE';
|
|
|
|
|
$error_code = $result_ar['errorCode'] ?? 'E999';
|
|
|
|
|
$error_type = $result_ar['errorType'] ?? 'OtherUnknownError';
|
|
|
|
|
$message = $result_ar['message'] ?? 'Unknown error occured';
|
|
|
|
|
}
|
|
|
|
|
// throw Error here with all codes
|
2021-10-18 18:24:38 +09:00
|
|
|
throw AmazonErrors::getError(
|
2021-10-19 09:39:14 +09:00
|
|
|
$error_status,
|
2021-10-18 18:24:38 +09:00
|
|
|
$error_code,
|
|
|
|
|
$error_type,
|
|
|
|
|
$message,
|
|
|
|
|
$err
|
|
|
|
|
);
|
|
|
|
|
}
|
2021-10-21 13:29:50 +09:00
|
|
|
return (string)$result;
|
2021-10-18 18:24:38 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2022-06-09 16:02:25 +09:00
|
|
|
* handles any CURL errors and throws an error with the correct
|
|
|
|
|
* error message
|
2021-10-18 18:24:38 +09:00
|
|
|
*
|
2022-06-09 16:02:25 +09:00
|
|
|
* @param string $url The url that was originaly used
|
|
|
|
|
* @param int $errno Error number from curl handler
|
|
|
|
|
* @param string $message The error message string from curl
|
2021-10-18 18:24:38 +09:00
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
private function handleCurlError(string $url, int $errno, string $message): void
|
|
|
|
|
{
|
|
|
|
|
switch ($errno) {
|
|
|
|
|
case CURLE_COULDNT_CONNECT:
|
|
|
|
|
case CURLE_COULDNT_RESOLVE_HOST:
|
|
|
|
|
case CURLE_OPERATION_TIMEOUTED:
|
2021-10-20 09:36:18 +09:00
|
|
|
$message = 'Could not connect to AWS (' . $url . '). Please check your '
|
|
|
|
|
. 'internet connection and try again. [' . $message . ']';
|
2021-10-18 18:24:38 +09:00
|
|
|
break;
|
|
|
|
|
case CURLE_SSL_PEER_CERTIFICATE:
|
2021-10-20 09:36:18 +09:00
|
|
|
$message = 'Could not verify AWS SSL certificate. Please make sure '
|
2021-10-18 18:24:38 +09:00
|
|
|
. 'that your network is not intercepting certificates. '
|
|
|
|
|
. '(Try going to ' . $url . 'in your browser.) '
|
2021-10-20 09:36:18 +09:00
|
|
|
. '[' . $message . ']';
|
2021-10-18 18:24:38 +09:00
|
|
|
break;
|
|
|
|
|
case 0:
|
|
|
|
|
default:
|
2021-10-20 09:36:18 +09:00
|
|
|
$message = 'Unexpected error communicating with AWS: ' . $message;
|
2021-10-18 18:24:38 +09:00
|
|
|
}
|
|
|
|
|
|
2021-10-19 10:52:04 +09:00
|
|
|
// throw an error like in the normal reqeust, but set to CURL error
|
|
|
|
|
throw AmazonErrors::getError(
|
|
|
|
|
'FAILURE',
|
2021-10-21 13:29:50 +09:00
|
|
|
'C002',
|
2021-10-19 10:52:04 +09:00
|
|
|
'CurlError',
|
|
|
|
|
$message,
|
|
|
|
|
$errno
|
|
|
|
|
);
|
2021-10-18 18:24:38 +09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// __END__
|