typecho自动发布60读懂世界代码以及教程
2024年9月28日 21:28
typecho自动发布60读懂世界
1.首先保存代码到你的ty中确保文件目录能正常访问
2.然后配置好文件中的数据库也就是你的typecho的数据库
3.修改你要发布的分类也就是代码中的$categoryName = '需要发布的分类名称';
4.最后就是在宝塔中创建定时任务访问你的这个文件,例如https://xxx.com/60s/这样就可以每天定时发布了
5.如果提示缺少views字段就在数据库中的typecho_contents增加字段就行了
演示站点cnm.cm
代码部分
[hide]
<?php
$apiUrl = 'https://api.suxun.site/api/sixs?type=json';
$response = file_get_contents($apiUrl);
$data = json_decode($response, true);
if ($data['code'] === '200') {
$dbHost = '127.0.0.1';
$dbUser = 'root';
$dbPass = 'root';
$dbName = 'root';
$conn = new mysqli($dbHost, $dbUser, $dbPass, $dbName);
if ($conn->connect_error) {
die("连接失败: ". $conn->connect_error);
}
$categoryName = '需要发布的分类名称';
$sqlGetCategoryId = "SELECT mid FROM `typecho_metas` WHERE `type`='category' AND `name`='$categoryName'";
$result = $conn->query($sqlGetCategoryId);
if ($result && $result->num_rows > 0) {
$row = $result->fetch_assoc();
$categoryId = $row['mid'];
} else {
die("无法找到指定分类的 ID。");
}
$sqlGetLastCid = "SELECT MAX(CAST(SUBSTRING(cid, 1) AS UNSIGNED)) AS last_cid FROM `typecho_contents`";
$resultLastCid = $conn->query($sqlGetLastCid);
$lastCid = 0;
if ($resultLastCid && $resultLastCid->num_rows > 0) {
$rowLastCid = $resultLastCid->fetch_assoc();
$lastCid = $rowLastCid['last_cid']? $rowLastCid['last_cid'] + 1 : 1;
}
$date = date('Y 年 n 月 j 日,l');
$title = $date. ',每日 60 秒读懂世界';
$content = '<img src="'.$data['head_image'].'"><br>';
foreach ($data['news'] as $newsItem) {
$content.= $newsItem.'<br>';
}
$content.= '<br>'.$data['weiyu'];
$tags = '每日 60 秒';
$cid = $lastCid;
$slug = date('Ymd');
$sqlInsertPost = "INSERT INTO `typecho_contents` (`cid`, `title`, `slug`, `created`, `modified`, `text`, `order`, `authorId`, `template`, `type`, `status`, `password`, `commentsNum`, `allowComment`, `allowPing`, `allowFeed`, `parent`, `views`, `agree`)
VALUES ('$cid', '$title', '$slug', UNIX_TIMESTAMP(), UNIX_TIMESTAMP(), '$content', '0', '1', NULL, 'post', 'publish', NULL, '0', '1', '1', '1', '0', '1', '0')";
if ($conn->query($sqlInsertPost) === TRUE) {
$lastInsertId = $conn->insert_id;
$sqlInsertRelation = "INSERT INTO `typecho_relationships` (`cid`, `mid`) VALUES ($lastInsertId, $categoryId)";
if ($conn->query($sqlInsertRelation) === TRUE) {
echo "文章发布成功!";
} else {
echo "Error inserting relationship: ". $conn->error;
}
} else {
echo "Error inserting post: ". $sqlInsertPost. "<br>". $conn->error;
}
$conn->close();
} else {
echo '获取 API 数据失败。';
}
?>[/hide]