Skip to contentSkip to navigationSkip to topbar
On this page

Verify v1 Phone Verification PHP Quickstart


(warning)

Warning

Verify v1 API has reached End of Sale. It is now closed to new customers and will be fully deprecated in the future.

For new development, we encourage you to use the Verify v2 API. v2 has an improved developer experience and new features, including:

  • Twilio helper libraries in multiple languages
  • PSD2 Secure Customer Authentication Support
  • Improved Visibility and Insights

Existing customers will not be impacted at this time until Verify v1 API has reached End of Life. For more information about migration, see Migrating from 1.x to 2.x.

Phone Verification is an important, high-confidence step in your registration flow to verify that a user has the device they claim to have. Adding phone verification to your application will greatly reduce your number of fraudulent registrations and protect future application users from having their numbers registered by scammers.

This quickstart guides you through creating a PHP(link takes you to an external page), Laravel(link takes you to an external page) and AngularJS(link takes you to an external page) app that requires a phone verification step with Twilio Verify to create an account. Two channels of Phone Verification are demoed: SMS and Voice.

Ready to add Phone Verification to a demo app and keep the bad actors away? Enter stage left!


Sign into -or create- a Twilio account

sign-into--or-create--a-twilio-account page anchor

Either sign up for a free Twilio trial, or sign into an existing Twilio account(link takes you to an external page).

Create a New Account Security Application

create-a-new-account-security-application page anchor

Once logged in, visit the Authy Console(link takes you to an external page). Click on the red 'Create New Application' (or big red plus ('+') if you already created one) to create a new Authy application then name it something memorable.

Authy create new application.

Twilio will redirect you to the Settings page next:

Account Security API Key.

Click the eyeball to reveal your Production API Key and copy it somewhere safe. You will use the API Key during the application setup step below.


Install the application prerequisites

install-the-application-prerequisites page anchor

To complete the quickstart today we'll use PHP 7.0+, Composer, MySQL, and the Twilio PHP Helper Library. Let's walk through each one now - but feel free to skip if you have already installed one.

When doing web development in PHP, we strongly suggest using Composer(link takes you to an external page) for package management. This quickstart relies on Composer to install the PHP Helper library. You can find manual Twilio PHP installation instructions on the PHP Helper Library page(link takes you to an external page).

While Twilio's Verify API doesn't return user information you'll need to store, to continue working on the app after this Quickstart you'll want a database. For this demo, we built our user database on top of MySQL 5.x.

If you haven't yet installed it, here are instructions for your platform:

When installed, start MySQL. If you're using the default MySQL credentials (as below), create a schema account_security with user homestead and password secret.


Install the quickstart application

install-the-quickstart-application page anchor

Clone our PHP repository locally(link takes you to an external page), then enter the directory:

1
git clone git@github.com:TwilioDevEd/account-security-quickstart-php.git
2
cd account-security-quickstart-php
3
composer install

cp .env.example .env

Next, copy your Authy API Key from the Authy Dashboard and set the API_KEY variable in your .env file.

Install PHP(link takes you to an external page), Composer(link takes you to an external page), Laravel(link takes you to an external page) and MySQL(link takes you to an external page).

Enter an Application API Key

enter-an-application-api-key page anchor

Enter the API Key from the Account Security console and optionally change the port.

1
# Get your API key here: https://www.twilio.com/console/authy/getting-started
2
API_KEY=your-authy-api-key
3
4
APP_DEBUG=true
5
APP_ENV=development
6
APP_KEY=
7
APP_LOG=errorlog
8
9
# By default, if you'd like to match this install MySQL locally.
10
# host: localhost:3306
11
# DB user: homestead
12
# DB pass: secret
13
# DB name: account_security
14
15
DB_DATABASE=account_security
16
DB_HOST=localhost
17
DB_PASSWORD=secret
18
DB_PORT=3306
19
DB_USERNAME=homestead
20
MYSQL_DATABASE=account_security
21
MYSQL_PASSWORD=secret
22
MYSQL_PORT=3306
23
MYSQL_ROOT_PASSWORD=secret
24
MYSQL_USER=homestead
25
SECRET=your-account-secret

