Inspired by Yiyan API, Ancient Poetry API, and Daily Bible
Using a reprint from 1966 found online, this version should be safe
The source file is TXT, formatted with a script, currently totaling 432 entries
PHP and data files have been uploaded to Github, feel free to use
- Create a file named api.php in the website directory, with the content
<?php
// File to store data
$filename = 'yulu.dat';
// Specify page encoding
header('Content-type: text/html; charset=utf-8');
if(!file_exists($filename)) {
die($filename . ' data file does not exist');
}
// Read the entire data file
$data = file_get_contents($filename);
// Split into an array by newline
$data = explode(PHP_EOL, $data);
// Randomly get a line index
$result = $data[array_rand($data)];
// Remove extra newline characters (for safety)
$result = str_replace(array("\r","\n","\r\n"), '', $result);
echo 'document.write("'.htmlspecialchars($result).'");';
-
Upload the yulu.dat data file in the same directory (see above)
-
Call this code where you need to insert it
<script src="http://yourdomain/api.php"></script>
Referenced: Simple Implementation of Yiyan / Random Sentence Function in PHP | Meng Kun Blog