落网数据库简单查询接口

一个简单的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的更多相关文章

  1. Spring Data JPA 简单查询--接口方法

    一.接口方法整理速查 下表针对于简单查询,即JpaRepository接口(继承了CrudRepository接口.PagingAndSortingRepository接口)中的可访问方法进行整理.( ...

  2. Spring Data JPA简单查询接口方法速查

    下表针对于简单查询,即JpaRepository接口(继承了CrudRepository接口.PagingAndSortingRepository接口)中的可访问方法进行整理.(1)先按照功能进行分类 ...

  3. 2018-08-24 中文代码之Spring Boot对H2数据库简单查询

    续前文: 中文代码之Spring Boot集成H2内存数据库 在词条中添加英文术语域: @Entity public class 词条 { @Id private long id; private S ...

  4. mysql学习 | LeetCode数据库简单查询练习

    力扣:https://leetcode-cn.com/ 力扣网数据库练习:https://leetcode-cn.com/problemset/database/ 文章目录 175. 组合两个表 题解 ...

  5. MySQL数据库简单查询

    --黑马程序员 DQL数据查询语言 数据库执行DQL语句不会对数据进行改变,而是让数据库发送结果集给客户端.查询返回的结果集是一张虚拟表. 查询关键字:SELECT 语法: SELECT 列名 FRO ...

  6. 数据库 简单查询 Sql Server 学生表 课程表 选课表

    创建教材中的三张表格,并输入相应的数据 Create table student( Sno char(9), Same char(20), Ssex char(2), Sage smallint, S ...

  7. Oracle 数据库 简单查询

    select DISTINCT dept_id from s_emp; desc s_emp; ; --给入职3年以上员工发10万元年终奖 ; --列出职位是仓库管理员的名字和工资 select la ...

  8. 在MongoDB数据库中查询数据(上)

    在MongoDB数据库中查询数据(上) 在MongoDB数据库中,可以使用Collection对象的find方法从一个集合中查询多个数据文档,find方法使用方法如下所示: collection.fi ...

  9. 前端笔记之NodeJS(四)MongoDB数据库&Mongoose&自制接口&MVC架构思想|实战

    一.MongoDB数据库 1.1 NoSQL简介 随着互联网web2.0网站的兴起,传统的SQL数据库(关系数据库)在应付web2.0网站,特别是超大规模和高并发的SNS(social network ...

随机推荐

  1. AcWing:244. 谜一样的牛(树状数组 + 二分)

    有n头奶牛,已知它们的身高为 1~n 且各不相同,但不知道每头奶牛的具体身高. 现在这n头奶牛站成一列,已知第i头牛前面有AiAi头牛比它低,求每头奶牛的身高. 输入格式 第1行:输入整数n. 第2. ...

  2. JavaWeb_(Spring框架)Spring中IoC与DI概念入门

    Spring是于2003 年兴起的一个轻量级的Java 开源框架,它由Rod Johnson创建.传统J2EE应用的开发效率低,Spring作为开源的中间件,提供J2EE应用的各层的解决方案,Spri ...

  3. 动态拼接tr,th

    var dltable=''; // <c:forEach items="data" var="data" ></c:forEach> ...

  4. ICEM—气体罐子

    原视频下载地址:https://yunpan.cn/cPfzaLzkcFXVK  访问密码 62cf

  5. 5 HashSet

    1.HashSet public class HashSet<E> extends AbstractSet<E> implements Set<E>, Clonea ...

  6. Async and Await (Stephen Cleary)

    https://blog.stephencleary.com/2012/02/async-and-await.html Most people have already heard about the ...

  7. 推送kafka消息失败

    晚上变更 怎么都推不过去,蛋疼,睡饱后加了个hosts没想到好了,然后搜了一下,大概是如下的原因 转自 https://www.cnblogs.com/linlianhuan/p/9258061.ht ...

  8. Flume-自定义 Source 读取 MySQL 数据

    开源实现:https://github.com/keedio/flume-ng-sql-source 这里记录的是自己手动实现. 测试中要读取的表 CREATE TABLE `student` ( ` ...

  9. GetProp和SetProp的区别

    GetProp 函数功能:该函数从给定窗口的属性列表中检索数据句柄.给定的字符串标识了要检索的句柄.该字符串和句柄必须在前一次调用SetProp函数时已经加到属性表中. 函数原型:HANDLE Get ...

  10. BGP多线 有些BGP IP也会被极少数运营商劫持 取Ip逻辑

    小结: 1.租用的服务器只有一个IP,用户的访问路线是由路由器根据访客的实际访问速度选择最优访问路径,来选择访问的.而且不占用任何的服务器资源.服务器的上行和下行都是有路由器来选择最佳的路线,所以这样 ...