Migrate and launch the PHP Verify quickstart

migrate-and-launch-the-php-verify-quickstart page anchor

Now, launch the application with:

1
php artisan key:generate
2
php artisan migrate
3
php artisan serve --port 8081

Assuming your API Key is correctly entered and the command above executed correctly, you'll soon get a message that the app is up!


Use the PHP Phone Verification Demo

use-the-php-phone-verification-demo page anchor

Keeping your phone at your side, vist the Phone Verification page of the demo at http://localhost:8081/verify/(link takes you to an external page)

Enter a Country Code and Phone Number, then choose which channel to request verification over, 'SMS' or 'CALL' (Voice). Finally, hit the blue 'Request Verification' button and wait.

Phone Verification by SMS or Voice.

You won't be waiting long - you'll either receive a phone call or an SMS with the verification token. If you requested a phone call, as an additional security feature you may need to interact to proceed (by entering a number on the phone keypad).

Send a Phone Verification via SMS or Voice

send-a-phone-verification-via-sms-or-voice page anchor
1
<?php
2
3
namespace App\Http\Controllers\Auth;
4
5
use \Exception;
6
use App\Http\Controllers\Controller;
7
use Authy\AuthyApi;
8
use Illuminate\Http\Request;
9
use Illuminate\Support\Facades\Validator;
10
11
class PhoneVerificationController extends Controller
12
{
13
/*
14
|--------------------------------------------------------------------------
15
| Phone Verification Controller
16
|--------------------------------------------------------------------------
17
|
18
| Uses Authy to verify a users phone via voice or sms.
19
|
20
*/
21
22
/**
23
* Create a new controller instance.
24
*
25
* @return void
26
*/
27
public function __construct()
28
{
29
$this->middleware('guest');
30
}
31
32
/**
33
* Get a validator for an incoming verification request.
34
*
35
* @param array $data
36
* @return \Illuminate\Contracts\Validation\Validator
37
*/
38
protected function verificationRequestValidator(array $data)
39
{
40
return Validator::make($data, [
41
'country_code' => 'required|string|max:3',
42
'phone_number' => 'required|string|max:10',
43
'via' => 'required|string|max:4',
44
]);
45
}
46
47
/**
48
* Get a validator for an code verification request.
49
*
50
* @param array $data
51
* @return \Illuminate\Contracts\Validation\Validator
52
*/
53
protected function verificationCodeValidator(array $data)
54
{
55
return Validator::make($data, [
56
'country_code' => 'required|string|max:3',
57
'phone_number' => 'required|string|max:10',
58
'token' => 'required|string|max:4'
59
]);
60
}
61
62
/**
63
* Request phone verification via PhoneVerificationService.
64
*
65
* @param array $data
66
* @return Illuminate\Support\Facades\Response;
67
*/
68
protected function startVerification(
69
Request $request,
70
AuthyApi $authyApi
71
) {
72
$data = $request->all();
73
$validator = $this->verificationRequestValidator($data);
74
extract($data);
75
76
if ($validator->passes()) {
77
return $authyApi->phoneVerificationStart($phone_number, $country_code, $via);
78
}
79
80
return response()->json(['errors'=>$validator->errors()], 403);
81
}
82
83
/**
84
* Request phone verification via PhoneVerificationService.
85
*
86
* @param array $data
87
* @return Illuminate\Support\Facades\Response;
88
*/
89
protected function verifyCode(
90
Request $request,
91
AuthyApi $authyApi
92
) {
93
$data = $request->all();
94
$validator = $this->verificationCodeValidator($data);
95
extract($data);
96
97
if ($validator->passes()) {
98
try {
99
$result = $authyApi->phoneVerificationCheck($phone_number, $country_code, $token);
100
return response()->json($result, 200);
101
} catch (Exception $e) {
102
$response=[];
103
$response['exception'] = get_class($e);
104
$response['message'] = $e->getMessage();
105
$response['trace'] = $e->getTrace();
106
return response()->json($response, 403);
107
}
108
}
109
110
return response()->json(['errors'=>$validator->errors()], 403);
111
}
112
}

