Alexander Mirvis

OpenAI Cook-book

Beyond ChatGPT: Secret and Surprisingly Powerful Things You Can Build With the OpenAI API

When most people think about the OpenAI API, they imagine a text box where somebody types a question and artificial intelligence sends back an answer. That is technically correct, but it barely scratches the surface of what the API can actually do.

I use OpenAI differently on AlexanderMirvis.com and BrooklynNotaryNinjas.com. Instead of treating artificial intelligence as a separate chatbot sitting in the corner of a website, I can make it part of the actual software. OpenAI can examine webpages before publication, generate metadata, analyze uploaded documents, assist with medical and legal research, power customer-service conversations, automate repetitive work, monitor servers, analyze cybersecurity logs, transcribe telephone calls, generate spoken responses, search private document collections and communicate with other parts of my applications through controlled functions.

The real trick is understanding that the OpenAI API does not have to be the application. It can be the intelligence inside the application.

A traditional PHP application might contain logic such as:

if ($documentType === 'affidavit') {
    // Do something
}

An AI-enabled application can go much further. I can give OpenAI an uploaded document and ask it to identify the document type, extract important dates, recognize the people involved, summarize what happened, identify missing information and return everything as structured data that PHP can process.

This changes OpenAI from a text generator into something much closer to an intelligent software component.

Building One Central OpenAI Function

For PHP applications, I prefer having one central server-side function that communicates with the OpenAI API. The rest of the website can then call that function whenever artificial intelligence is needed.

The API key should always remain on the server. It should never be placed inside JavaScript that is sent to a visitor's browser.

<?php

function openaiResponse(array $payload): array
{
    $apiKey = getenv('OPENAI_API_KEY');

    if (!$apiKey) {
        throw new RuntimeException('OPENAI_API_KEY is not configured.');
    }

    $ch = curl_init('https://api.openai.com/v1/responses');

    curl_setopt_array($ch, [
        CURLOPT_POST => true,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_HTTPHEADER => [
            'Authorization: Bearer ' . $apiKey,
            'Content-Type: application/json'
        ],
        CURLOPT_POSTFIELDS => json_encode($payload),
        CURLOPT_TIMEOUT => 120
    ]);

    $raw = curl_exec($ch);

    if ($raw === false) {
        throw new RuntimeException(curl_error($ch));
    }

    $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    curl_close($ch);

    $data = json_decode($raw, true);

    if ($status >= 400) {
        throw new RuntimeException(
            $data['error']['message'] ?? 'OpenAI request failed.'
        );
    }

    return $data;
}

A basic request can then look like this:

$response = openaiResponse([
    'model' => 'gpt-5',
    'input' => 'Explain what a New York notarial acknowledgment is.'
]);

echo $response['output'][0]['content'][0]['text'] ?? '';

The exact model can be changed depending on the task, desired speed, reasoning requirements and cost. The important architectural decision is that everything goes through my server. The browser talks to my PHP application, my application decides what is allowed, and only then does the server communicate with OpenAI.

This gives me control over authentication, rate limits, security, logging, permissions, billing and exactly what information the model is allowed to access.

Using OpenAI to Generate Titles, Metadata and Analyze Every Page

One of the most useful OpenAI integrations on AlexanderMirvis.com is automatic content and SEO analysis. Instead of manually writing a title tag, meta description, keyword list and search description every time I publish something, OpenAI can examine the actual article and propose them automatically.

The important trick is that I do not necessarily want OpenAI to answer me conversationally. I want my CMS to receive data that it can store directly in the database.

For example, I may want a response that looks like this:

{
    "title": "Suggested Page Title",
    "meta_description": "Suggested description",
    "keywords": [
        "keyword one",
        "keyword two"
    ],
    "seo_score": 84,
    "problems": [
        "Meta description is too vague"
    ],
    "recommendations": [
        "Add a stronger introductory paragraph"
    ]
}