落网数据库简单查询接口

一个简单的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. 内存管理2-set方法的内存管理-程序解析

    创建class Book .h 有@ property float price;  //@synthesize 自动 ------------ 创建class Student #import &quo ...

  2. java试题复盘——11月25日

    上: 11.下列表述错误的是?(D) A.int是基本类型,直接存数值,Integer是对象,用一个引用指向这个对象. B.在子类构造方法中使用super()显示调用父类的构造方法,super()必须 ...

  3. 以太坊 Geth 环境搭建(Ubuntu)

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/u014409380/article/details/79897335 时隔多日,我又想起来更新博客, ...

  4. web worker 的 self

    A self object, which is the global object representing the worker in this scope. 对self对象的译法,未知妥否. // ...

  5. react对字符串转义成html并渲染

    <div dangerouslySetInnerHTML={{__html: "字符串内容"}} />  

  6. Git Command之Code Review

    原文链接 准备 Step 1. Create a team and add a teammate Step 2. Create a repository with some content 应用 Cl ...

  7. centos下使用virtualenv建立python虚拟环境

    在centos使用python3的virtualenv,先用yum install python3 安装后pip3也已经安装好了,pip3 install virtualenv, 在系统中新建一个空文 ...

  8. 为Django添加图片验证码

    可直接复制到Django项目中使用 # author:sunshine from django.http import HttpResponse from PIL import Image, Imag ...

  9. IIS中应用Application Request Route 配置负载均衡

    转自:https://blog.csdn.net/wucong60/article/details/84930234 简介ApplicationRequest Route(后面简称为ARR)是一个寄宿 ...

  10. webdriervAPI(鼠标事件)

    from  selenium  import  webdriver from selenium.webdriver.common.action_chains import ActionChains 导 ...