Skip to content

Error: Array to string conversion on Osms.php (line 291) #6

@daibe

Description

@daibe

An "Array to string conversion" error gets trigggered when sending a request:

image

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

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions