大家好,我是PHP小志。
给大家推荐一个 专为 PHP + OpenAI API(v1/chat/completions)+ 技术写作 / SEO 内容类文章生成 的完整对接模板。
🧩 一、整体思路
在 PHP 中通过 curl
或 Guzzle
调用 https://api.openai.com/v1/chat/completions
接口,
并传入以下 3 层提示词结构:
🔹 1. System(系统角色指令)
定义模型身份、任务类型、输出格式等。
例如:
You are a professional SEO content strategist and technical copywriter.
Write detailed, human-like English articles with strong structure and inline HTML styling.
Do not include <html>, <head>, or <body> tags.
🔹 2. User(用户输入提示)
你可以传入一个变量,如 $topic
,用于生成不同主题的文章。
示例:
Write an SEO-optimized technical article about “$topic”.
The article must start with <div> and use inline CSS for every element.
Include sections, TL;DR summary, use-cases, FAQs, and a conclusion.
🔹 3. Assistant(输出)
由模型生成,内容为完整 HTML 文章。
🧰 二、PHP 接口模板(可直接运行)
<?php
$apiKey = 'YOUR_OPENAI_API_KEY';
$topic = 'Kid outdoor pants'; // 你可以改成任意技术或SEO主题
$data = [
"model" => "gpt-4o-mini", // 可换 gpt-4o 或 gpt-5
"messages" => [
[
"role" => "system",
"content" => "You are a professional SEO content strategist and technical copywriter. Write detailed, structured English HTML articles for technical and B2B readers. Use inline CSS only, start with <div>, no <html> or <body> tags."
],
[
"role" => "user",
"content" => "Write a full SEO article about '$topic'. Include TL;DR summary, introduction, technical explanation, use cases, pros & cons, and FAQ. Each section ≥100 words. Use inline CSS."
]
],
"temperature" => 0.7,
"max_tokens" => 3000,
"top_p" => 1.0,
];
$ch = curl_init('https://api.openai.com/v1/chat/completions');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Authorization: Bearer ' . $apiKey,
]);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
$response = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
$result = json_decode($response, true);
echo $result['choices'][0]['message']['content'];
🧠 三、提示词模板(可批量复用)
可以在程序中拼接变量,实现自动化 SEO 内容生成:
$prompt = <<<PROMPT
You are a professional technical content strategist.
Write an SEO-optimized HTML article about "$topic".
Rules:
- Start with <div>, use inline CSS on every element.
- Minimum 1500 words.
- Include TL;DR summary, introduction, feature highlights, technical specs, use scenarios, pros & cons, and FAQs.
- Write in natural, engaging tone suitable for engineers and B2B readers.
PROMPT;
⚙️ 四、可选增强项
功能 | 方法 | 示例 |
---|---|---|
换模型 | "model" => "gpt-4o" | 更高质量输出 |
结构化输出 | 加上 "response_format" => ["type" => "json_object"] | 输出 JSON |
函数调用(工具调用) | 使用 "tools" 字段 | 用于抓取关键词或生成摘要 |
自动换主题 | $topic 循环 | 批量生成内容 |
异常处理 | 捕获 429/500 状态码 | 防止限流 |
🪄 五、可直接用的 Prompt 模板(SEO内容类)
下面是一个你可在 PHP 中直接复用的高效模板:
$topic = "LED industrial lighting systems";
$messages = [
[
"role" => "system",
"content" => "You are an expert B2B SEO writer. Write high-quality HTML articles with inline CSS for WordPress editors."
],
[
"role" => "user",
"content" => "
Write a professional SEO article about '$topic'.
Requirements:
- Start with <div>.
- Use inline CSS for a modern clean style.
- Add TL;DR, introduction, key features, technical explanation, benefits, applications, buying guide, and FAQ.
- Target keyword must appear at least 5 times.
- Tone: informative, trustworthy, and engaging.
- No <html>, <head>, or <body> tags."
]
];
以上这是最常见的SEO自动化用法。
但是要更好的适配百度的富文本编辑器格式,提示词还可以按照以下的方式修改,首先看百度富文本的特点
🧩 一、百度富文本的特点
支持标准 HTML + 内联样式;
禁止
<html>
,<head>
,<body>
;不支持复杂 CSS 选择器;
建议使用
<p>
,<h2>
,<ul>
,<ol>
,<table>
,<img>
等常用标签;避免
<section>
、<article>
等不兼容标签;图片或超链接请使用标准
<a>
<img>
语法。
✅ 二、优化后的 PHP 接口模板(可直接运行)
🧠 三、生成效果说明
输出内容示例(模型生成后结构如下):
🔖 四、TDK(自动生成规则)
可以让模型在内容最前方自动输出 HTML 注释形式的 TDK:
这些注释不会被百度编辑器展示出来,但能被程序读取,
你可以在 PHP 中正则提取出来,存入数据库或 <meta>
标签中。
示例提取逻辑:
💡 五、如果你想增强输出质量
可以在提示词中加入:
发表评论 取消回复