每日60秒新闻早报单页文字版的,这个很早就有了,最近有用户问我这个怎么实现,其实很简单的只是一个单页而已,这个是PHP版的会将头部图片缓存到本地然后在展示可防止接口信息泄露。
HTML代码
<?php
$url = 'https://api.afx5.cn/api/mrzb';
$response = file_get_contents($url);
$data = json_decode($response, true);
if ($data['code'] == 200) {
$headImage = $data['data']['head_image'];
$date = $data['data']['date'];
$news = $data['data']['news'];
$weiyu = $data['data']['weiyu'];
$cacheDir = '60s';
$headImageFile = $cacheDir . '/' . basename($headImage);
if (!file_exists($cacheDir)) {
mkdir($cacheDir, 0777, true);
}
if (!file_exists($headImageFile)) {
$ch = curl_init($headImage);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3');
$imageData = curl_exec($ch);
curl_close($ch);
file_put_contents($headImageFile, $imageData);
}
} else {
echo "<p>获取数据失败,请稍后再试。</p>";
exit;
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>每日新闻早报</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
display: flex;
flex-direction: column;
align-items: center;
}
.container {
width: 90%;
max-width: 800px;
margin: 20px auto;
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
.header img {
width: 100%;
height: auto;
border-radius: 8px;
}
h1 {
text-align: center;
margin: 20px 0;
}
ul {
list-style-type: none;
padding: 0;
}
li {
background-color: #fff;
padding: 10px;
border-radius: 8px;
}
.weiyu {
text-align: center;
font-style: italic;
margin-top: 20px;
}
</style>
</head>
<body>
<div class="container">
<div class="header">
<img src="<?php echo $headImageFile; ?>">
</div>
<h1><?php echo $date; ?> 新闻早报</h1>
<ul>
<?php foreach ($news as $newsItem): ?>
<li><?php echo $newsItem; ?></li>
<?php endforeach; ?>
</ul>
<p class="weiyu"><?php echo $weiyu; ?></p>
</div>
</body>
</html>
© 版权声明
THE END