落网数据库简单查询接口 caddy+php7+mongodb
落网数据库简单查询接口
一个简单的DEMO,使用了caddy + php7 + mongodb
数据库&接口设计 来自 https://github.com/Aedron/Luoo.spider 项目(V1.0版本分支)
参考地址:https://www.cnblogs.com/edit/p/luoo-service_caddy-php7-mongodb.html
环境配置:
下载程序,新建一个目录,比如 C:\web
https://caddyserver.com/download 下载caddy并解压到文件夹内,比如 C:\web\caddy_v1.0.0_windows_amd64
https://www.php.net/downloads.php 下载PHP7.3最新版并解压到文件夹内,比如 C:\web\php-7.3.5-nts-Win32-VC15-x64
https://www.mongodb.com/download-center/community 下载MongoDB社区版zip包 并解压到文件夹内,比如 C:\web\mongodb-win32-x86_64-2008plus-ssl-4.0.9
配置&启动脚本:
【PHP 安装mongodb拓展】
https://pecl.php.net/package/mongodb 下载最新的stable版本,比如现在是1.5.3,点击链接 “dll” ,在弹出的新页面里找到对应你PHP版本的地址然后下载。
下载完成后把 tgz包里面的 php_mongodb.dll 解压到 php 目录下的 ext 文件夹内。
找到 php目录下的 php.ini-production,复制一份并重命名为 php.ini ,打开文件加入两行配置
extension_dir = "ext"
extension = mongodb
【caddy 配置&启动脚本】
caddy目录下新建 root 文件夹,用来存放php或html页面
caddy目录下新建文本文件 Caddyfile (不需要后缀名),写入
访问域名 {
gzip
tls 你的邮箱 #如果不需要https可删除此行
root C:\web\root
errors {
404 404.html # Not Found
}
on startup _php_cgi.bat &
fastcgi php 127.0.0.1:6545 php
rewrite /luoo/ {
to {path} {path}/ /luoo.php?_url={path}&{query}
}
header /luoo {
Access-Control-Allow-Origin *
Access-Control-Allow-Methods "GET, POST, OPTIONS"
}
header / {
-Server
-X-Powered-By
}
}
caddy目录下新建文本文件 _php_cgi.bat,写入
:start
C:\web\php-7.3.-nts-Win32-VC15-x64\php-cgi.exe -b
goto start
然后就可以 双击caddy.exe 启动了,或者是配置成windows服务使用。
【Mongodb 配置&导入bson文件】
Mongodb 目录下新建 data 文件夹
Mongodb 目录下新建文本文件 _start.bat,写入
title MongoDB
cd bin
mongod --dbpath C:\web\mongodb-win32-x86_64-2008plus-ssl-4.0.\data
pause
双击 _start.bat ,即可启动 Mongodb 。
Mongodb 目录下新建文本文件 _import.bat,写入
title MongoDB import
echo MongoDB import
cd bin
mongorestore -d luoo C:\web\mongodb-win32-x86_64-2008plus-ssl-4.0.\luoo
pause
顺便去github上下载一份落网数据文件,地址在 https://github.com/Aedron/Luoo.spider/tree/master/db/luoo
下载完成后,把 db目录下的 luoo 文件夹拷贝到 Mongodb 目录下,然后运行上面的 _import.bat 脚本,即可完成数据导入。
PHP代码编写:
在 C:\web\root 目录下新建 luoo.php,写入
<?php
header('Content-Type:application/json; charset=utf-8');
if(empty($_GET['_url'])){
exit(json_encode(['code'=>'400','msg'=>'访问异常']));
}
$router = explode('/',str_replace('/luoo/','',$_GET['_url']));
define('API_LIST', ['vol','vols','single','singles','article','latest']);
if(!in_array($router[0],API_LIST)){
exit(json_encode(['code'=>'404','msg'=>'接口不存在']));
}
class Luoo{
private $manager;
private $config = ["latestVol"=>997,"latestSingle"=>20170721,"latestArticle"=>927];
public function __construct(){
$this->manager = new MongoDB\Driver\Manager();
}
private function getCount($collection,$where=[]){
$command = new MongoDB\Driver\Command(['count' => $collection,'query'=>$where]);
$result = $this->manager->executeCommand('luoo',$command);
$response = current($result->toArray());
if($response->ok==1){
return $response->n;
}
return 0;
}
private function getLyric($param){
$query = new MongoDB\Driver\Query($param,['projection'=>['_id' => 0],'limit'=>100]);
$cursor = $this->manager->executeQuery('luoo.lyrics', $query);
return $cursor->toArray();
}
public function vol(int $vol){
$query = new MongoDB\Driver\Query(['vol'=>$vol], ['projection'=>['_id' => 0],'limit'=>1]);
$cursor = $this->manager->executeQuery('luoo.vols', $query);
$result = ['code'=>200];
$result['data'] = current($cursor->toArray());
if($result['data'] && $result['data']->id){
$volId = $result['data']->id;
$result['data']->lyrics = $this->getLyric(['volId'=>$volId]);
}
echo json_encode($result);
}
public function vols(int $date){
$page = $_GET['page']??1;
$where = ['vol'=>['$gte'=>$date]];
$options = ['projection'=>['_id' => 0],'limit'=>10,'sort'=>['vol' => 1]];
if($page>1) $options['skip'] = ($page-1)*10;
$count = $this->getCount('singles',$where);
$query = new MongoDB\Driver\Query($where, $options);
$cursor = $this->manager->executeQuery('luoo.vols', $query);
$result = ['code'=>200];
$result['page'] = $page;
$result['total'] = $count;
$result['data'] = $cursor->toArray();
echo json_encode($result);
}
public function single(int $date){
$query = new MongoDB\Driver\Query(['date'=>$date],['projection'=>['_id' => 0],'limit'=>1]);
$cursor = $this->manager->executeQuery('luoo.singles', $query);
$result = ['code'=>200];
$result['data'] = current($cursor->toArray());
if($result['data'] && $result['data']->id){
$result['data']->lyrics = $this->getLyric(['type'=>1,'date'=>$date]);
}
echo json_encode($result);
}
public function singles(int $date){
$page = $_GET['page']??1;
$where = ['date'=>['$gte'=>$date]];
$options = ['projection'=>['_id' => 0],'limit'=>10,'sort'=>['date' => 1]];
if($page>1) $options['skip'] = ($page-1)*10;
$count = $this->getCount('singles',$where);
$query = new MongoDB\Driver\Query($where, $options);
$cursor = $this->manager->executeQuery('luoo.singles', $query);
$result = ['code'=>200];
$result['page'] = $page;
$result['total'] = $count;
$result['data'] = $cursor->toArray();
echo json_encode($result);
}
public function article(int $id){
$query = new MongoDB\Driver\Query(['id'=>$id], ['projection'=>['_id' => 0],'limit'=>1]);
$cursor = $this->manager->executeQuery('luoo.articles', $query);
$result = ['code'=>200];
$result['data'] = current($cursor->toArray());
if($result['data'] && $result['data']->id){
$articleId = $result['data']->id;
$result['data']->lyrics = $this->getLyric(['articleId'=>$articleId]);
}
echo json_encode($result);
}
public function latest(string $type){
switch (strtolower($type)) {
case 'vol':
$this->vol($this->config['latestVol']);
break;
case 'single':
$this->single($this->config['latestSingle']);
break;
case 'article':
$this->article($this->config['latestArticle']);
break;
default:
echo json_encode(['code'=>'400','msg'=>'允许的 type 为 vol 或 single 或 article']);
break;
}
}
}
try {
$LUOO = new Luoo();
$LUOO->{$router[0]}($router[1]);
}
catch (TypeError $ex) { exit(json_encode(['code'=>'400','msg'=>'请求参数有误']));}
catch (Error $ex) { exit(json_encode(['code'=>'500','msg'=>'服务器内部错误']));}
API列表如下:
/vol/<volIndex>
根据期刊数来获取期刊数据, /vol/717
/vols/<volIndex>
根据当前期刊数来获取该期刊之后的所有新的期刊数据, 如 /vols/926 /single/<singleDate>
根据发布日期来获取单曲数据, 如 /single/20160722
/singles/<singleDate>
根据当前发布日期来获取该发布日期之后的所有新的单曲数据, 如 /singles/20170628 /article/<articleIndex>
根据文章id来获取文章数据, 如 /article/922 /latest/<type>
获取最新的期刊或单曲或文章, 允许的 type 为 vol 或 single 或 article, 如 /latest/vol
来源地址: https://www.cnblogs.com/edit/p/10880194.html
落网数据库简单查询接口 caddy+php7+mongodb的更多相关文章
- Spring Data JPA 简单查询--接口方法
一.接口方法整理速查 下表针对于简单查询,即JpaRepository接口(继承了CrudRepository接口.PagingAndSortingRepository接口)中的可访问方法进行整理.( ...
- Spring Data JPA简单查询接口方法速查
下表针对于简单查询,即JpaRepository接口(继承了CrudRepository接口.PagingAndSortingRepository接口)中的可访问方法进行整理.(1)先按照功能进行分类 ...
- 2018-08-24 中文代码之Spring Boot对H2数据库简单查询
续前文: 中文代码之Spring Boot集成H2内存数据库 在词条中添加英文术语域: @Entity public class 词条 { @Id private long id; private S ...
- mysql学习 | LeetCode数据库简单查询练习
力扣:https://leetcode-cn.com/ 力扣网数据库练习:https://leetcode-cn.com/problemset/database/ 文章目录 175. 组合两个表 题解 ...
- MySQL数据库简单查询
--黑马程序员 DQL数据查询语言 数据库执行DQL语句不会对数据进行改变,而是让数据库发送结果集给客户端.查询返回的结果集是一张虚拟表. 查询关键字:SELECT 语法: SELECT 列名 FRO ...
- 数据库 简单查询 Sql Server 学生表 课程表 选课表
创建教材中的三张表格,并输入相应的数据 Create table student( Sno char(9), Same char(20), Ssex char(2), Sage smallint, S ...
- Oracle 数据库 简单查询
select DISTINCT dept_id from s_emp; desc s_emp; ; --给入职3年以上员工发10万元年终奖 ; --列出职位是仓库管理员的名字和工资 select la ...
- 在MongoDB数据库中查询数据(上)
在MongoDB数据库中查询数据(上) 在MongoDB数据库中,可以使用Collection对象的find方法从一个集合中查询多个数据文档,find方法使用方法如下所示: collection.fi ...
- 前端笔记之NodeJS(四)MongoDB数据库&Mongoose&自制接口&MVC架构思想|实战
一.MongoDB数据库 1.1 NoSQL简介 随着互联网web2.0网站的兴起,传统的SQL数据库(关系数据库)在应付web2.0网站,特别是超大规模和高并发的SNS(social network ...
随机推荐
- 以字符串为例,谈谈Python到底要学到什么程度
古语云:慈不掌兵,义不为商:离商业越近,离人性越远:我们在自学数据科学时,一定会辅助一些书籍或者视频来学习,怎么学习,选择哪些资料来学习?这时,我们都要理解好第一句话,理解不好,浪费钱是次要,重要的是 ...
- Ubuntu 14.04 更换为阿里云源
#备份sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak#编辑sudo vim /etc/apt/sources.list,清空后,加入以下 ...
- shell 重定向0,1,2
.1和2分别表示标准输入.标准输出和标准错误信息输出,可以用来指定需要重定向的标准输入或输出,比如 >a.txt 表示将错误信息输出到文件a.txt中. #将1,2输出转发给/dev/null设 ...
- Robot Framework(十六) 扩展RobotFramework框架——使用监听器接口
4.3使用监听器接口 Robot Framework有一个侦听器接口,可用于接收有关测试执行的通知.监听器是具有某些特殊方法的类或模块,它们可以用Python和Java实现.监听器接口的示例用法包括外 ...
- 下板不动, 上板匀速平板间流动(Crank-Nicolson格式)【转载】
摘自<FLUENT流体工程仿真计算实例与分析>,程序略有修改 两个间距为1cm水平平板,如下图所示: 上板匀速平板间流动(Crank-Nicolson格式)[转载]"> 充 ...
- HearthAgent A Hearthstone agent
http://www.intelligence.tuc.gr/~robots/ARCHIVE/2015w/Projects/LAB51326833/download.html The project ...
- Linux中查看系统资源占用情况的命令
用 'top -i' 看看有多少进程处于 Running 状态,可能系统存在内存或 I/O 瓶颈,用 free 看看系统内存使用情况,swap 是否被占用很多,用 iostat 看看 I/O 负载情况 ...
- Java synchronized和Lock
Synchronized 1. 将synchronized加在方法上, 即可实现对此方法的同步 public synchronized void deposit(float amt) { float ...
- python Image 模块处理图片
Python-Image 基本的图像处理操作,有需要的朋友可以参考下. Python 里面最常用的图像操作库是 pip install Pillow #安装模块 from PIL import Ima ...
- WordPress的默认循环
WordPress的默认循环是相对我们上一篇的WordPress自定义循环而言的,默认循环是根据链接结构的来查询数据的. 我们知道WordPress模板文件就是根据文件名来找寻模板的,这里的链接结构也 ...