EOF;
echo $str;
}
// 辅助函数:调用TuShare API
function callTushareApi($method, $params){
$token = '1bc28452ba375da19320cda845ae6307578964cb3ae473d0dc702aea';
// 将参数编码为JSON格式
$postData = json_encode([
"api_name" => $method,
"token" => $token,
"params" => $params,
"fields" => "" // 其他字段需要根据具体API调整
]);
print_r($postData);
// 初始化cURL会话
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://api.tushare.pro');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Content-Length: ' . strlen($postData)
]);
// 执行cURL请求
$response = curl_exec($ch);
$err = curl_error($ch);
curl_close($ch);
// 调试输出:打印请求和响应信息
if (debug_enabled()) { // 假设这是一个函数,用于检查是否启用了调试模式
error_log("TuShare API Request: " . $postData);
error_log("TuShare API Response: " . $response);
}
// 错误处理
if ($err) {
error_log("cURL error: " . $err);
return "cURL error: " . $err;
}
$responseData = json_decode($response, true);
// 检查API调用是否成功
if (isset($responseData['code']) && $responseData['code'] != 200) {
error_log("TuShare API Error: " . $responseData['msg']);
return "TuShare API Error: " . $responseData['msg'];
}
if (isset($responseData['data']) && isset($responseData['data']['items'])) {
return $responseData['data']['items'];
} else {
return [];
}
}
// 假设的调试模式开关函数
function debug_enabled()
{
// 这里可以根据实际情况来设置调试模式的开关条件
// 例如,可以检查环境变量、配置文件或HTTP请求头等
return true; // 或者返回一个条件表达式的结果
}
?>