用户组 
易积分6645
热心0
好评15
|
本帖最后由 十一 于 2018-6-19 12:17 编辑
什么是毒汤日历毒汤日历是一本有毒的日历,每天用毒鸡汤来唤醒你。
你甚至不用打开日历,打开 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("binduyao.txt", "a+");//创建文件保存抓取的句子
- //循环次数 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 多条。
搭建 API- //获取句子文件的绝对路径
- //如果你介意别人可能会拖走这个文本,可以把文件名自定义一下,或者通过Nginx禁止拉取也行。
- $path = dirname(__FILE__);
- $file = file($path."/binduyan.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';
- }
- //格式化判断,输出js或纯文本
- if ($_GET['encode'] === 'js') {
- echo "function binduyan(){document.write('" . $content ."');}";
- } else {
- echo $content;
- }
复制代码
使用方法将 API 代码保存为 index.php 与 binduyan.txt 上传到网站 binduyan 目录内
请求地址
http://你的域名/binduyan/(返回随机一句)
http://你的域名/binduyan/index.php/?encode=js(返回 js 格式)
博客集成- <script type="text/javascript" src="http://你的域名/binduyan/index.php/?encode=js&charset=utf-8"></script>
- <div id="binduyan"><script>binduyan()</script></div>
复制代码
自用 API毒言
APihttp://duyan.fooor.cn/word.php
[td]参数 | 值 | encode | 默认返回文本格式; js 返回为 JS 格式 | charset | 默认 UTF-8; GBK 返回 GBk 格式 |
举个栗子http://duyan.fooor.cn/word.php/?encode=js&charset=utf-8
古诗
APihttp://duyan.fooor.cn/word.php
[td]参数 | 值 | type | d (必选) | encode | 默认返回文字; js 返回为 JS 格式 | charset | 默认 UTF-8 ; GBK 返回 GBk 格式 | author | 默认返回作者; 1 返回作者 |
举个栗子http://duyan.fooor.cn/word.php/?type=d&author=1&charset=utf-8
php 初学者 如有错误请见谅 到此结束
本文为FOOOR.CN,转载请注明来源。
|
评分
-
查看全部评分
|