随机句子随机句子

生命不息,
折腾不止!

随机句子

1.(推荐)

博客集成Hitokoto·一言经典语句功能 https://zhang.ge/5127.html

一言网(Hitokoto.cn)创立于 2016 年,隶属于萌创 Team,目前网站主要提供一句话服务。

动漫也好、小说也好、网络也好,不论在哪里,我们总会看到有那么一两个句子能穿透你的心。我们把这些句子汇聚起来,形成一言网络,以传递更多的感动。如果可以,我们希望我们没有停止服务的那一天。

简单来说,一言指的就是一句话,可以是动漫中的台词,也可以是网络上的各种小段子。

或是感动,或是开心,有或是单纯的回忆。来到这里,留下你所喜欢的那一句句话,与大家分享,这就是一言存在的目的。

 

一、他山之石

张戈博客之前是调用的自由天空的一言 API 接口,稳定性和速度还不错,不想折腾的朋友推荐参考他的教程快速部署一个即可。

Ps:喜欢使用官方接口也可以前往官方的 API 介绍地址参考部署:http://hitokoto.cn/api

如果是像我这种比较喜欢折腾的朋友,可以考虑自己部署一个,主要是方便 DIY 句子库内容。当然,自己部署的文章其实也已经有博友分享过了==>传送门

下面简单的介绍下张戈博客这边的 DIY 部署过程。

二、部署接口

首先按照我个人编码习惯,把小霖小朋友的代码略微改了下(代码强迫症):

<?php
//获取句子文件的绝对路径
//如果你介意别人可能会拖走这个文本,可以把文件名自定义一下,或者通过Nginx禁止拉取也行。
$path = dirname(__FILE__);
$file = file($path."/hitokoto.txt");

//随机读取一行
$arr  = mt_rand( 0, count( $file ) - 1 );
$content  = trim($file[$arr]);

//编码判断,用于输出相应的响应头部编码
if (isset($_GET['charset']) && !empty($_GET['charset'])) {
    $charset = $_GET['charset'];
    if (strcasecmp($charset,"gbk") == 0 ) {
        $content = mb_convert_encoding($content,'gbk', 'utf-8');
    }
} else {
    $charset = 'utf-8';
}
header("Content-Type: text/html; charset=$charset");

//格式化判断,输出js或纯文本
if ($_GET['format'] === 'js') {
    echo "function hitokoto(){document.write('" . $content ."');}";
} else {
    echo $content;
}
?>

以上代码保存为 index.php,然后上传到网站根目录下的 hitokoto 文件夹(这个自己随机定义),最后,从小霖分享的文章下载 hitokoto.txt 文本文件

当然这里我也传了一份到微云网盘,方便大家下载:

下载地址:https://share.weiyun.com/cff40cfd057fca81fde3aeb6f00fbfb1

hitokoto.txt 上传到和 index.php 同级目录,比如 hitokoto 文件夹内。

现在,浏览器访问 http://你的域名/hitokoto/ 就可以看到输出内容了。

三、博客集成

第一步我们已经完成了这个接口的自建部署,现在可以把这个功能搬到博客上了。

部署方法和其他博客基本一致,非常简单,将下面两行代码添加到博客你想显示一言的位置即可:

<script type="text/javascript" src="https://你的域名/hitokoto/?format=js&charset=utf-8"></script>
<div id="hitokoto"><script>hitokoto()</script></div>

不过,这样输出的样式可能会比较丑,如果你懂 CSS 的话可以自己再美化美化。

当然,Begin 主题或者不会 css 的朋友可以先试下我这边写好的 css 代码(可以加到 style.css):

#hitokoto{
    border-left: 5px solid #2f889a;
    border-right: 5px solid #2f889a;
    background-color: #ebebeb;
    padding: 10px;text-align: center;
    color: #095AD4;
    margin: 5px 0 5px 0;
}

部署完成之后,前台刷新应该就可以看到效果了,每次刷新都会随机展示一言经典句子。如果你有新的句子,也只要编辑 hitokoto.txt 文件加入即可。

好了,文章就介绍这么多,喜欢的朋友可以试下了。

2.

极客一言 API 上线 https://www.v2ex.com/t/417008

GitHub:https://github.com/AkinoMaple/hitokoto

一言纯净API

版权归作者及原作者所有,任何人不得未经授权修改版权,二次开发请遵守开源协议

1.php

<?php
/*
名称:一言API纯净版
版本:1.0
最后更新时间:2017/10/04 15:57
作者:狂放
作者博客:https://www.iknet.top
作者地址:https://www.iknet.top/568.html
原作者:小霖
原作者博客:https://xiaolin.in/
原作者地址:https://xiaolin.in/read/hitokoto-api-xiaolin-edition.html
开发:二次开发,已取得原作者二次开发及开源授权
开源协议:GPL v3
GitHub项目地址:https://github.com/kfangf/hitokoto
码云项目地址:https://gitee.com/kfang/hitokoto
版权归作者及原作者所有,任何人不得未经授权修改版权,二次开发请遵守开源协议
版权所有,侵权必究
*/ 
header('X-Powered-By:KFang API (api.iknet.top)');
header('access-control-allow-origin:*');
if ($_GET['charset']=='GBK' ||$_GET['charset']=='gbk' || $_GET['charset']=='gb2312'){
  $array=file('hitokoto.txt');
  $rand=rand(0,3385);
 
  function utf8_to_gbk($str){
    return mb_convert_encoding($str, 'gbk', 'utf-8');
}
    $string=$array[$rand];
	header('Content-Type: text/html; charset=GBK');
    if ($_GET['code']==='js' || $_GET['code']==='javascript' || $_GET['code']==='JavaScript') {
		header('Content-type: application/x-javascript; charset=GBK');
          echo "function kfanghitokoto(){document.write(\"";
          echo trim(utf8_to_gbk($string)) . "\");}";
		 } elseif ($_GET['code']==='array' || $_GET['code']==='Array' || $_GET['code']==='arr' || $_GET['code']==='Arr') {
			$arr = array(
			'code' => 200 ,
			'msg' => trim(utf8_to_gbk($string))
			);
			var_dump($arr);
	    }else{
          echo trim(utf8_to_gbk($string));
		}
    }else{ 

  $array=file('hitokoto.txt');
  $rand=rand(0,3385);
  $string=$array[$rand];
  function arrayToXml($arr,$dom=null,$node=null,$root='xml',$cdata=false){  
    if (!$dom){  
        $dom = new DOMDocument('1.0','utf-8');  
    }  
    if(!$node){  
        $node = $dom->createElement($root);  
        $dom->appendChild($node);  
    }  
    foreach ($arr as $key=>$value){  
        $child_node = $dom->createElement(is_string($key) ? $key : 'node');  
        $node->appendChild($child_node);  
        if (!is_array($value)){  
            if (!$cdata) {  
                $data = $dom->createTextNode($value);  
            }else{  
                $data = $dom->createCDATASection($value);  
            }  
            $child_node->appendChild($data);  
        }else {  
            arrayToXml($value,$dom,$child_node,$root,$cdata);  
        }  
    }  
    return $dom->saveXML();  
}  
header('Content-Type: text/html; charset=UTF-8');
    if ($_GET['code']==='js' || $_GET['code']==='javascript' || $_GET['code']==='JavaScript') {
		 header('Content-type: application/x-javascript; charset=UTF-8');
          echo "function kfanghitokoto(){document.write(\"";
          echo trim($string);
          echo "\");}";
		 } elseif ($_GET['code']==='json' || $_GET['code']==='JSON') {
			header('Content-type: application/json; charset=UTF-8');
			$json = json_encode(array(
			'code' => 200 ,
			'msg' => trim($string)
			));
			echo $json;
		} elseif ($_GET['code']==='xml' || $_GET['code']==='XML') {
		$xml = arrayToXml(array(
			'msg' => trim($string)
			));	
			echo $xml;
		 } elseif ($_GET['code']==='array' || $_GET['code']==='Array' || $_GET['code']==='arr' || $_GET['code']==='Arr') {
			$arr = array(
			'code' => 200 ,
			'msg' => trim($string)
			);
		var_dump($arr);
        }else{
          echo trim($string);
          }
	}
