Calling an AI API From PHP With cURL

By Darus S Alfatah · 2025-09-12 · Tutorial · 8 menit

Calling an AI API From PHP With cURL

Author note: In PHP, cURL often forgets to raise the timeout for large models; the default is too short.

Focus of this article

This article is native PHP only: cURL and Guzzle, without a framework. For the Laravel version, see the companion article.

Read also: Integrating an AI API in Laravel

Why cURL

PHP ships with cURL almost everywhere, including shared hosting where Composer may be unavailable. It is not the most elegant option, but it is the most portable one, and for a single endpoint it is entirely sufficient.

The important parts are not the request itself but the handling around it: timeouts, error checking, and logging.

Structure Your Code

Put configuration in one place, the HTTP call in another, and business logic in a third. When the key or base URL changes, you edit one file instead of searching the codebase for scattered strings.

Wrap the call in a function that accepts a prompt and returns text. Every feature then uses the same function, and improvements such as retries or caching benefit everything at once.

Timeouts Matter

Shared hosting often kills requests after thirty seconds. Set a shorter timeout for the API call and decide what to show the user when it is exceeded. A clear message is better than a blank page.

For long answers, either stream or move the work to a background job. Users will not wait a minute staring at a spinner.

Error Handling

Check the HTTP status before decoding the body, and treat a network failure differently from an invalid request. Log the status and a short message, never the full key or full customer data.

Return controlled errors to the interface. Technical traces help attackers more than they help users.

Caching

Many applications receive the same questions repeatedly. Hash the prompt, store the answer with an expiry, and skip the API call when a cache hit occurs. This is the simplest cost reduction available in PHP.

Choose an expiry that matches how quickly your facts change; a price list needs hours, a policy document may need days.

Deployment Notes

Store the key outside the web root or in an environment variable provided by the host. Ensure outbound HTTPS is allowed, because some shared hosts block external requests by default.

Keep a log file with rotation enabled. An unbounded log will eventually fill the disk and take the site down.

Frequently Asked Questions

Do I need Composer? No, though a client library makes code cleaner if available.

How do I stream in PHP? Use a callback with CURLOPT_WRITEFUNCTION and flush output.

Is cURL slower? The difference is negligible compared with model latency.

What about retries? Retry on timeouts and 5xx, at most twice.

Can I use it in WordPress? Yes, though the built-in HTTP API is more idiomatic.

Conclusion

cURL is enough for a robust integration when you wrap it well: configuration in one place, timeouts, error handling, and caching.

Monitoring on a Small Host

Write one log line per call: timestamp, feature, duration, status, and token count. A simple log viewer is enough to spot patterns, and it answers the question every owner eventually asks: which feature is actually worth paying for.

Set a daily budget check that runs from cron and emails you when usage crosses a threshold. Cheap insurance against a runaway loop overnight.

An example of an adequate timeout: Large models can take dozens of seconds. Set CURLOPT_TIMEOUT high enough, say sixty seconds, so it does not cut off mid-way.

Frequently Asked Questions

Can beginners follow this tutorial?

Yes. The steps are explained in order with code examples, suitable for beginners and experienced developers alike.

Is BankAI’s endpoint compatible with the library I already use?

Yes, BankAI is 100% OpenAI-compatible, so libraries such as the OpenAI SDK and LangChain work without code changes beyond the Base URL and API key.

Where do I get an API key to follow this tutorial?

You get the API key (and Secret Token) after registering and completing payment in the BankAI dashboard.

Sources & References

About the author

Darus S Alfatah — Founder & Writer

Darus S Alfatah is the founder of BankAI. He builds and operates the LLM API gateway that serves Indonesian developers, and writes about LLM integration, gateway architecture, and using AI practically and cost-effectively. Every guide is tested in a real environment — including code, configuration, and token calculations — before it is published, and updated when provider documentation or pricing changes.

Editorial policy

Related articles

🚀 Ready to Use the All-in-One API Key?

Sign up, top up with QRIS, and start building with ChatGPT, Claude, DeepSeek and every other model through one key.

🔑 SIGN UP NOW · Pricing