Either way you requested the passcode, enter the token into the Verification entry form and click 'Verify Phone':

Phone Verification Entry Box.

This function verifies the token for a user delivered over the Voice or SMS channel.

1
<?php
2
3
namespace App\Http\Controllers\Auth;
4
5
use \Exception;
6
use App\Http\Controllers\Controller;
7
use Authy\AuthyApi;
8
use Illuminate\Http\Request;
9
use Illuminate\Support\Facades\Validator;
10
11
class PhoneVerificationController extends Controller
12
{
13
/*
14
|--------------------------------------------------------------------------
15
| Phone Verification Controller
16
|--------------------------------------------------------------------------
17
|
18
| Uses Authy to verify a users phone via voice or sms.
19
|
20
*/
21
22
/**
23
* Create a new controller instance.
24
*
25
* @return void
26
*/
27
public function __construct()
28
{
29
$this->middleware('guest');
30
}
31
32
/**
33
* Get a validator for an incoming verification request.
34
*
35
* @param array $data
36
* @return \Illuminate\Contracts\Validation\Validator
37
*/
38
protected function verificationRequestValidator(array $data)
39
{
40
return Validator::make($data, [
41
'country_code' => 'required|string|max:3',
42
'phone_number' => 'required|string|max:10',
43
'via' => 'required|string|max:4',
44
]);
45
}
46
47
/**
48
* Get a validator for an code verification request.
49
*
50
* @param array $data
51
* @return \Illuminate\Contracts\Validation\Validator
52
*/
53
protected function verificationCodeValidator(array $data)
54
{
55
return Validator::make($data, [
56
'country_code' => 'required|string|max:3',
57
'phone_number' => 'required|string|max:10',
58
'token' => 'required|string|max:4'
59
]);
60
}
61
62
/**
63
* Request phone verification via PhoneVerificationService.
64
*
65
* @param array $data
66
* @return Illuminate\Support\Facades\Response;
67
*/
68
protected function startVerification(
69
Request $request,
70
AuthyApi $authyApi
71
) {
72
$data = $request->all();
73
$validator = $this->verificationRequestValidator($data);
74
extract($data);
75
76
if ($validator->passes()) {
77
return $authyApi->phoneVerificationStart($phone_number, $country_code, $via);
78
}
79
80
return response()->json(['errors'=>$validator->errors()], 403);
81
}
82
83
/**
84
* Request phone verification via PhoneVerificationService.
85
*
86
* @param array $data
87
* @return Illuminate\Support\Facades\Response;
88
*/
89
protected function verifyCode(
90
Request $request,
91
AuthyApi $authyApi
92
) {
93
$data = $request->all();
94
$validator = $this->verificationCodeValidator($data);
95
extract($data);
96
97
if ($validator->passes()) {
98
try {
99
$result = $authyApi->phoneVerificationCheck($phone_number, $country_code, $token);
100
return response()->json($result, 200);
101
} catch (Exception $e) {
102
$response=[];
103
$response['exception'] = get_class($e);
104
$response['message'] = $e->getMessage();
105
$response['trace'] = $e->getTrace();
106
return response()->json($response, 403);
107
}
108
}
109
110
return response()->json(['errors'=>$validator->errors()], 403);
111
}
112
}

And with that, your demo app is protected with Twilio's Verify phone verification! You can now log out to try the other channel.


Your demo app is now keeping hordes of fraudulent users from registering with your business and polluting the database. Next, you should check out all of the variables and options available to you in the Verify API Reference. Also, for protecting your customers in an ongoing manner (with this same codebase) try the PHP Authy Two-Factor Authentication Quickstart.

After that, take a stroll through the Docs for more Account Security demos and tutorials - as well as sample web applications using all of Twilio's products. Encore!

Need some help?

Terms of service

Copyright © 2024 Twilio Inc.