?>

 

hitokoto.txt

有你在的日子才是我的日常。
夹在我女友与前女友与青梅竹马间的果然是修罗场!
既然如此,就再努力一次吧。别在这里愁眉不展,也不要再自欺欺人,重新来过!
比自己,比梦想更重要的东西永远都存在着...
嘛,那又怎么样呢?
自身不先改变的话,一切都不会改变。
比起有一百个朋友,不如有个比一百人还要重要的真心朋友。
我有在反省,但我不后悔。
要超越过去与悲伤,用坚强和笑容去开拓明天。


调用方式

TXT/XML格式
直接 GET 请求:https://api.guoch.xyz 
实例:curl:https://api.guoch.xyz 
输出:你不被认同,就无法存在了么?

JS方式
GET请求https://api.guoch.xyz/?code=js 
实例:curl https://api.guoch.xyz/?code=js 
输出:function kfanghitokoto(){document.write("无法心想事成,是人之常情。");}

嵌入:
<script type="text/javascript" src="https://api.guoch.xyz/?code=js&charset=utf-8"></script>
<div id="saying"><script>saying()</script></div> 

放入footer <div id="saying"><script>saying()</script></div> 
放到需要的地方 
比如:
才华是刀刃,辛苦是磨刀石

json格式
GET请求: https://api.guoch.xyz/?code=json 
实例: curl https://api.guoch.xyz/?code=json 
返回: {"code":200,"msg":"\u4e8b\u60c5\u90fd\u6709\u5343\u4e07\u79cd\u89e3\u51b3\u65b9\u6cd5\uff0c\u76f8\u53cd\uff0c\u6709\u65f6\u4e5f\u4f1a\u65e0\u6cd5\u53ef\u89e3\u3002"}

ARRAY格式
GET : https://api.guoch.xyz/?code=array 
或者: https://api.guoch.xyz/?code=arr 
实例:
返回:array(2) { ["code"]=> int(200) ["msg"]=> string(40) "木叶飞舞之处,火亦生生不息." }

3.

毒鸡汤核心代码

来源:https://www.v2ex.com/t/564696#r_7339343

$sql="select * from soul order by rand( ) limit 1";
$rs=mysql_query($sql);

 

4.毒汤日历搭建一言 API接口

来源:http://www.heibai.org/post/1189.html

什么是毒汤日历 毒汤日历是一本有毒的日历,每天用毒鸡汤来唤醒你。 你甚至不用翻开日历,翻开 App 的推送,每天会守时送上一杯毒鸡汤。 自己也能制作毒鸡汤?那太好了,毒性够强,如果让他人扎到心你就厉害了。 每条毒汤能够点扎心、发毒评,或许转发给他人,让他人也扎扎心。

准备工作

通过抓包得到了毒汤日历的 API http://www.dutangapp.cn/u/toxic?date=2018-6-18

API 后面的data=xxxx-xx-xx 为日期

<?php
header("Content-type: text/html; charset=utf-8"); //设置编码 utf-8 
$utime = date("Y-m-d");//api的尾缀时间
//使用curl提高运行速度 不用动
function httpGet($url) {
    $curl = curl_init();
    $httpheader[] = "Accept:*/*";
    $httpheader[] = "Accept-Language:zh-CN,zh;q=0.8";
    $httpheader[] = "Connection:close";
    curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1" );
    curl_setopt($curl, CURLOPT_HTTPHEADER, $httpheader);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_TIMEOUT, 3);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($curl, CURLOPT_URL, $url);
    $res = curl_exec($curl);
    curl_close($curl);
    return $res;
}
$myfile = fopen("binduyan-$utime.txt", "a+");//创建文件保存抓取的句子为 binduyan-20xx-xx-xx.txt文件xx为当前年月日
//fopen("binduyan.txt", "a+") 默认binduyan.txt

