OpenAI grants us the feature to Generate SEO Meta Titles and Descriptions Automatically. We may presume that we deal with the versions offered by OpenAI, such as GPT-3.5 or GPT-4.0, which use its GPT API.
The functionality of generating SEO meta titles and descriptions Automatically Using OpenAI and PHP.
But has a few requirements, including but not limited to: PHP 7 or above version, OpenAI API Key, and Curl enabled on your PHP.
This folder structure should be maintain easy because we need to create one root folder and name it “Seo-generator”.
Then within this folder we have to create 3 files like index.php file to take input of the form, generate-seo.php file will be a Backend APl Call to Open AI.
Lastly we need to create a file called .env file where the Open AI API key stores.

Checkout our latest Blog on how to speed up Development using AI Tools. Visit Now
Create a .env file to generate SEO Meta Titles and Descriptions Automatically Using OpenAI + PHP.
The .env file is the file where we will be keeping our OpenAI API Key because it will be kept secure here. The example key, like we have to integrate ours, is as follows –
OPENAI_API_KEY=your-openai-secret-key-here
We need to insert our API Key from the OpenAI dashboard, and we need to keep our key secret from everyone.
Form UI to display the Content or Data
We have to create an index.html form where there is a section of a textarea along with a button to generate the same. The code for the same is as follows –
<!DOCTYPE html> <html> <head> <title>SEO Meta Generator</title> </head> <body> <h2>Generate SEO Meta Title & Description</h2> <form method="POST" action="generate-seo.php"> <label>Page Content:</label><br> <textarea name="content" rows="10" cols="80" required></textarea><br><br> <input type="submit" value="Generate SEO Meta"> </form> </body> </html>

Generating PHP Logic to Generate SEO Meta Titles and Descriptions Automatically Using OpenAI + PHP
We have to create a PHP file and name it generate-seo.php, where we have to write the logic to generate SEO Meta Titles and descriptions automatically using OpenAI + PHP.
The code for the same is as follows –
<?php
// Load API key from .env file
$env = parse_ini_file('.env');
$apiKey = $env['OPENAI_API_KEY'] ?? '';
if (!$apiKey) {
die("OpenAI API key not found.");
}
// Collect user input
$content = $_POST['content'] ?? '';
if (!$content) {
die("Content is required.");
}
// Prepare prompt
$prompt = "Given the following content, generate an SEO-friendly title and meta description:\n\n"
. "Content:\n" . $content . "\n\n"
. "Output in JSON format with keys 'title' and 'description'.";
// Call OpenAI API
$data = [
'model' => 'gpt-3.5-turbo',
'messages' => [
['role' => 'user', 'content' => $prompt]
],
'temperature' => 0.7
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.openai.com/v1/chat/completions');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Authorization: Bearer ' . $apiKey
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
$response = curl_exec($ch);
curl_close($ch);
$result = json_decode($response, true);
$aiContent = $result['choices'][0]['message']['content'] ?? 'No response from OpenAI.';
// Parse and display result
$seo = json_decode($aiContent, true);
?>
<!DOCTYPE html>
<html>
<head>
<title>SEO Result</title>
</head>
<body>
<h2>Generated SEO Title & Description</h2>
<p><strong>Title:</strong> <?= htmlspecialchars($seo['title'] ?? 'Error') ?></p>
<p><strong>Description:</strong> <?= htmlspecialchars($seo['description'] ?? 'Error') ?></p>
<hr>
<a href="index.php">🔙 Generate Again</a>
</body>
</html>There are several tasks achieved via the code, such as – Loading API key, Collecting User Input, Preparing Prompt, Calling OpenAI API, and Parsing and Displaying Result.

Conclusion
To sum up, it is possible to conclude that the automatic creation of meta Titles is a fantastic way to save time and leave the rest of the work to AI.
There are, however, some limitations that we should discuss as follows: we do not need to share our API Key publicly.
Add .env to. gitignore in case of using version control, and we should implement rate limiting in the event of sharing publicly.
Get your tailor-made Website within one year of validity of hosting and domain names worth only Rs. 5,499. Make contact with us.




