z_appendix_101:php_api_call_for_the_ai_era

17. A Simple API Call — Modern HTTP in PHP

— how to talk to another system without complexity

At some point,
our application needs to ask another system a question:

  • “What’s the weather?”
  • “Is this email valid?”
  • “Give me the latest exchange rate.”
  • “Send this message.”

These are API calls.

And just like database queries and routing,
the shape is simple:

Prepare the request.
Send it.
Decode the response
.

Everything else
— clients, middleware, retries, authentication —
is optional.

The goal of this page is to give us the mental model that lets us collaborate with AI on any HTTP request, without needing to memorize libraries.


1. The Shape of a Modern HTTP Request

In modern PHP, the simplest safe way to make an HTTP request is with a small, focused client like Symfony’s HttpClient or Guzzle.

The shape looks like this:

$response = $client->request('GET', 'https://api.example.com/data');
 
$data = $response->toArray();

Two steps:

  1. Send the request
  2. Decode the response

That’s the whole mental model.


2. Why This Matters

AI can generate HTTP code instantly
— but without guidance, it may:

  • use outdated cURL boilerplate
  • skip error handling
  • forget timeouts
  • ignore JSON decoding
  • mix concerns
  • over‑engineer the client

Our literacy lets us steer the conversation:

“Use a modern HTTP client.”
“Decode JSON into an array.”
“Keep the request simple.”
“Handle errors gently.”

We’re not memorizing APIs
— we’re shaping intention.


3. A Gentle Example

Let’s say we want to fetch weather data.

We ask AI:

“Give me a simple function that fetches weather data 
from an API using Symfony HttpClient.”

AI should give us something like:

use Symfony\Component\HttpClient\HttpClient;
 
function fetchWeather(string $city): array {
    $client = HttpClient::create();
 
    $response = $client->request(
        'GET',
        'https://api.example.com/weather?city=' . urlencode($city)
    );
 
    return $response->toArray();
}

This is the entire shape:

  • create a client
  • send a request
  • decode the response

Everything else is refinement.


4. Giving API Calls a Home

As soon as we make more than one API call,
we feel the friction:

  • repeated URLs
  • repeated headers
  • repeated decoding
  • repeated error handling

This is the moment when API access becomes a concept,
so we give it a home:

class WeatherApi {
    public function __construct(private HttpClientInterface $client) {}
 
    public function fetch(string $city): array {
        $response = $this->client->request(
            'GET',
            'https://api.example.com/weather',
            ['query' => ['city' => $city]]
        );
 
        return $response->toArray();
    }
}

Now our domain reads like a story:

$weather = $weatherApi->fetch('Lisbon');

That’s the whole point.


5. Error Handling — The Calm Version

A modern API call should fail gently:

try {
    $data = $response->toArray();
} catch (\Throwable $e) {
    // log, fallback, or return a safe default
    return ['error' => 'Service unavailable'];
}

We don’t need a full retry system.
We don’t need circuit breakers.
We don’t need middleware.

We just need a calm fallback.


6. Why This Matters in the AI Era

AI can generate:

  • cURL boilerplate
  • 50‑line Guzzle configs
  • unnecessary abstractions
  • full-blown SDKs
  • over‑engineered clients

But we don’t need any of that.

We need the shape:

  • Request → Response → Decode → Use

Once we hold that shape, we can guide AI:

“Make this simpler.”
“Use query parameters instead of string concatenation.”
“Decode JSON into an array.”
“Move this into a dedicated API class.”
“Handle errors gently.”

We’re not fighting complexity — we’re preventing it.


7. When to Ask AI for a Simple API Call

The moment when we need data from another system,
not a framework

Here’s what that moment looks like.

We may start with:

“Fetch weather data from this API.”

AI may give us:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, ...);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
...

We feel the noise.
We feel the outdated approach.

This is the moment to say:

“Use Symfony HttpClient (or Guzzle).
Give me a simple, modern API call.”

AI will correct itself.

Then, when we ask for more API calls and the code starts repeating, we say:

“Move all API logic into a dedicated class.”

This is the literacy we’re learning:

When we need one call, we ask for a simple request.
When we need many, we ask for an API client class.


8. The Mental Model in One Sentence

An API call is a small conversation with another system:
send, decode, use.

Keep it simple. Keep it modern. Keep it safe.

Once we see this shape, we can collaborate with AI on any HTTP integration
— from a tiny script to a full application —
without losing clarity.



Tony de Araujo —New York


z_appendix_101/php_api_call_for_the_ai_era.txt · Last modified: by editor