为typecho joe主题添加一个随机笑话的独立页面
演示图片
演示页面
使用方法
1在joe主题根目录随便创建一个.php文件将代码粘贴进去
2在后台添加独立页面选择笑话模板
具体代码
<?php
/**
* 笑话
*
* @package custom
*
**/
?>
<?php
$API_KEY = '7e9401a58dc937d2d827a09fc39f37a3';
$API_URL = 'http://v.juhe.cn/joke/randJoke.php';
$get_post_data = array(
'key' => $API_KEY,
);
$sk = 'e80d5492e21e7390907b97076cb770a5';
$resdata = api::send($API_URL, $get_post_data, 'POST', true, $sk);
$cc= json_decode($resdata)->result;
// var_dump($cc);
$bb =serialize($cc);
class api
{
public static function send($API_URL, $get_post_data, $type, $ifsign, $sk)
{
$get_post_data = http_build_query($get_post_data);
if ($ifsign) {
$sign = md5($get_post_data . $sk);
$res = self::send_curl($API_URL, $type, $get_post_data, $sign);
} else {
$res = self::send_curl($API_URL, $type, $get_post_data, null);
}
return $res;
}
public static function send_curl($API_URL, $type, $get_post_data, $sign)
{
$ch = curl_init();
if ($type == 'POST') {
curl_setopt($ch, CURLOPT_URL, $API_URL);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $get_post_data);
} elseif ($type == 'GET') {
curl_setopt($ch, CURLOPT_URL, $API_URL . '?' . $get_post_data);
}
if ($sign) {
curl_setopt($ch, CURLOPT_HTTPHEADER, ['sign:' . $sign]);
}
curl_setopt($ch, CURLOPT_REFERER, $API_URL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$resdata = curl_exec($ch);
curl_close($ch);
return $resdata;
}
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<?php $this->need('public/include.php'); ?>
<?php if ($this->options->JPrismTheme) : ?>
<link rel="stylesheet" href="<?php $this->options->JPrismTheme() ?>">
<?php else : ?>
<link rel="stylesheet" href="https://fastly.jsdelivr.net/npm/prismjs@1.23.0/themes/prism.min.css">
<?php endif; ?>
<script src="https://fastly.jsdelivr.net/npm/clipboard@2.0.6/dist/clipboard.min.js"></script>
<script src="https://fastly.jsdelivr.net/npm/typecho-joe-next@6.2.4/plugin/prism/prism.min.js"></script>
<script src="<?php $this->options->themeUrl('assets/js/joe.post_page.min.js'); ?>"></script>
</head>
<body>
<div id="Joe">
<?php $this->need('public/header.php'); ?>
<div class="joe_container">
<div class="joe_main">
<div class="joe_detail" data-cid="<?php echo $this->cid ?>">
<?php $this->need('public/batten.php'); ?>
<?php $this->need('public/article.php'); ?>
<?php
$friends = [];
$friends_color = [
'#F8D800',
'#0396FF',
'#EA5455',
'#7367F0',
'#32CCBC',
'#F6416C',
'#28C76F',
'#9F44D3',
'#F55555',
'#736EFE',
'#E96D71',
'#DE4313',
'#D939CD',
'#4C83FF',
'#F072B6',
'#C346C2',
'#5961F9',
'#FD6585',
'#465EFB',
'#FFC600',
'#FA742B',
'#5151E5',
'#BB4E75',
'#FF52E5',
'#49C628',
'#00EAFF',
'#F067B4',
'#F067B4',
'#ff9a9e',
'#00f2fe',
'#4facfe',
'#f093fb',
'#6fa3ef',
'#bc99c4',
'#46c47c',
'#f9bb3c',
'#e8583d',
'#f68e5f',
];
$friends_text = $this->options->JFriends;
if ($friends_text) {
$friends_arr = explode("\r\n", $friends_text);
if (count($friends_arr) > 0) {
for ($i = 0; $i < count($friends_arr); $i++) {
$name = explode("||", $friends_arr[$i])[0];
$url = explode("||", $friends_arr[$i])[1];
$avatar = explode("||", $friends_arr[$i])[2];
$desc = explode("||", $friends_arr[$i])[3];
$friends[] = array("name" => trim($name), "url" => trim($url), "avatar" => trim($avatar), "desc" => trim($desc));
};
}
}
?>
<ul class="joe_detail__friends">
<?php foreach ($cc as $xh) : ?>
<li class="joe_detail__friends-item">
<a class="contain" href="<?php echo $item['url']; ?>" target="_blank" rel="noopener noreferrer" style="background: <?php echo $friends_color[mt_rand(0, count($friends_color) - 1)] ?>">
<span class="title"><?php echo $xh->title; ?></span>
<div class="content">
<div class="desc"><?php echo $xh->content; ?></div>
</div>
</a>
</li>
<?php endforeach; ?>
</ul>
<?php $this->need('public/handle.php'); ?>
<?php $this->need('public/copyright.php'); ?>
</div>
<?php $this->need('public/comment.php'); ?>
</div>
<?php $this->need('public/aside.php'); ?>
</div>
<?php $this->need('public/footer.php'); ?>
</div>
</body>
</html>