Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
daibe opened this issue Jan 6, 2020 · 3 comments
Open

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

daibe opened this issue Jan 6, 2020 · 3 comments

Comments

@daibe
Copy link

daibe commented Jan 6, 2020

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

// ...
}
@salmankalela
Copy link

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.");

@slydesign1
Copy link

slydesign1 commented Jan 6, 2021

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.

@Douglasokolaa
Copy link

Hmm.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants