Files
QuickAPI/api/zhihu/index.php
muzihuaner 504b7c9921 更新
2026-03-18 11:02:06 +08:00

47 lines
1.3 KiB
PHP
Raw Permalink 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.
<?php
$file = 'counter.dat';
// 读取并自增
$counter = (int)@file_get_contents($file);
$counter++;
// 使用 LOCK_EX 防止并发写入冲突
file_put_contents($file, $counter, LOCK_EX);
// 设置响应头为JSON格式
header('Content-Type: application/json; charset=utf-8');
// 定义要获取的API地址
$api_url = 'http://news-at.zhihu.com/api/2/news/latest';
// 初始化cURL会话
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $api_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过SSL证书验证仅用于测试
curl_setopt($ch, CURLOPT_TIMEOUT, 30); // 设置超时时间
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // 跟随重定向
// 执行请求并获取响应
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$error = curl_error($ch);
// 关闭cURL资源
curl_close($ch);
// 处理响应
if ($http_code === 200 && !empty($response)) {
// 直接返回获取到的内容
echo $response;
} else {
// 返回错误信息
http_response_code(500);
echo json_encode([
'error' => true,
'message' => '获取接口内容失败',
'http_code' => $http_code,
'curl_error' => $error ?: '无错误信息'
]);
}
?>