-
Notifications
You must be signed in to change notification settings - Fork 43
Open
Description
An "Array to string conversion" error gets trigggered when sending a request:
Here's the code used when error was triggered:
$config = [
'clientId' => 'xxxxxxx',
'clientSecret' => 'xxxxxxx'
];
$orangeSmsClient = new Osms($config);
$orangeSmsClient->setVerifyPeerSSL(false);
$response = $orangeSmsClient->getTokenFromConsumerKey();
$sndSms = $orangeSmsClient->sendSMS("+216000000", "+2160000", "Hi X, this is a test message.");The exception thrown reads as follow:
array(1) {
["requestError"]=> array(1) {
["serviceException"]=> array(3) {
["messageId"]=> string(7) "SVC0004"
["text"]=> string(46) "No valid addresses provided in message part %1"
["variables"]=> array(1) {
[0]=> string(13) "senderAddress"
}
}
}
}As can be seen above, the "variables" index contains an array and not string, said values are stand ins or placeholders for the error message (in the "text" index).
Corrective:
Edit this:
$errorMessage = $response['requestError']['serviceException']['text']
. ' ' . $response['requestError']['serviceException']['variables'];Into this:
$errorMessage = $response['requestError']['serviceException']['text'];
$errorVariables = $response['requestError']['serviceException']['variables'];
foreach ($errorVariables as $key => $errorVariable) {
$errorMessage = str_replace('%'.strtoupper($key+1), $errorVariable, $errorMessage);
}Or better yet, add a method;
private function getErrorMessage(string $exceptionType, array $response)
{
$message = '';
$exceptionTypes = ['serviceException', 'policyException'];
if (in_array($exceptionType, $exceptionTypes)) {
$message = $response['requestError'][$exceptionType]['text'];
$errorVariables = $response['requestError']['serviceException']['variables'];
if (!empty($errorVariables) && is_array($errorVariables)) {
foreach ($errorVariables as $key => $errorVariable) {
$message = str_replace('%'.strtoupper($key+1), $errorVariable, $message);
}
}
}
return $message;
}Then modify the "callApi" method from to reflect these changes:
public function callApi( ... )
{
// ...
if ($httpCode !== $successCode) {
$errorMessage = '';
if (!empty($response['error_description'])) {
$errorMessage = $response['error_description'];
} elseif (!empty($response['error'])) {
$errorMessage = $response['error'];
} elseif (!empty($response['description'])) {
$errorMessage = $response['description'];
} elseif (!empty($response['message'])) {
$errorMessage = $response['message'];
} elseif (!empty($response['requestError']['serviceException'])) {
$errorMessage = $this->getErrorMessage('serviceException', $response);
} elseif (!empty($response['requestError']['policyException'])) {
$errorMessage = $this->getErrorMessage('policyException', $response);
}
return array('error' => $errorMessage);
}
// ...
}Metadata
Metadata
Assignees
Labels
No labels
