php – Laravel Nexmo invalid signature-ThrowExceptions

Exception or error:

I have SMS notification sending method via Nexmo client:

 try {
        $client = new Client(new SignatureSecret($nexmoKey, $signatureSecret, 'md5hash'));
        $message = $client->message()->send(
            [
                'to' => $this->userNumber,
                'from' => $from,
                'text' => $this->notificationMessage,
                'callback' => $webhookReceiveURL,
            ]
        );
        $response = $message->getResponseData();
        $this->notificationId = $response['messages'][0]['message-id'];

        return true;
    } catch (\Exception $e) {
        $this->exceptions[] = $e->getMessage();

        return false;
    }

If I put this piece of code in a service class where handle() method is called in Laravel’s commands via crontab I get: Invalid Signature error but if I call this method from any controller method notification sends successfully. Can anyone explain why I get error if I try to send notification via crontab?

How to solve:

If the same code works in a controller, it sounds like configuration settings aren’t getting passed into the job correctly. I would make sure that $nexmoKey and $signatureSecret look correct in the job.

On a side note, we do also offer a Laravel plugin through nexmo/laravel (https://github.com/Nexmo/nexmo-laravel/), which provides access to a Nexmo object via the Service Container and Facade layer. That may help cut down on the boilerplate for making the Nexmo object.

Leave a Reply

Your email address will not be published. Required fields are marked *