メニュー

Expand
Rate this page:

Thanks for rating this page!

We are always striving to improve our documentation quality, and your feedback is valuable to us. How could this documentation serve you better?

Twilio Verify PHP Laravel Quickstart

With just a few lines of code, your PHP Laravel application can verify phone numbers and add an additional layer of security with Twilio Verify.

This PHP Verify Quickstart will teach you how to do this using our Verify REST API, the Twilio PHP helper library, and Laravel PHP Framework to ease development.

このクイックスタートでは、下記のことを学んでいきます:

  1. Twilioにサインアップ
  2. メッセージを送受信できるように開発環境をセットアップする
  3. Send your first SMS phone verification
  4. Check verification codes

Twilioにサインアップ

If you already have a Twilio account, you’re all set here! Feel free to jump to the next step.

Before you can send an SMS from PHP, you'll need to sign up for a Twilio account or sign into your existing account.

You can sign up for a free Twilio trial account here.

  • When you sign up, you'll be asked to verify your personal phone number. This helps Twilio verify your identity and also allows you to send test verification messages to your phone from your Twilio account while in trial mode. This phone verification step is exactly what you'll learn how to build in this tutorial!
  • Once you verify your number, you'll be asked to create a project. For the sake of this tutorial, you can click on the "Learn and Explore" template. Give your project a name, or just click "skip remaining steps" to continue with the default.
  • Once you get through the project creation flow, you'll arrive at your project dashboard in the Twilio Console. This is where you'll be able to access your Account SID, authentication token, create a verification service, and more.

Do I need a phone number?

If you've sent SMS with Twilio in the past, you might remember needing to buy a phone number. With Twilio Verify, we take care of that for you! The Verify API selects the best routes for quickly and reliably delivering verification codes globally.

Now that you have a Twilio account and a verification service, you can start writing some code! To make things even easier we will use Twilio's official helper for PHP applications.

Install PHP Core

If you’ve gone through one of our other PHP Quickstarts already and have PHP and Composer installed, you can skip this step and get straight to sending your first verification.

To start a phone verification and send your first SMS, you’ll need to have PHP 7 installed - if you don't know if you have PHP installed, run the following command to see what version you have:

php --version

The Twilio SDK requires PHP version 5.3 or higher, but we'll use PHP >= 7.1 in this tutorial as older versions are no longer supported.

If you have a not supported version of PHP you'll need to install it before going any further. Follow the directions for installing PHP for your platform (Windows, Mac, Linux).

The project dependencies are managed using composer. If you don't have it installed follow the instructions to download and install it.

検証アプリケーションをクローンおよびセットアップする

Start by cloning our Verify Quickstart PHP repository.

git clone git@github.com:TwilioDevEd/verify-v2-quickstart-php.git

If you don't have git installed or prefer to download the source code you can grab a zip file of the project here.

Install project dependencies

Twilio PHP Helper Library is already in the dependencies of the project. Run the following in the CLI to download them.

cd verify-v2-quickstart-php/

composer install

Copy .env.example to.env and update the content to set your Twilio Account sensitive data.

# Twilio API credentials
# (find here https://www.twilio.com/console)
TWILIO_ACCOUNT_SID=ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
TWILIO_AUTH_TOKEN=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

# Verification Service SID
# (create one here https://www.twilio.com/console/verify/services)
TWILIO_VERIFICATION_SID=VAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Create the local SQLite database file

touch database/twilio_verify_quickstart.sqlite (Unix like OS only)

Run database migrations

php artisan migrate

Create an application encryption key

php artisan key:generate

Run the app

php artisan serve

If it is all set up correctly you'll soon get a message that the app is up!

Use the PHP Twilio Verify Demo

Navigate to http://localhost:8000/register. You should see a registration form that looks like this:

sign up form with phone verification

Enter your phone number and choose which channel to request verification over. Finally hit the green Sign Up button and wait. 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 (the call will tell you to enter a number on the phone keypad).

Enter the token into the Verification entry form and click 'Verify':

verification entry form

これで、デモアプリケーションはTwilioの電話番号検証で保護されました! ログアウトして他のチャンネルもお試しいただけます。

関連トピック

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

After that, visit the Docs for more Account Security demos and tutorials and web applications using all of Twilio's products.

Rate this page:

ヘルプが必要ですか?

誰しもが一度は考える「コーディングって難しい」。そんな時は、お問い合わせフォームから質問してください。 または、Stack Overflow でTwilioタグのついた情報から欲しいものを探してみましょう。