Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
V

vonage-php-sdk-core

> 安全
Open source

Vonage REST API client for PHP. API support for SMS, Voice, Text-to-Speech, Numbers, Verify (2FA) and more.

934 stars0 likes3 views
WebsiteGitHub

About

Vonage REST API client for PHP. API support for SMS, Voice, Text-to-Speech, Numbers, Verify (2FA) and more.

Client Library for PHP

This library requires a minimum PHP version of 8.1

This is the PHP client library for use Vonage's API. To use this, you'll need a Vonage account. Sign up for free here.

  • Installation
  • Usage
  • Examples
  • Test Suite
  • Contributing

Installation

To use the client library you'll need to have [created a Vonage account][signup].

To install the PHP client library to your project, we recommend using Composer.

bash
composer require vonage/client

Note that this actually points to a wrapper library that includes an HTTP client -and- this core library. You can install this library directly from Composer if you wish, with the ability to choose the HTTP client your project uses.

You don't need to clone this repository to use this library in your own projects. Use Composer to install it from Packagist.

If you're new to Composer, here are some resources that you may find useful:

  • Composer's Getting Started page from Composer project's documentation.
  • A Beginner's Guide to Composer from the good people at ScotchBox.

Usage

If you're using Composer, make sure the autoloader is included in your project's bootstrap file:

php
require_once "vendor/autoload.php";

Create a client with your API key and secret:

php
$client = new Vonage\Client(new Vonage\Client\Credentials\Basic(API_KEY, API_SECRET));     

For testing purposes you may want to change the URL that vonage/client makes requests to from api.vonage.com to something else. You can do this by providing an array containing base_api_url as the second parameter when creating a Vonage\Client instance.

php
$client = new Vonage\Client(
    new Vonage\Client\Credentials\Basic(API_KEY, API_SECRET),
    [
        'base_api_url' => 'https://example.com'
    ]
);

For APIs that would usually hit rest.nexmo.com, supplying a base_rest_url as an option to the constructor will change those requests.

Examples

Sending a Message via the SMS API

