---
updatedAt: 2026-03-25T09:39:27.000Z
---

# SMS Verify API - Tutorial: Send one-time passcode with Telesign PHP SDK

This tutorial teaches you how to use the Telesign PHP SDK to send an SMS with a one-time passcode (OTP). Go to GitHub to see the complete [sample code](https://github.com/TeleSign/sample_code/blob/master/php/sdk/sms_verify/src/verify_with_own_code.php).

## Before you begin

Make sure you have the following before you start:

* **Authentication credentials:** Your Customer ID and API Key. If you need help finding these items, go to the support article [How do I find my Customer ID and API Key](https://support.telesign.com/s/article/Find-Customer-ID-and-API-Key).
* **Testing device:** A mobile phone on which you can receive SMS.
* **Composer** - This package manager isn't required to use this SDK, but it is required to use this tutorial.

> 📘 NOTE:
>
> This tutorial uses the following.
>
> * MacOS
> * PHP v8.2.4
> * Composer v2.4.4
>
> Please modify accordingly if your developer environment differs from these details.

## Set up your project

> 🚧 CAUTION:
>
> You should use the Full-service SDK for SMS Verify API even if you have a Self-service account!

1. Follow the Telesign Full-service PHP SDK install instructions on GitHub [here](https://github.com/TeleSign/php_telesign_enterprise), incorporating the following details:

   * Use `sms_verify` as the project directory and package name.
   * Use `project` as the Package Type.
   * Use `no` for defining both your `require` and `require-dev` dependencies interactively.
   * Use the default directory `src/` for PSR-4 autoload mapping.

You should end up in the top-level directory ("sms\_verify") for your project in the Terminal.

2. Create a new file in the "src" directory called "verify\_with\_own\_code.php"

   ```shell Terminal
   touch src/verify_with_own_code.php
   ```

## Create code to send the SMS

1. Open the file "src/verify\_with\_own\_code.php".

2. Add a script opening tag.

   ```php
   <?php
   ```

3. Add the imports below. The `telesign\enterprise\sdk\verify\VerifyClient` import pulls selected functionality from the Telesign full-service SDK and the `telesign\sdk\util` import pulls from the Telesign self-service SDK.

   ```php
   require __DIR__ . "/../vendor/autoload.php";
   use telesign\enterprise\sdk\verify\VerifyClient;
   use function telesign\sdk\util\randomWithNDigits;
   ```

   > 📘 NOTE:
   >
   > The `require` line may need to be modified depending on the location of "autoload.php" in your file system. By default, Composer generates this file in a "vendor" directory at the top-level of your project, as indicated by the path shown in the example above.

4. Define variables to store your authentication credentials. For testing purposes, you can just overwrite the default value with your credentials or use environment variables.

   ```php
   $customer_id = getenv('CUSTOMER_ID') ? getenv('CUSTOMER_ID') :'FFFFFFFF-EEEE-DDDD-1234-AB1234567890';
   $api_key = getenv('API_KEY') ? getenv('API_KEY') :'ABC12345yusumoN6BYsBVkh+yRJ5czgsnCehZaOYldPJdmFh6NeX8kunZ2zU1YWaUw/0wV6xfw==';
   ```

5. Define a variable to hold the end-user's phone number you want to send an OTP to. For this tutorial, hardcode your testing device's phone number or pull it from an environment variable.

   ```php
   $phone_number = getenv('PHONE_NUMBER') ? getenv('PHONE_NUMBER'):'11234567890';
   ```

   > 📘 NOTE:
   >
   > In your production integration, pull the phone number from your recipient database instead of hardcoding it.

6. Randomly generate your OTP. We will use a Telesign SDK utility for this. The parameter value `5` specifies the number of digits generated.

   ```php
   $verify_code = randomWithNDigits(5);
   ```

   > 🚧 CAUTION:
   >
   > The method used above to generate a code is actually *pseudo-random*. In your production implementation, you might want to use a more robust method for randomizing.

7. Instantiate a verification client object with your authentication credentials.

   ```php
   $verify_client = new VerifyClient($customer_id, $api_key);
   ```

   > 📘 NOTE:
   >
   > When you use a Telesign SDK to make your request, authentication is handled behind-the-scenes for you. All you need to provide is your Customer ID and API Key. The SDKs apply Digest authentication whenever they make a request to a Telesign service where it is supported. When Digest authentication is not supported, the SDKs apply Basic authentication.

8. Make the request and capture the response. Behind the scenes, this sends an HTTP request to the Telesign SMS Verify API. Telesign then sends an SMS with an OTP to the end-user.

   ```php
   $response = $verify_client->sms($phone_number, [ "verify_code" => $verify_code ]);
   ```

9. Display the response in the console for debugging purposes. In your production code, you would likely remove this.

   ```php
   echo("\nResponse HTTP status:\n");
   print_r($response->status_code);
   echo("\nResponse body: \n");
   print_r($response->json);
   ```

10. Collect the asserted OTP entered by the end-user in your application. You can simulate this by prompting for input from the command line.

    ```php
    echo "Please enter the verification code you were sent: ";
    $user_entered_verify_code = rtrim(fgets(STDIN));
    ```

    > 📘 NOTE:
    >
    > In your production implementation, collect input from your website or other application where the end-user is trying to sign in.

11. Determine if the user-entered code matches your OTP, and resolve the sign in attempt accordingly. You can simulate this by reporting whether the codes match.

```php
if ($user_entered_verify_code == $verify_code) {
    echo "Your code is correct.\n";
} else {
    echo "Your code is incorrect.\n";
}
```

> 📘 NOTE:
>
> In your production implementation, add code here to sign in the user when the user-entered code matches the OTP.

1. Add a script closing tag.

   ```php
   ?>
   ```

## Test your integration

1. Switch from your editor to the terminal and run `src/verify_with_own_code.php`.

   ```shell Terminal
   php src/verify_with_own_code.php
   ```

   You should receive an SMS on your phone that looks like this:

[block:image]
{
  "images": [
    {
      "image": [
        "https://files.readme.io/45c6886-small-SMS-Verify-message-example.jpeg",
        null,
        "A screenshot of a SMS message containing a one time passcode displayed on a phone."
      ],
      "align": "center",
      "sizing": "550px"
    }
  ]
}
[/block]

2. Enter the OTP you received on your phone at the command prompt on the terminal to test that verification is successful.

   ```shell Terminal
   Please enter the verification code you were sent: 82139
   Your code is correct.
   ```

3. Now let's test an unsuccessful verification. Run again.

   ```shell Terminal
   php src/verify_with_own_code.php
   ```

   You should receive a new OTP on your phone.

4. Enter something else that isn't correct at the command prompt on the terminal and you should get a message that verification failed.

   ```shell Terminal
   Please enter the verification code you were sent: 55555
   Your code is incorrect.
   ```

## Sample code

The complete [sample code](https://github.com/TeleSign/sample_code/blob/master/php/sdk/sms_verify/src/verify_with_own_code.php) for this tutorial can be found on GitHub.