Free Numbers Pricing Reviews Feedback Terms & Policy API Docs Blog About
Log in Get Started
0 views 4 min read
Back to Blog
Tutorials

PHP Example: Integrate an SMS Activation API Into Your Website

PHP Example: Integrate an SMS Activation API Into Your Website

Adding virtual number ordering to a PHP website is simpler than most developers expect. This php sms activation api example covers the order call, status checking, code delivery and error handling using plain PHP and cURL.

Direct answer: In PHP, use cURL to post your API key, country and service to the order endpoint, save the returned order ID, then call the status endpoint in a loop with sleep() until the code appears or the timeout is reached.

What You Need Before You Start

  • PHP 7.4 or newer with the cURL extension enabled.
  • An OtpGet API key from your dashboard.
  • A funded balance.
  • Access to the API documentation for exact parameter names.

Step 1: Store the API Key Safely

Never place the key directly in your PHP file. Put it in an environment variable or a config file outside the web root, and load it at runtime. If your code is on a shared host, a .env file with restricted permissions is a practical option.

Step 2: Create a Reusable Request Function

  1. Write one function that accepts a URL and an array of parameters.
  2. Set the cURL options for POST, timeouts and JSON headers.
  3. Return the decoded response and the HTTP status.
  4. Log failures without exposing the API key.

Reusing one function keeps all API calls consistent and makes errors easier to trace.

Step 3: Place the Order

Call the order endpoint with your key, country and service. On success, save the order ID and the phone number in your database. Show the number to the user right away with a countdown.

If the API returns NO_NUMBERS, show an alternative country option instead of a generic failure. If it returns NO_BALANCE, log an admin alert because your reseller balance needs a top-up.

Step 4: Check the Status and Read the Code

Use a short polling loop with sleep() between calls. In a web request, keep the total loop under the server timeout, or better, move polling to a scheduled job so the browser is not blocked.

ApproachWhen to use
Inline loopShort waits, low traffic
Cron jobProduction, many orders

Step 5: Deliver the Code and Handle Timeouts

Once the code arrives, show it to the user and store it against the order. If the timeout is reached, call the cancel endpoint and refund the user's balance in your system. Document the timeout rule on your site so disputes are easy to resolve.

Step 6: Validate Input and Protect Your Site

  • Validate country and service values against an allowlist.
  • Require a logged-in user with a funded balance before ordering.
  • Rate-limit order requests per account.
  • Use HTTPS for every request.

Step 7: Test Before You Go Live

Test the happy path, the NO_NUMBERS path and the timeout path. A simple test script that calls each function with mock data catches most bugs before users see them.

Frequently Asked Questions

Can I use file_get_contents instead of cURL?

It works for simple GET requests, but cURL gives better control over timeouts and headers for POST calls.

How do I avoid blocking the browser during polling?

Move polling to a background job or use a frontend request that checks your own database, not the API directly.

Where should I store order data?

In a database table with the order ID, phone number, status and user ID so you can trace every request.

What if the API key leaks?

Rotate it immediately in your dashboard and update your configuration.

Does this work on shared hosting?

Yes, as long as cURL is enabled and outbound requests are allowed.

Conclusion

This php sms activation api example gives you a working base for virtual number ordering on any PHP site. Keep the key safe, validate inputs, and handle errors and timeouts properly. Sign up on OtpGet and test the flow before launch.

Related reading: more integration guides on the OtpGet blog.

Tags

#php sms activation api #php virtual number #otp integration #curl api #php tutorial #sms verification
Contact us