To use [Vonage's SMS API][doc_sms] to send an SMS message, call the $client->sms()->send() method.

A message object is used to create the SMS messages. Each message type can be constructed with the required parameters, and a fluent interface provides access to optional parameters.

php
$text = new \Vonage\SMS\Message\SMS(VONAGE_TO, VONAGE_FROM, 'Test message using PHP client library');
$text->setClientRef('test-message');

The message object is passed to the send method:

php
$response = $client->sms()->send($text);

Once sent, the message object can be used to access the response data.

php
$data = $response->current();
echo "Sent message to " . $data->getTo() . ". Balance is now " . $data->getRemainingBalance() . PHP_EOL;

Since each SMS message can be split into multiple messages, the response contains an object for each message that was generated. You can check to see how many messages were generated using the standard count() function in PHP. If you want to get the first message, you can use the current() method on the response.

php
$data = $response->current();
$data->getRemainingBalance();
foreach($response as $index => $data){
    $data->getRemainingBalance();
}

The [send example][send_example] also has full working examples.

Detecting Encoding Type

You can use a static isGsm7() method within the SMS Client code to determine whether to send the message using GSM-7 encoding or Unicode. Here is an example:

php
$sms = new \Vonage\SMS\Message\SMS('123', '456', 'is this gsm7?');

if (Vonage\SMS\Message\SMS::isGsm7($text)) {
    $sms->setType('text');
} else {
    $sms->setType('unicode');
}

Receiving a Message

Inbound messages are [sent to your application as a webhook][doc_inbound]. The Client library provides a way to create an inbound message object from a webhook:

php
try {
    $inbound = \Vonage\SMS\Webhook\Factory::createFromGlobals();
    error_log($inbound->getText());
} catch (\InvalidArgumentException $e) {
    error_log('invalid message');
}

Signing a Message

You may also like to read the documentation about message signing.

The SMS API supports the ability to sign messages by generating and adding a signature using a "Signature Secret" rather than your API secret. The algorithms supported are:

  • md5hash1
  • md5
  • sha1
  • sha256
  • sha512

Both your application and Vonage need to agree on which algorithm is used. In the dashboard, visit your account settings page and under "API Settings" you can select the algorithm to use. This is also the location where you will find your "Signature Secret" (it's different from the API secret).

Create a client using these credentials, and the algorithm to use, for example:

php
$client = new Vonage\Client(new Vonage\Client\Credentials\SignatureSecret(API_KEY, SIGNATURE_SECRET, 'sha256'));

Using this client, your SMS API messages will be sent as signed messages.

Verifying an Incoming Message Signature

You may also like to read the documentation about message signing.

If you have message signing enabled for incoming messages, the SMS webhook will include the fields sig, nonce and timestamp. To verify the signature is from Vonage, you create a Signature object using the incoming data, your signature secret and the signature method. Then use the check() method with the actual signature that was received (usually _GET['sig']) to make sure, that it is correct.

php
$signature = new \Vonage\Client\Signature($_GET, SIGNATURE_SECRET, 'sha256');

// is it valid? Will be true or false
$isValid = $signature->check($_GET['sig']);

Using your signature secret and the other supplied parameters, the signature can be calculated and checked against the incoming signature value.

Sending a Message via. the Messages API

The Messages API is used to send a variety of outbound messages. The following platforms are currently supported:

  • SMS
  • MMS
  • WhatsApp
  • Messenger
  • Viber

Each one of these platforms has a different category of message you can send (for example, with WhatsApp you can send text, an image, audio, video, a file or a template but for Viber you can only send a text or an image). You can find all the sendable message types under the namespace \Vonage\Messages\Channel. The reason each type is separated out this way is that the platform and message type requires different parameters in the API call.

The \Vonage\Messages\Client is configured in a similar way to the SMS API Client. The difference is that the authentication can be either a JSON Web Token (JWT) or Basic Authentication. You can find more info on how to set up your Client's credentials under the 'Usage' section of this ReadMe.

Here some examples:

Sending a WhatsApp Text

First, we need to create a new WhatsAppText object like so:

php
$whatsAppText = new Vonage\Messages\Channel\WhatsApp\WhatsAppText(
    FROM_NUMBER,
    TO_NUMBER,
    'this is a WA text from vonage'
);

The Messages API Client has one method, send() where you can send any of the message types provided. So, to send this message, the following code will do that, assuming you have already set up your Vonage client correctly:

php
$client->messages()->send($whatsAppText);

Your response will be a JSON payload if the error range is with 200, or will throw a relevant APIException if it's within 400/500.

Send a Viber Image

Some Channel objects require more arguments in order to be created. You can see the rough mapping of these requirements by comparing the constructor arguments vs. the API Documentation. Some of these messages take custom reusable objects (that are under the \Vonage\Messages\MessageObjects namespace). One of these is an image - so here is an example of how to send a Viber Image:

php
$imageObject = Vonage\Messages\MessageObjects\ImageObject(
    'https://picsum.photos/200/300',
    'image caption'
);

$viberImage = new Vonage\Messages\Channel\Viber\ViberImage(
    FROM_NUMBER,
    TO_NUMBER,
    $imageObject
);

$client->messages()->send($viberImage);

Verify Examples (v1)

Starting a Verification

Vonage's [Verify API][doc_verify] makes it easy to prove that a user has provided their own phone number during signup, or implement second factor authentication during sign in.

You can start a verification process using code like this:

php
$request = new \Vonage\Verify\Request('14845551212', 'My App');
$response = $client->verify()->start($request);
echo "Started verification with an id of: " . $response->getRequestId();

Once the user inputs the pin code they received, call the check() method (see below) with the request ID and the PIN to confirm the PIN is correct.

Controlling a Verification

To cancel an in-progress verification, or to trigger the next attempt to send the confirmation code, you can pass either an existing verification object to the client library, or simply use a request ID:

php
$client->verify()->trigger('00e6c3377e5348cdaf567e1417c707a5');
$client->verify()->cancel('00e6c3377e5348cdaf567e1417c707a5');

Checking a Verification

In the same way, checking a verification requires the PIN the user provided, and the request ID:

php
try {
    $client->verify()->check('00e6c3377e5348cdaf567e1417c707a5', '1234');
    echo "Verification was successful (status: " . $verification->getStatus() . ")\n";
} catch (Exception $e) {
    echo "Verification failed with status " . $e->getCode()
        . " and error text \"" . $e->getMessage() . "\"\n";
}

Searching For a Verification

You can check the status of a verification, or access the results of past verifications using a request ID. The verification object will then provide a rich interface:

php
$client->verify()->search('00e6c3377e5348cdaf567e1417c707a5');

echo "Codes checked for verification: " . $verification->getRequestId() . PHP_EOL;
foreach($verification->getChecks() as $check){
    echo $check->getDate()->format('d-m-y') . ' ' . $check->getStatus() . PHP_EOL;
}

Payment Verification

Vonage's [Verify API][doc_verify] has SCA (Secure Customer Authentication) support, required by the PSD2 (Payment Services Directive) and used by applications that need to get confirmation from customers for payments. It includes the payee and the amount in the message.

Start the verification for a payment like this:

php
$request = new \Vonage\Verify\RequestPSD2('14845551212', 'My App');
$response = $client->verify()->requestPSD2($request);
echo "Started verification with an id of: " . $response['request_id'];

Once the user inputs the pin code they received, call the /check endpoint with the request ID and the pin to confirm the pin is correct.

Verify Examples (v2)

Starting a Verification

Vonage's Verify v2 relies more on asynchronous workflows via. webhooks, and more customisable Verification workflows to the developer. To start a verification, you'll need the API client, which is under the namespace verify2.

Making a Verify request needs a 'base' channel of communication to deliver the mode of verification. You can customise these interactions by adding different 'workflows'. For each type of workflow, there is a Verify2 class you can create that will handle the initial workflow for you. For example:

php
$client = new Vonage\Client(
    new Vonage\Client\Credential

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

PHPdeveloper-destinationmessagingnexmophone

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category安全
PricingOpen source

> Related tools

O
OWASP ZAP
开源 Web 应用安全扫描器
O
owasp-wstg-tracker
Simple web app to track OWASP WSTG security testing progress
H
homebridge-mi-gateway-security
XiaoMi Gateway Security plugin for HomeBridge.