『返回』首页

随机图片api与一言(hitokoto)api

想法来源

日常逛博客看到未闻花名的博客 头部图片一直在变换,F12看到用了一个PHP来加载随机图片。

不得不感慨一句和泉沙雾最可爱了,你们不要跟我抢 顺着链接爬到了月宅酱漫月api ,看到他每个月维护所需的花费那么巨大,我还是撸一个自己用吧

(其实主要是可用的接口过少)目前有一个公开接口 以及一个博文内隐藏接口-小忍壁纸 还有我爬取到的和泉沙雾和泉沙雾最可爱

顺手爬了一下月宅酱的和泉沙雾api接口内的全部图片,一共50张(相册)

由于很久没有写PHP的原因,月宅酱的漫月api也没有公开源码我打算照抄一下一言(hitokoto)api的源码来写这个随机图片的api

寻找一言API

lwl12的 LWL-Hitokoto API(一言-纯净API) 未开源

狂放的 [开源]一言纯净API 开源 也很好用

在狂放的评论区找到了后宫学长的超简洁版api 使用“一言”为博客增加文雅气息 差不多7行代码就搞定

  • 新建一个文件夹,名为:hitokoto
  • 复制以下代码,保存为 index.php 文件,并放置上面的文件夹中
<?php
//获取句子文件的绝对路径
$path = dirname(__FILE__);
$file = file($path."/hitokoto.txt");
//随机读取一行
$arr  = mt_rand( 0, count( $file ) - 1 );
$hitokoto  = trim($file[$arr]);
//输出内容
echo $hitokoto;
?>
  • 把你的一言名言写进 hitokoto.txt 文件中,一句话一行,并放置上面的文件夹中

示例:

有你在的日子才是我的日常。 夹在我女友与前女友与青梅竹马间的果然是修罗场! 既然如此,就再努力一次吧。别在这里愁眉不展,也不要再自欺欺人,重新来过! 比自己,比梦想更重要的东西永远都存在着… 嘛,那又怎么样呢?

就这样就可以了

改造成随机图片API

那么一言api有了之后,我们该如何改造他变成一个随机图片api呢?

http的303跳转的作用是:对于POST请求,它表示请求已经被处理,客户端可以接着使用GET方法去请求Location里的URI

这样我们可以用

if (isset($url)) 
{ 
Header("HTTP/1.1 303 See Other"); 
Header("Location: $url"); 
exit; 
} 

来进行跳转

在原有的API基础上修改一下就是

<?php
//获取句子文件的绝对路径
$path = dirname(__FILE__);
$file = file($path."/pics.txt");
//随机读取一行
$arr  = mt_rand( 0, count( $file ) - 1 );
$url  = trim($file[$arr]);
//输出内容
#echo $url;
//303跳转
if (isset($url)) 
{ 
Header("HTTP/1.1 303 See Other"); 
Header("Location: $url"); 
exit; 
} 
?>

新建一个pics.txt写入你所需要的随机展示的图片即可食用

我的和泉沙雾是

https://img.example.com/images/2018/01/15/mVKl.png
https://img.example.com/images/2018/01/15/muqM.jpg
https://img.example.com/images/2018/01/15/mj3d.png
https://img.example.com/images/2018/01/15/mqJ9.jpg
https://img.example.com/images/2018/01/15/mHNR.png
https://img.example.com/images/2018/01/15/miMI.jpg
https://img.example.com/images/2018/01/15/m291.jpg
https://img.example.com/images/2018/01/15/m4Ca.jpg
https://img.example.com/images/2018/01/15/mnng.jpg
https://img.example.com/images/2018/01/15/mZEP.jpg
https://img.example.com/images/2018/01/15/mfzT.jpg
https://img.example.com/images/2018/01/15/mXTU.jpg
https://img.example.com/images/2018/01/15/mW33.jpg
https://img.example.com/images/2018/01/15/mMFV.jpg
https://img.example.com/images/2018/01/15/msNJ.jpg
https://img.example.com/images/2018/01/15/mDMs.jpg
https://img.example.com/images/2018/01/15/mAek.jpg
https://img.example.com/images/2018/01/15/m3C7.jpg
https://img.example.com/images/2018/01/15/monX.jpg
https://img.example.com/images/2018/01/15/mhBy.jpg
https://img.example.com/images/2018/01/15/mPzp.jpg
https://img.example.com/images/2018/01/15/mGTn.jpg
https://img.example.com/images/2018/01/15/mEoS.jpg
https://img.example.com/images/2018/01/15/mBFE.jpg
https://img.example.com/images/2018/01/15/mr8i.jpg
https://img.example.com/images/2018/01/15/mpwK.jpg
https://img.example.com/images/2018/01/15/mmeh.jpg
https://img.example.com/images/2018/01/15/meyj.jpg
https://img.example.com/images/2018/01/15/m7n0.jpg
https://img.example.com/images/2018/01/15/mxBA.jpg
https://img.example.com/images/2018/01/15/mLg8.jpg
https://img.example.com/images/2018/01/15/mJTl.jpg
https://img.example.com/images/2018/01/15/mFoM.jpg
https://img.example.com/images/2018/01/15/m00d.jpg
https://img.example.com/images/2018/01/15/9U89.jpg
https://img.example.com/images/2018/01/15/9OwR.jpg
https://img.example.com/images/2018/01/15/9K7I.jpg
https://img.example.com/images/2018/01/15/9gy1.jpg
https://img.example.com/images/2018/01/15/9cZa.jpg
https://img.example.com/images/2018/01/15/9S1g.jpg
https://img.example.com/images/2018/01/15/9IgP.jpg
https://img.example.com/images/2018/01/15/9CHT.jpg
https://img.example.com/images/2018/01/15/9yhU.jpg
https://img.example.com/images/2018/01/15/9t03.jpg
https://img.example.com/images/2018/01/15/95bV.jpg
https://img.example.com/images/2018/01/15/9kwJ.jpg
https://img.example.com/images/2018/01/15/9N7s.jpg
https://img.example.com/images/2018/01/15/9btk.jpg
https://img.example.com/images/2018/01/15/9VZ7.jpg
https://img.example.com/images/2018/01/15/9u1X.jpg
https://img.example.com/images/2018/01/15/9qcy.jpg
https://img.example.com/images/2018/01/15/9THp.jpg
https://img.example.com/images/2018/01/15/9Hhn.jpg
https://img.example.com/images/2018/01/15/92vS.jpg
https://img.example.com/images/2018/01/15/9abE.jpg
https://img.example.com/images/2018/01/15/94si.jpg

就这样,一份随机图片api就写好了,不用受到别人的约束,自己写的才是最好用的。

© 2015 - 至今 | Powered by Jekyll | Theme - no style, please!
主站:TOTORO625 | 镜像:橙汁不甜 | 站点地图