AI in PHP
A language model can be built right into your own website – for example to summarize a text or suggest a caption. An example of sending a request to the Claude API from PHP with cURL, without other libraries. The description of the API is as of October 2026.
As soon as a website calls an AI API, every request costs money and a new kind of user input appears. How to protect the API key, keep your credit from being drained, handle outages and prevent misuse of the model.
Every API answers with an error now and then: an exceeded limit, an overloaded server, a dropped connection. Which errors are temporary, which are not, and how to repeat a request safely in PHP so that the website does not wait forever. The description of the API is as of October 2026.
A longer answer from a language model takes tens of seconds. With streaming, the text appears on the page gradually as the model writes it, and the visitor does not wait in front of an empty screen. How to read the data stream from the Claude API in PHP and pass it on to the browser. The description of the API is as of October 2026.
When a program is to process the result – save it to a database, sort it into a category, fill in a form –, you need data in a fixed shape instead of free text. The Claude API can return JSON exactly according to a given schema. The description of the API is as of October 2026.
When every request starts the same way – long instructions, a price list, a product manual –, you pay for the same text over and over again. Prompt caching stores the repeated beginning in a cache, and later requests read it much more cheaply and quickly. The information is as of October 2026.
A language model does not know the status of an order or what is on offer today. Through tools (tool use), however, you can let it ask for the data – the model decides which function to call with which parameters, your PHP runs it and returns the result. The description of the API is as of October 2026.
Current Claude models also read images and PDFs – they describe a photo, read the text of a scanned invoice or explain a chart. How to send a file from PHP, how to resize it beforehand and what to watch out for. The description of the API and the limits are as of October 2026.
Descriptions for thousands of products, tags for old articles, translating a whole website – tasks whose result nobody is waiting for in front of the screen. The Batch API processes them all at once at half the usual price. How to send, watch and collect a batch in PHP. The description of the API is as of October 2026.