AI development tools

Connect an AI-designed search interface to a PHP JSON endpoint

Give a generated interface a stable response shape and test its empty state with one PHP file.

Agree on the response before drawing the page

A small catalog can start with one query parameter, q, and one JSON object containing an items array. Define the empty result as an empty array. This gives the frontend a reliable condition for its “no matching programs” message.

Create a local endpoint

Save the following as search.php in a new local project. The two records are example input for this exercise. The function performs a case-insensitive match and reindexes the result before encoding it.

<?php
$items = [["name" => "Atlas"], ["name" => "Compass"]];
$q = is_string($_GET["q"] ?? null) ? trim($_GET["q"]) : "";
$matches = array_filter($items, static function (array $item) use ($q): bool {
    return $q === "" || stripos($item["name"], $q) !== false;
});
header("Content-Type: application/json; charset=utf-8");
echo json_encode(["items" => array_values($matches)], JSON_THROW_ON_ERROR);

Check three requests

An empty query should return both items. A query of COMP should return Compass. A query of banana should return an object with an empty items array. The response header should identify JSON.

Hand the interface builder a precise task

Ask the builder to send the visitor’s query to this endpoint and render four states: idle, loading, results and failure. Render item names as text. Check the empty array and a failed request separately so a service failure cannot be mistaken for a valid empty search.

Method & sample

Original worked example, informed by the linked reference.

Discussion

Sign in to participate.