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

OTP Verification API in PHP: Complete Working Example

OTP Verification API in PHP: Complete Working Example

PHP remains one of the most common languages for small verification tools, and the flow is short enough to fit in a single class. An OTP verification API in PHP integration is four requests: order a number, poll for the message, extract the code and release the number. The value is in the structure, the error handling and the cleanup. This guide provides all three with code you can adapt for a script, a Laravel app or a plain PHP service.

Direct answer: To use the OTP verification API in PHP, send a POST to order a number, poll the message endpoint until a code arrives, extract it with a service-specific regex, and send a release request in a finally block. Use cURL with a timeout and read the API key from configuration.

The Class Structure

  1. Create a class with a constructor that accepts the HTTP client and configuration.
  2. Add one method per endpoint: order, fetch, release.
  3. Read the API key and base URL from configuration, never from a constant.
  4. Return typed objects so the rest of the application does not parse JSON.
  5. Define an exception class for API failures.
  6. Bind the class in the container, or instantiate it in a factory for plain PHP.

A single class keeps the key in one place and makes the flow easy to mock in tests.

Ordering a Number

The order call takes the country, the service and optionally the number type. Send JSON and check the HTTP status before reading the body.

  • 200: number ready; read the identifier and the number.
  • 202: provisioning; poll the status endpoint.
  • 402: insufficient balance; stop and top up.
  • 429: rate limited; back off before retrying.

Store the identifier in your database before continuing. A process that orders and then crashes without saving the identifier has spent money for nothing.

Polling for the Code

Polling is a loop with three exit conditions: a message arrived, the budget expired or the number is gone. Write it explicitly rather than with a fixed sleep.

  1. Set a maximum number of attempts and a delay between them.
  2. Fetch the message endpoint with the identifier.
  3. If the response is empty, sleep and try again.
  4. If a message is present, break and parse it.
  5. If attempts run out, throw an exception with the identifier attached.

Two to three seconds between attempts with a ninety-second budget is a sensible default.

Parsing the Code Reliably

A generic regex will match dates and order numbers in the message footer. Write a pattern per service, anchored to the verification phrase, and validate the length. Hash the code if you need to compare later, and never log the plain value.

Mark the code consumed on success so a replay inside the expiry window fails.

Releasing the Number

Send the release request in a finally block so it runs whether the flow succeeded or threw. Abandoned numbers leak inventory and skew reporting, and on a shared team account the leak is invisible until the invoice arrives.

Releasing is also good testing hygiene. A released number cannot receive a stray message from a later run.

Error Handling

SituationWhat to logWhat to do
Empty inbox after budgetIdentifier, attempts, timestampsThrow a not-ready error and offer a reorder
402 responseIdentifier, endpointTop up and retry once
404 responseIdentifier, timestampOrder a new number
Connection errorEndpoint, error typeRetry with backoff within budget

Testing and Running It

Write three tests first: a missing key throws the typed exception, an empty inbox returns a not-ready state, and a message with a six-digit code produces the expected value. Then run one real flow in staging with an account from the SMS Feel registration page and confirm the output against the SMS Feel developer documentation.

Check the wallet balance before a long run. A top-up mid-test turns a five-minute job into an afternoon.

Frequently Asked Questions

Can I use this class in Laravel?

Yes. Bind it in a service provider and inject it into controllers or jobs. The class itself does not depend on the framework.

How do I avoid duplicate orders after a timeout?

Send an idempotency key per order intent and reuse it across retries. A repeated request returns the original result instead of ordering again.

Why does the message endpoint return nothing for a while?

Either the sending service is slow or the number range is filtered. Check the order status, then try a different country.

Is the same class usable for temporary email?

The shape is similar, but the endpoints and fields differ. Read the documentation for the email flow before reusing the code.

What if the release call fails?

Log the failure with the identifier and retry once. Persistent release failures mean inventory is leaking and support should be informed.

Conclusion

A complete OTP verification API in PHP example is four calls and a disciplined loop: order, poll, parse, release. Put the release in a finally block, keep the parsing specific and log identifiers rather than codes. To run it against real numbers, create a free SMS Feel account and start with the API documentation.

Related reading: more PHP integration guides on the SMS Feel blog.

Tags

#otp verification api in php #php otp api #order number #poll for code #api integration #api #docs
Contact us