落网数据库简单查询接口

一个简单的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. WEB测试重点及视频教程

    WEB测试重点如下: 1.WEB测试基础-2.理解网络协议-3.HTTP协议详解-4.WEB前段分析-5WEB安全性测试-6.WEB兼容性及可用性测试. 1.通常需要承受长时间的大量操作,因此web项 ...

  2. C语言学习笔记5-程序结构

    本系列文章由jadeshu编写,转载请注明出处.http://blog.csdn.net/jadeshu/article/details/50752148 作者:jadeshu   邮箱: jades ...

  3. AGC023C Painting Machines

    题意 有一排\(n\)个格子,\(i\)操作会使\(i\)和\(i+1\)都变黑. 一个操作序列的得分为染黑所有格子时所用的步数 问所有排列的得分和. \(n\le 10^6\) 传送门 思路 有一个 ...

  4. mysql 常见面试题

    附录: https://mp.weixin.qq.com/s/pC0_Y7M7BkoUmlRwneZZdA 一.为什么用自增列作为主键 1.如果我们定义了主键(PRIMARY KEY),那么InnoD ...

  5. 蚁群算法求解TSP问题

    一.蚁群算法简介 蚁群算法是对自然界蚂蚁的寻径方式进行模似而得出的一种仿生算法:蚂蚁在运动过程中,能够在它所经过的路径上留下信息素(pheromone)的物质进行信息传递,而且蚂蚁在运动过程中能够感知 ...

  6. SpringMVC返回Map类型转换成JSON失败

    错误信息:WARN DefaultHandlerExceptionResolver:380 - Failed to write HTTP message: org.springframework.ht ...

  7. Spring Security整合JWT,实现单点登录,So Easy~!

    前面整理过一篇 SpringBoot Security前后端分离,登录退出等返回json数据,也就是用Spring Security,基于SpringBoot2.1.4 RELEASE前后端分离的情况 ...

  8. Centos - php5.4升级到7.1 yum安装

    查看当前 PHP 版本 1 php -v 查看当前 PHP 相关的安装包,删除之 1 2 3 4 5 yum list installed | grep php   yum remove php   ...

  9. 性能调优 | 如何通过性能调优突破 MySQL 数据库性能瓶颈?

    本文出自头条号老王谈运维,转载请说明出处. MySQL 数据库瓶颈对 DBA 程序员而言,是非常棘手的问题.要正确的优化SQL,我们需要快速定位能性的瓶颈点,也就是说快速找到我们SQL主要的开销在哪里 ...

  10. java time

    package cn.itcast_04;       import java.text.SimpleDateFormat;   import java.util.Date;       public ...