大家好,我是PHP小志。
给大家推荐一个 专为 PHP + OpenAI API(v1/chat/completions)+ 技术写作 / SEO 内容类文章生成 的完整对接模板。


🧩 一、整体思路

在 PHP 中通过 curlGuzzle 调用 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 接口模板(可直接运行)

<?php

$apiKey = 'YOUR_OPENAI_API_KEY';

$topic = 'LED industrial lighting systems';

// SEO主题,可改成变量

$data = [ "model" => "gpt-4o-mini",

// 也可换 gpt-4o / gpt-5

"messages" => [

[

"role" => "system",

"content" => "You are a professional SEO content strategist and technical writer. Write a long, structured, SEO-optimized HTML article for Baidu Rich Text Editor (UEditor compatible). Use only <p>, <h2>, <h3>, <ul>, <ol>, <table>, and <img> tags with inline CSS. Avoid <section>, <div> nesting, and no <html>, <head>, or <body> tags."

Write a long, structured, SEO-optimized HTML article for Baidu Rich Text Editor (UEditor compatible). Use only <p>, <h2>, <h3>, <ul>, <ol>, <table>, and <img> tags with inline CSS. Avoid <section>, <div> nesting, and no <html>, <head>, or <body> tags." ],

[

"role" => "user",

"content" => " Write a full Chinese-English bilingual SEO article about '

Write a full Chinese-English bilingual SEO article about '$topic'. Requirements: - Use clear headings (<h2>、<h3>) for each section. - Each section ≥100 words. - Include: TL;DR, Introduction, Features, Technical Explanation, Applications, Buying Guide, and FAQ. - Inline CSS style must be simple, e.g. style='margin:10px 0;font-size:15px;line-height:1.7;'. - Output TDK metadata (Title, Description, Keywords) at the top as plain text comments. Example: <!-- Title: xxx --> <!-- Description: xxx --> <!-- Keywords: xxx --> " ]

],

"temperature" => 0.7,

"max_tokens" => 3500,

"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'];


🧠 三、生成效果说明

输出内容示例(模型生成后结构如下):

<!-- Title: LED Industrial Lighting Systems | Factory Efficiency and Safety -->

<!-- Description: Discover how LED industrial lighting systems improve energy efficiency, safety, and workplace productivity. Suitable for warehouses, factories, and large facilities. -->

<!-- Keywords: LED industrial lighting, factory lights, warehouse illumination, energy-saving lights -->

<h2 style="margin:10px 0;font-size:20px;">TL;DR 简要概述</h2>

<p style="margin:10px 0;font-size:15px;line-height:1.7;">

LED工业照明系统为工厂、仓库提供节能与高亮解决方案。本文介绍其原理、优势与应用场景。</p>

<h2 style="margin:10px 0;font-size:20px;">Introduction 介绍</h2><p style="margin:10px 0;font-size:15px;line-height:1.7;">

LED industrial lighting systems are designed to deliver consistent illumination for industrial environments...</p>

<h2 style="margin:10px 0;font-size:20px;">Key Features 主要特点</h2>

<ul style="margin:10px 0;font-size:15px;line-height:1.7;">

<li>High luminous efficiency</li>

<li>Long lifespan (over 50,000 hours)</li>

<li>Dust and moisture resistant</li>

</ul>

<h2 style="margin:10px 0;font-size:20px;">Technical Explanation 技术原理</h2>

<p style="margin:10px 0;font-size:15px;line-height:1.7;">LED drivers convert AC to DC...</p>

<h2 style="margin:10px 0;font-size:20px;">Applications 应用场景</h2>

<ul style="margin:10px 0;font-size:15px;line-height:1.7;">

<li>Warehouses</li>

<li>Manufacturing workshops</li>

<li>Parking areas</li>

</ul>

<h2 style="margin:10px 0;font-size:20px;">Buying Guide 采购指南</h2>

<p style="margin:10px 0;font-size:15px;line-height:1.7;">When choosing LED industrial lights, consider lumen output...</p>

<h2 style="margin:10px 0;font-size:20px;">FAQ 常见问题</h2>

<h3 style="margin:10px 0;font-size:17px;">1. How long do LED factory lights last?</h3>

<p style="margin:10px 0;font-size:15px;line-height:1.7;">Typically between 50,000 and 100,000 hours...</p>


🔖 四、TDK(自动生成规则)

可以让模型在内容最前方自动输出 HTML 注释形式的 TDK:

<!-- Title: {{自动生成标题}} --><!-- Description: {{一句简短描述,约150字以内}} --><!-- Keywords: {{核心关键词, 3–8个}} -->

这些注释不会被百度编辑器展示出来,但能被程序读取
你可以在 PHP 中正则提取出来,存入数据库或 <meta> 标签中。

示例提取逻辑:

preg_match('/<!-- Title:(.*?) -->/i', $content, $title);

preg_match('/<!-- Description:(.*?) -->/i', $content, $desc);

preg_match('/<!-- Keywords:(.*?) -->/i', $content, $keywords);


💡 五、如果你想增强输出质量

可以在提示词中加入:

Write in fluent, native-level English. Add a natural mix of Chinese and English for bilingual SEO. Focus on B2B tone, product advantages, and practical applications. Ensure every section is at least 120 words.



点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论
立即
投稿
发表
评论
返回
顶部