Automate User Feedback Classification with a Large‑Model API in PHP
This guide shows how to use the Tongyi Qianwen large‑model API with PHP to automatically classify user feedback into predefined categories, eliminating manual analysis and complex NLP development while providing clear steps, code, and result interpretation for rapid business insights.
Introduction
With the rapid advancement of large‑model technology, developers can fulfill complex business needs such as text classification and sentiment analysis through simple API calls. This article walks through a practical scenario where the Tongyi Qianwen API is used to automatically classify user feedback.
Business Scenario
Imagine you are the technical lead of a smartphone manufacturer and need to analyze a batch of user feedback, categorizing each comment into one of four reasons: price too high, insufficient after‑sales support, poor product experience, or other. The goal is to compute the proportion of each category for targeted product improvements.
Implementation Steps
Step 1: Obtain API Key
Register on the Tongyi Qianwen platform (or any OpenAI‑compatible service), log in, and generate a personal API key in the console.
Note: For security, store the API key in an environment variable rather than hard‑coding it; for quick personal testing you may skip this step.
Step 2: Code Implementation
The following PHP script calls the API, sends each feedback string, and extracts the classification result.
<?php
/** @desc 大模型编程0X01〡调用 API 完成内容分类 */
declare(strict_types=1);
// Set API Key and endpoint
$api_key = "sk-xxxxxxxxxxxxxxxxxxxx"; // replace with your key
$base_url = "https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions";
// Sample feedback array
$userFeedbacks = [
"这个手机太贵了,对于我这样的普通消费者不太友好。",
"客服总是找不到,售后支持根本不给力。",
"使用起来卡顿,体验很不好。",
"包装有点损坏,不过可以接受。"
];
function classify_feedback(string $feedback): string {
global $api_key, $base_url;
$data = [
"model" => "qwen-plus",
"messages" => [
["role" => "system", "content" => "You are a helpful assistant for classifying user feedback."],
["role" => "user", "content" => "请将以下用户反馈按原因分类:价格过高、售后支持不足、产品使用体验不佳、其他。反馈内容:{$feedback}
回答格式:分类结果:"]
],
"temperature" => 0
];
$ch = curl_init($base_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Authorization: Bearer $api_key",
"Content-Type: application/json"
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
$response = curl_exec($ch);
if ($response === false) {
$error = curl_error($ch);
curl_close($ch);
return "Error: cURL error - $error";
}
$response_data = json_decode($response, true);
curl_close($ch);
if (isset($response_data['choices'][0]['message']['content'])) {
return trim($response_data['choices'][0]['message']['content']);
} else {
return "Error: Failed to classify feedback";
}
}
foreach ($userFeedbacks as $feedback) {
$classification = classify_feedback($feedback);
echo "用户反馈: $feedback
$classification
";
}
?>Step 3: Run and View Results
Executing the script yields output similar to the following:
用户反馈: 价格太高了,感觉性价比不高。
分类结果:价格过高
用户反馈: 售后服务响应太慢,联系不上客服。
分类结果:售后支持不足
用户反馈: 手机用着卡顿,体验很差。
分类结果:产品使用体验不佳
用户反馈: 包装有点简陋,但功能还行。
分类结果:其他Step 4: Result Analysis
With the classification results you can easily compute the share of each category. For example, assuming 100 feedback items, you might obtain:
Price too high: 30%
Insufficient after‑sales support: 25%
Poor product experience: 35%
Other: 10%
These percentages help the product team prioritize improvements, such as focusing on the user experience which shows the highest complaint rate.
Conclusion
By calling the Tongyi Qianwen API with a few lines of PHP, developers can automate feedback classification without building custom NLP models, dramatically reducing development effort while delivering actionable business insights. The approach is suitable for both beginners and seasoned engineers looking to leverage large‑model capabilities.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Tech Hub
Sharing cutting-edge internet technologies and practical AI resources.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
