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
- Create a class with a constructor that accepts the HTTP client and configuration.
- Add one method per endpoint: order, fetch, release.
- Read the API key and base URL from configuration, never from a constant.
- Return typed objects so the rest of the application does not parse JSON.
- Define an exception class for API failures.
- 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.
- Set a maximum number of attempts and a delay between them.
- Fetch the message endpoint with the identifier.
- If the response is empty, sleep and try again.
- If a message is present, break and parse it.
- 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
| Situation | What to log | What to do |
|---|---|---|
| Empty inbox after budget | Identifier, attempts, timestamps | Throw a not-ready error and offer a reorder |
| 402 response | Identifier, endpoint | Top up and retry once |
| 404 response | Identifier, timestamp | Order a new number |
| Connection error | Endpoint, error type | Retry 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.