//循环次数 2018-3-21 至现在日期相差的天数 
for ($i=1; $i<83; $i++) { 
    $json_string =httpGet('http://www.dutangapp.cn/u/toxic?date='.$utime);//curl 自定义函数访问api
    $data= json_decode($json_string,true);//解析json 转为php
 //2018-4-11之前只有一条数据 so 加判断
if (isset($data['data']['0']['data'])) {
     $text1= $data['data']['0']['data']."\n"; 
    fwrite($myfile, $text1);
}
if (isset($data['data']['1']['data'])) {
    $text2= $data['data']['1']['data']."\n";
   fwrite($myfile, $text2);
}   
if (isset($data['data']['2']['data'])) {
    $text3= $data['data']['2']['data']."\n";
    fwrite($myfile, $text3);
}
 
    $utime= date("Y-m-d",strtotime("-".strval($i)." day")); //每循环一次 当前日期减去循环变量
 
 } 
fclose($myfile);
echo "ok";
?>

访问这个页面后 会在自动生成 binduyan.txt 文件,大概 200 多条。

 

pa.php

<?php
header("Content-type: text/html; charset=utf-8"); //设置编码 utf-8 
$utime = date("Y-m-d");
$str = file_get_contents('data.txt');
$d=date('Y/m/d H:i',strtotime($str));
//请更改监控key 默认binduyan
if($_GET['p']==='binduyan'){
//判断今天是否已爬
    if(strtotime($utime)>strtotime($d)){
//爬虫开始    
$utime = date("Y-m-d");//api的尾缀时间
//使用curl提高运行速度 不用动
function httpGet($url) {
    $curl = curl_init();
    $httpheader[] = "Accept:*/*";
    $httpheader[] = "Accept-Language:zh-CN,zh;q=0.8";
    $httpheader[] = "Connection:close";
    curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1" );
    curl_setopt($curl, CURLOPT_HTTPHEADER, $httpheader);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_TIMEOUT, 3);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($curl, CURLOPT_URL, $url);
    $res = curl_exec($curl);
    curl_close($curl);
    return $res;
}
//定义写入函数 
function myfile($txt){
    $myfile = fopen("binduyan.txt", "a+");
    fwrite($myfile,$txt);
    fclose($myfile);
}
 
    $json_string =httpGet('http://api.qqsuu.net/u/toxic?date='.$utime);//curl 自定义函数访问api
    $data= json_decode($json_string,true);//解析json 转为php
 //2018-4-11之前只有一条数据 so 加判断 
if (isset($data['data']['0']['data'])) {
     $text1="\n". $data['data']['0']['data']."\n";
     myfile($text1);
 }
if (isset($data['data']['1']['data'])) {
    $text2= $data['data']['1']['data']."\n";
    myfile($text2);
}   
if (isset($data['data']['2']['data'])) {
    $text3= $data['data']['2']['data'];
    myfile($text3);
}
$myfile = fopen("data.txt", "w");
fwrite($myfile,$utime);
fclose($myfile);
echo "ok";
//爬虫结束
}else{
    echo "已爬";
}
}else
echo "老铁 搞事情吗";
?>

 

将以上代码保存为 pa.php 上传到网站(记得更改监控秘钥),每天访问下这个http://heibai.org/pa.php/?p=key 就会自动抓取当天的毒言并保存到 binduyan.txt 但是并不能达到全自动的目的。这时候需要监控平台,阿里云,360 都可以.本人推荐宝塔面板。

添加计划任务--访问url--设置每天访问

监控格式http://域名/文件目录/pa.php/?p=key key自己修改

PHP代码第七行

 

 

 

赞() 打赏
未经允许不得转载:我的博客 » 随机句子
分享到: 更多 (0)

评论 4

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
  1. #1

    这是评论测试

    DUX主题小秘7个月前 (09-17)回复
    • 飞歌导航东方红烦得很

      themebetter主题小秘3周前 (04-08)回复
  2. #2

    这是新的一条评论

    DUX主题小秘7个月前 (09-17)回复
  3. #3

    未来一年,中国的移动支付市场会是什么样?

    DUX主题小秘7个月前 (09-17)回复

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏

大前端WP主题 更专业 更方便

联系我们 联系我们