落网数据库简单查询接口 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.5-nts-Win32-VC15-x64\php-cgi.exe -b 6545
    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.9\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.9\luoo
pause

顺便去github上下载一份落网数据文件,地址在 https://github.com/Aedron/Luoo.spider/tree/master/db/luoo 

下载完成后,把 db目录下的 luoo 文件夹拷贝到 Mongodb 目录下,然后运行上面的 _import.bat 脚本,即可完成数据导入。

 

PHP代码编写:

在 C:\web\root 目录下新建 luoo.php,写入

  1 <?php
  2 header('Content-Type:application/json; charset=utf-8');
  3 if(empty($_GET['_url'])){
  4     exit(json_encode(['code'=>'400','msg'=>'访问异常']));
  5 }
  6 $router = explode('/',str_replace('/luoo/','',$_GET['_url']));
  7 define('API_LIST', ['vol','vols','single','singles','article','latest']);
  8 if(!in_array($router[0],API_LIST)){
  9     exit(json_encode(['code'=>'404','msg'=>'接口不存在']));
 10 }
 11 class Luoo{
 12     private $manager;
 13     private $config = ["latestVol"=>997,"latestSingle"=>20170721,"latestArticle"=>927];
 14     public function __construct(){
 15         $this->manager = new MongoDB\Driver\Manager();
 16     }
 17     private function getCount($collection,$where=[]){
 18         $command = new MongoDB\Driver\Command(['count' => $collection,'query'=>$where]);
 19         $result = $this->manager->executeCommand('luoo',$command);
 20         $response = current($result->toArray());
 21         if($response->ok==1){
 22             return $response->n;
 23         }
 24         return 0;
 25     }
 26     private function getLyric($param){
 27         $query = new MongoDB\Driver\Query($param,['projection'=>['_id' => 0],'limit'=>100]);
 28         $cursor = $this->manager->executeQuery('luoo.lyrics', $query);
 29         return $cursor->toArray();
 30     }
 31     public function vol(int $vol){
 32         $query = new MongoDB\Driver\Query(['vol'=>$vol], ['projection'=>['_id' => 0],'limit'=>1]);
 33         $cursor = $this->manager->executeQuery('luoo.vols', $query);
 34         $result = ['code'=>200];
 35         $result['data'] = current($cursor->toArray());
 36         if($result['data'] && $result['data']->id){
 37             $volId = $result['data']->id;
 38             $result['data']->lyrics = $this->getLyric(['volId'=>$volId]);
 39         }
 40         echo json_encode($result);
 41     }
 42     public function vols(int $date){
 43         $page = $_GET['page']??1;
 44         $where = ['vol'=>['$gte'=>$date]];
 45         $options = ['projection'=>['_id' => 0],'limit'=>10,'sort'=>['vol' => 1]];
 46         if($page>1) $options['skip'] = ($page-1)*10;
 47         $count = $this->getCount('singles',$where);
 48         $query = new MongoDB\Driver\Query($where, $options);
 49         $cursor = $this->manager->executeQuery('luoo.vols', $query);
 50         $result = ['code'=>200];
 51         $result['page'] = $page;
 52         $result['total'] = $count;
 53         $result['data'] = $cursor->toArray();
 54         echo json_encode($result);
 55     }
 56     public function single(int $date){
 57         $query = new MongoDB\Driver\Query(['date'=>$date],['projection'=>['_id' => 0],'limit'=>1]);
 58         $cursor = $this->manager->executeQuery('luoo.singles', $query);
 59         $result = ['code'=>200];
 60         $result['data'] = current($cursor->toArray());
 61         if($result['data'] && $result['data']->id){
 62             $result['data']->lyrics = $this->getLyric(['type'=>1,'date'=>$date]);
 63         }
 64         echo json_encode($result);
 65     }
 66     public function singles(int $date){
 67         $page = $_GET['page']??1;
 68         $where = ['date'=>['$gte'=>$date]];
 69         $options = ['projection'=>['_id' => 0],'limit'=>10,'sort'=>['date' => 1]];
 70         if($page>1) $options['skip'] = ($page-1)*10;
 71         $count = $this->getCount('singles',$where);
 72         $query = new MongoDB\Driver\Query($where, $options);
 73         $cursor = $this->manager->executeQuery('luoo.singles', $query);
 74         $result = ['code'=>200];
 75         $result['page'] = $page;
 76         $result['total'] = $count;
 77         $result['data'] = $cursor->toArray();
 78         echo json_encode($result);
 79     }
 80     public function article(int $id){
 81         $query = new MongoDB\Driver\Query(['id'=>$id], ['projection'=>['_id' => 0],'limit'=>1]);
 82         $cursor = $this->manager->executeQuery('luoo.articles', $query);
 83         $result = ['code'=>200];
 84         $result['data'] = current($cursor->toArray());
 85         if($result['data'] && $result['data']->id){
 86             $articleId = $result['data']->id;
 87             $result['data']->lyrics = $this->getLyric(['articleId'=>$articleId]);
 88         }
 89         echo json_encode($result);
 90     }
 91     public function latest(string $type){
 92         switch (strtolower($type)) {
 93             case 'vol':
 94                 $this->vol($this->config['latestVol']);
 95                 break;
 96             case 'single':
 97                 $this->single($this->config['latestSingle']);
 98                 break;
 99             case 'article':
100                 $this->article($this->config['latestArticle']);
101                 break;
102             default:
103                 echo json_encode(['code'=>'400','msg'=>'允许的 type 为 vol 或 single 或 article']);
104                 break;
105         }
106     }
107 }
108 try {
109     $LUOO = new Luoo();
110     $LUOO->{$router[0]}($router[1]);
111 }
112 catch (TypeError $ex) { exit(json_encode(['code'=>'400','msg'=>'请求参数有误']));}
113 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 

 

posted @ 2019-05-17 15:04  _st  阅读(2259)  评论(0编辑  收藏  举报