PHPackages                             settermjd/laminas-twilio-phone-number-validator - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Validation &amp; Sanitization](/categories/validation)
4. /
5. settermjd/laminas-twilio-phone-number-validator

ActiveLibrary[Validation &amp; Sanitization](/categories/validation)

settermjd/laminas-twilio-phone-number-validator
===============================================

A laminas-validator class that validates phone numbers using Twilio's Verify API

1.4.5(8mo ago)218[2 issues](https://github.com/settermjd/laminas-phone-number-validator/issues)2BSD-3-ClausePHPPHP ~8.3.0 || ~8.4.0CI passing

Since Oct 21Pushed 8mo ago1 watchersCompare

[ Source](https://github.com/settermjd/laminas-phone-number-validator)[ Packagist](https://packagist.org/packages/settermjd/laminas-twilio-phone-number-validator)[ Fund](https://funding.communitybridge.org/projects/laminas-project)[ RSS](/packages/settermjd-laminas-twilio-phone-number-validator/feed)WikiDiscussions main Synced today

READMEChangelog (10)Dependencies (12)Versions (15)Used By (2)

Phone Number Validator that uses Twilio's Lookup (V2) API
=========================================================

[](#phone-number-validator-that-uses-twilios-lookup-v2-api)

[![Build Status](https://github.com/settermjd/laminas-phone-number-validator/actions/workflows/php.yml/badge.svg)](https://github.com/settermjd/laminas-phone-number-validator/actions/workflows/php.yml/badge.svg)

This is a custom laminas-validator class that checks if a phone number is valid by using [Twilio's Lookup API](https://www.twilio.com/docs/lookup).

Overview
--------

[](#overview)

The package provides a custom laminas-validator class that checks if a phone number is valid by using Twilio's Lookup API, providing a simple way of validating phone numbers are valid, based on communications provider data, accessed through Twilio.

Requirements
------------

[](#requirements)

To use the application, you'll need the following:

- A Twilio account (free or paid). [Create an account](http://www.twilio.com/referral/QlBtVJ) if you don't already have one.
- PHP 8.3 or 8.4
- [Composer](https://getcomposer.org) installed globally
- [Git](https://git-scm.com/downloads)

Getting Started
---------------

[](#getting-started)

### Add the Package as a Project Dependency

[](#add-the-package-as-a-project-dependency)

To use the package in your project, first, either add it in the project's *composer.json*'s `require` attribute, as in the example below.

```
"require": {
    "settermjd/laminas-twilio-phone-number-validator": "^1.0"
}
```

Alternatively, use `composer require` to add it:

```
composer require settermjd/laminas-twilio-phone-number-validator
```

### How to Use the Validator

[](#how-to-use-the-validator)

#### Direct Instantiation

[](#direct-instantiation)

You can use it directly, as in the following example, to validate a phone number.

```
use Settermjd\Validator\VerifyPhoneNumber;
use Twilio\Rest\Client;

$validator = new VerifyPhoneNumber(new Client(
    ``,
    ``,
));

if ($validator->isValid($email)) {
    // The phone number is valid, so do what you want knowing that.
} else {
    // The phone number is not valid, so show the reasons why.
    foreach ($validator->getMessages() as $messageId => $message) {
        printf("Validation failure '%s': %s\n", $messageId, $message);
    }
}
```

#### Used with laminas-inputfilter

[](#used-with-laminas-inputfilter)

Or, you can use it in conjunction with [laminas-inputfilter](https://docs.laminas.dev/laminas-inputfilter/), as in the following example.

```
use Laminas\InputFilter\InputFilter;
use Laminas\InputFilter\Input;
use Laminas\Validator;
use Settermjd\Filter\QueryParametersFilter;
use Settermjd\Validator\VerifyPhoneNumber;
use Twilio\Rest\Client;

$phoneNumber = new Input('phone_number');
$phoneNumber->getValidatorChain()
          ->attach(
          new VerifyPhoneNumber(
              new Client(
                  ``,
                  ``,
              ),
              new QueryParametersFilter(),
          )
    );

$inputFilter = new InputFilter();
$inputFilter->add($phoneNumber);

$inputFilter->setData($_POST);
if ($inputFilter->isValid()) {
    echo "The form is valid\n";
} else {
    echo "The form is not valid\n";
    foreach ($inputFilter->getInvalidInput() as $error) {
        print_r($error->getMessages());
    }
}
```

In both of the above examples, the `VerifyPhoneNumber` validator is initialised with a `Twilio\Rest\Client` object, which in turn is initialised with a Twilio Account SID and Auth Token. To retrieve these, open [the Twilio Console](https://console.twilio.com/) in your browser of choice, then copy the **Account SID** and **Auth Token** from the **Account Info**, as you can see in the screenshot below.

[![The Account Info panel of the Twilio Console, showing a user's Account SID, Auth Token, and phone number, where the Account SID and phone number have been partially or completely redacted.](./docs/images/twilio-console-account-info-panel.png)](./docs/images/twilio-console-account-info-panel.png)

Caution

Use a package such as [PHP Dotenv](https://github.com/vlucas/phpdotenv) to keep credentials, such as the Twilio Account SID and Auth Token out of code, and avoid them accidentally being tracked by Git (or your version control tool of choice), or your deployment tool's secrets manager is strongly encouraged.

#### In a Mezzio Project

[](#in-a-mezzio-project)

If you use this package with [Mezzio applications](https://docs.mezzio.dev/mezzio/), you don't need to do anything to instantiate or configure a `VerifyPhoneNumber` instance, as you do in the two previous examples. You only need to, first, retrieve the validator from the DI container, such as in a factory class that instantiates a handler, as in the example below.

```
