帮我用PHP写一个使用图片链接txt文件返回随机图片的API
以下是一个使用PHP编写的API,用于从图片链接的txt文件中返回随机图片:
<?php
// 图片链接的txt文件路径
$filePath = 'path/to/image_links.txt';
// 读取txt文件中的图片链接
$imageLinks = file($filePath, FILE_IGNORE_NEW_LINES);
// 随机选择一个图片链接
$randomImage = $imageLinks[array_rand($imageLinks)];
// 返回JSON格式的响应
$response = array('image' => $randomImage);
header('Content-Type: application/json');
echo json_encode($response);
?>
使用方法:
- 将上述代码保存为
random_image_api.php文件,并将path/to/image_links.txt替换为实际的图片链接txt文件路径。 - 将
random_image_api.php文件放置在可以访问的Web服务器上。 - 通过访问
random_image_api.php,将会返回一个JSON格式的响应,其中包含一个随机选择的图片链接。
请确保图片链接的txt文件中每行只包含一个图片链接,并确保文件中的链接以换行符分隔
原文地址: https://www.cveoy.top/t/topic/iFIf 著作权归作者所有。请勿转载和采集!