osms-php icon indicating copy to clipboard operation
osms-php copied to clipboard

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

Open daibe opened this issue 6 years ago • 3 comments

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);
        }

// ...
}

daibe avatar Jan 06 '20 11:01 daibe

Replace this : $sndSms = $orangeSmsClient->sendSMS("+216000000", "+2160000", "Hi X, this is a test message."); By : $sndSms = $orangeSmsClient->sendSMS("tel:+216000000", "tel:+2160000", "Hi X, this is a test message.");

salmankalela avatar Aug 07 '20 20:08 salmankalela

Hello i use your " fixed php notice ( Array to string conversion ) in src/Osms.php #8" to resolve my issue but i want to use composite sendername like "one world" but i still get an error array(1) { ["error"]=> string(47) "Invalid input value for message part senderName" } but on postman the message is going perfectly.

slydesign1 avatar Jan 06 '21 12:01 slydesign1

Hmm.

I recommend you should consider not using spaces in sender name. It's not a good practice. Most telecoms reject it.

Douglasokolaa avatar Jan 06 '21 13:01 Douglasokolaa