pangu-user-platform/scripts/test/openapi-student-list-test.php

211 lines
7.0 KiB
PHP
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env php
<?php
/**
* 开放API - 学生列表查询测试PHP 版本)
*
* 功能演示:
* 1. 调用学生列表接口
* 2. 自动计算签名
* 3. 处理响应数据
* 4. 展示脱敏后的数据
*
* 使用方法:
* php openapi-student-list-test.php <appCode> <appSecret> [baseUrl]
*
* 示例:
* php openapi-student-list-test.php YY000001 2221c3d6f00c4baabfabfd4f679693f1
* php openapi-student-list-test.php YY000001 2221c3d6f00c4baabfabfd4f679693f1 http://localhost:8080
*
* @author pangu
* @date 2026-02-04
*/
require_once __DIR__ . '/OpenApiClient.php';
// ============================================================
// 参数解析
// ============================================================
if ($argc < 3) {
echo "使用方法: php openapi-student-list-test.php <appCode> <appSecret> [baseUrl]\n";
echo "\n";
echo "参数说明:\n";
echo " appCode 应用编码(必填)\n";
echo " appSecret 应用密钥(必填)\n";
echo " baseUrl API 基础地址(可选,默认 http://localhost:8080\n";
echo "\n";
echo "示例:\n";
echo " php openapi-student-list-test.php YY000001 2221c3d6f00c4baabfabfd4f679693f1\n";
echo " php openapi-student-list-test.php YY000001 2221c3d6f00c4baabfabfd4f679693f1 http://your-domain.com:8080\n";
exit(1);
}
$appCode = $argv[1];
$appSecret = $argv[2];
$baseUrl = $argv[3] ?? 'http://localhost:8080';
// ============================================================
// 创建客户端(开启调试模式)
// ============================================================
$client = OpenApiClient::create($baseUrl, $appCode, $appSecret, true);
// ============================================================
// 示例 1: 分页查询学生列表
// ============================================================
echo "\n";
echo "【示例 1】分页查询学生列表\n";
echo "========================================\n";
try {
$response = $client->get('/open/api/student/list', [
'pageNum' => 1,
'pageSize' => 10
]);
// 检查响应状态
if ($response['code'] === 200) {
echo "✅ 调用成功!\n\n";
// 展示分页信息
echo "【分页信息】\n";
echo " 总记录数: {$response['total']}\n";
echo " 当前页数据量: " . count($response['rows']) . "\n\n";
// 展示学生数据
if (!empty($response['rows'])) {
echo "【学生列表】(已脱敏)\n";
echo str_repeat('-', 100) . "\n";
printf("%-10s %-15s %-15s %-10s %-20s %-15s\n",
'ID', '学号', '姓名', '性别', '学校', '年级班级');
echo str_repeat('-', 100) . "\n";
foreach ($response['rows'] as $student) {
$gender = $student['gender'] === '1' ? '男' : ($student['gender'] === '2' ? '女' : '未知');
$gradeClass = ($student['gradeName'] ?? '-') . ' ' . ($student['className'] ?? '-');
printf("%-10s %-15s %-15s %-10s %-20s %-15s\n",
$student['studentId'] ?? '-',
$student['studentCode'] ?? '-',
$student['studentName'] ?? '-',
$gender,
$student['schoolName'] ?? '-',
$gradeClass
);
}
echo str_repeat('-', 100) . "\n";
} else {
echo " 暂无数据\n";
}
} else {
echo "❌ 调用失败: {$response['msg']}\n";
if (isset($response['data'])) {
echo "错误详情: " . json_encode($response['data'], JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) . "\n";
}
}
} catch (Exception $e) {
echo "❌ 异常: {$e->getMessage()}\n";
}
// ============================================================
// 示例 2: 按学校查询学生
// ============================================================
echo "\n\n";
echo "【示例 2】按学校查询学生\n";
echo "========================================\n";
try {
$response = $client->get('/open/api/student/list', [
'pageNum' => 1,
'pageSize' => 5,
'schoolId' => 1 // 假设学校 ID 为 1
]);
if ($response['code'] === 200) {
echo "✅ 调用成功!\n";
echo " 学校筛选后的学生数: {$response['total']}\n";
} else {
echo "❌ 调用失败: {$response['msg']}\n";
}
} catch (Exception $e) {
echo "❌ 异常: {$e->getMessage()}\n";
}
// ============================================================
// 示例 3: 按姓名模糊查询
// ============================================================
echo "\n\n";
echo "【示例 3】按姓名模糊查询\n";
echo "========================================\n";
try {
$response = $client->get('/open/api/student/list', [
'pageNum' => 1,
'pageSize' => 10,
'studentName' => '张' // 模糊查询姓张的学生
]);
if ($response['code'] === 200) {
echo "✅ 调用成功!\n";
echo " 查询到的学生数: {$response['total']}\n";
if (!empty($response['rows'])) {
echo " 学生姓名列表:\n";
foreach ($response['rows'] as $index => $student) {
echo " " . ($index + 1) . ". {$student['studentName']}";
if (isset($student['studentCode'])) {
echo " ({$student['studentCode']})";
}
echo "\n";
}
}
} else {
echo "❌ 调用失败: {$response['msg']}\n";
}
} catch (Exception $e) {
echo "❌ 异常: {$e->getMessage()}\n";
}
// ============================================================
// 常见错误处理示例
// ============================================================
echo "\n\n";
echo "【常见错误处理】\n";
echo "========================================\n";
echo "1. 签名错误处理:\n";
echo " - 检查 appSecret 是否正确\n";
echo " - 检查参数排序是否按 ASCII 码升序\n";
echo " - 检查 MD5 结果是否转为大写\n\n";
echo "2. 时间戳过期处理:\n";
echo " - 签名有效期为 5 分钟\n";
echo " - 确保服务器时间同步\n\n";
echo "3. 权限不足处理:\n";
echo " - 检查应用是否已授权该接口\n";
echo " - 联系管理员在应用管理中配置接口授权\n\n";
echo "4. 应用停用处理:\n";
echo " - 检查应用状态是否为正常\n";
echo " - 联系管理员启用应用\n\n";
// ============================================================
// 测试完成
// ============================================================
echo "\n";
echo "========================================\n";
echo "测试完成!\n";
echo "========================================\n";
echo "\n";
echo "💡 提示:\n";
echo " - 如需关闭调试模式,请修改代码中的 debug 参数为 false\n";
echo " - 更多接口请参考技术文档\n";
echo " - 如有问题请查看 /tmp/pangu-admin.log 日志\n";
echo "\n";