落網數據庫簡單查詢接口 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 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM