task-report/client_example.php
2025-04-14 00:33:31 +08:00

80 lines
2.1 KiB
PHP

<?php
/**
* 任务报告系统客户端示例
*
* 此示例演示如何使用PHP向任务报告系统API提交数据
*/
// 设置API地址
$api_url = 'https://toolstrack.cgnico.com/api.php';
// 本地测试可以使用:
// $api_url = 'http://192.168.2.4:9000/api.php';
// 准备要提交的数据
$data = [
'username' => 'JeffreyTsai',
'tool_name' => 'MetaBox',
'task_name' => 'ExampleTask',
'time_saved' => '1.5', // 节省时间(小时)
'time_cost' => '0.2', // 花费时间(小时)
// 'timestamp' => date('Y-m-d H:i:s') // 可选,如果不提供将使用服务器时间
];
// 将数据转换为JSON
$json_data = json_encode($data);
// 设置cURL选项
$ch = curl_init($api_url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Content-Length: ' . strlen($json_data)
]);
// 执行请求
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
// 输出结果
echo "HTTP响应代码: $http_code\n";
echo "响应内容:\n";
echo $response;
/**
* Python客户端示例
*
* 以下是使用Python向API提交数据的示例代码
*
* ```python
* import requests
* import json
* from datetime import datetime
*
* # 设置API地址
* api_url = 'https://toolstrack.cgnico.com/api.php'
* # 本地测试可以使用:
* # api_url = 'http://192.168.2.4:9000/api.php'
*
* # 准备要提交的数据
* data = {
* 'username': 'JeffreyTsai',
* 'tool_name': 'MetaBox',
* 'task_name': 'ExampleTask',
* 'time_saved': '1.5', # 节省时间(小时)
* 'time_cost': '0.2', # 花费时间(小时)
* # 'timestamp': datetime.now().strftime('%Y-%m-%d %H:%M:%S') # 可选
* }
*
* # 发送POST请求
* response = requests.post(api_url, json=data)
*
* # 输出结果
* print(f"HTTP响应代码: {response.status_code}")
* print("响应内容:")
* print(response.json())
* ```
*/