给网站增加“每天60s读懂世界”的方法
你和这个世界,只差60秒!
参考自冷筱宇的小窝
效果展示
点击查看→60秒看世界
图片调用(三方接口)
新建文章或独立页面,加入以下代码即可。(位置、样式等自行调整)
<img src="https://api.03c3.cn/api/zb" />
API调用(三方接口)
header("content-type:image/jpg");
echo file_get_contents(json_decode(file_get_contents("https://api.03c3.cn/api/zb"))->imageUrl);
原生调用(知乎接口)
https://www.zhihu.com/api/v4/columns/c_1261258401923026944/items
修改functions.php
function getZhihuContent() {
$url = "https://www.zhihu.com/api/v4/columns/c_1261258401923026944/items";
// 初始化 cURL 会话
<!--INLINEMATH0-->url);
// 设置 cURL 选项
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// 执行 cURL 请求并获取返回的数据
<!--INLINEMATH1-->ch);
// 检查是否有错误发生
if(curl_errno($ch)){
echo 'Curl 错误:' . curl_error($ch);
}
// 关闭 cURL 会话
curl_close($ch);
// 解码 JSON 数据
<!--INLINEMATH2-->response, true);
// 获取"data"数组下标为0的"content"内容并输出
if(isset($data['data'][0]['content'])){
<!--INLINEMATH3-->data['data'][0]['content'];
// 正则表达式,用于匹配第一个img标签中的data-original属性值
$pattern = '/<img[^>]*\sdata-original="([^"]*)"[^>]*>/i';
$headPic = 'https://www.03c3.cn/zb/H.php';
if (preg_match(<!--INLINEMATH4-->content, $matches)) {
<!--INLINEMATH5-->matches[1];
}
// 删除a标签及其内容
<!--INLINEMATH6-->content);
// 删除img标签及其内容
<!--INLINEMATH7-->content);
// 删除figure标签及其内容
<!--INLINEMATH8-->content);
echo '<p style="text-align:center"><img src="'.<!--INLINEMATH9-->content;
} else {
echo "未找到内容";
}
}
调用方式
<?php getZhihuContent(); ?>
18 个赞


