Permalink
Browse files

第一版

  • Loading branch information...
bu committed Mar 24, 2013
1 parent 75b8b70 commit 25bcbca643a3e008d092a291908d938e55f164c8
Showing with 134 additions and 3 deletions.
  1. +7 −3 README.md
  2. +19 −0 for_log.php
  3. +108 −0 search.php
View
@@ -1,4 +1,8 @@
-FJU_OPAC
-========
+FJU Library OPAC API
+==================================
-存取輔仁大學圖書館公開線上目錄的 API
+這是用來存取輔仁大學圖書館 OPAC 系統的 API 元件
+
+系統要求
+-------------------
+最少需要 PHP 5.4 版本
View
@@ -0,0 +1,19 @@
+<?php
+
+require "./search.php";
+
+use FJU\OPAC as OPAC;
+
+$search = OPAC\query(OPAC\TYPE_KEYWORD, "哈利");
+
+// 取得筆數
+echo "筆數" . $search->getCount();
+
+// 取得頁數
+echo "總頁數" . $search->getPageCount();
+
+// 每一頁的位移值
+echo "位移值" . $search->getPageOffset();
+
+// 取得第n頁的清單, array of book_id
+var_dump( $search->getPage(8) );
View
@@ -0,0 +1,108 @@
+<?php
+
+namespace FJU\OPAC;
+
+const TYPE_KEYWORD = "Y";
+
+function query($query_type, $keyword) {
+ return ( new SearchResult() )->setType($query_type)->setKeyword($keyword)->exec();
+}
+
+class SearchResult {
+ protected $count = 0;
+
+ protected $pages = [
+ "count" => 1,
+ "offset" => 12
+ ];
+
+ protected $type = TYPE_KEYWORD;
+
+ // raw text for the keyword ( 哈利 )
+ protected $keyword = "";
+ // ucs string ( {u54C8}{u5229} )
+ protected $keyword_ucs = "";
+ // utf-8 encoded ( %E5%93%88%E5%88%A9 )
+ protected $keyword_utf8_encoded = "";
+
+
+
+
+
+ public function setType($query_type) {
+ $this->type = $query_type;
+
+ return $this;
+ }
+
+ public function setKeyword($keyword) {
+ $this->keyword = $keyword;
+
+ return $this;
+ }
+
+ protected function _getURLContent($url) {
+ return file_get_contents($url);
+ }
+
+ public function exec() {
+ // we fetch for the first page get the results
+ $url = "http://140.136.208.1/search~S0*cht/?searchtype=" . $this->type . "&searcharg=" . urlencode($this->keyword);
+
+ $content = $this->_getURLContent($url);
+
+ // ---------------------
+
+ $matches = [];
+
+ $result = preg_match('/<a\ href="\/search\~S0\*cht\/X\?NOSRCH=([{}0-9a-zA-Z]+)&SORT=D&SUBKEY=([A-Z\%0-9]+)">/', $content, $matches);
+
+ if($result > 0) {
+ $this->keyword_ucs = $matches[1];
+ $this->keyword_utf8_encoded = $matches[2];
+ }
+
+ // -------------------
+
+ $matches = [];
+
+ $result = preg_match('/關鍵字\ \(([0-9]+)\-([0-9]+) 之 ([0-9]+)\)/', $content, $matches);
+
+ if($result > 0) {
+ $this->count = $matches[3];
+
+ $this->pages['count'] = ceil($matches[3] / $matches[2]);
+ $this->pages['offset'] = $matches[2];
+ }
+
+ return $this;
+ }
+
+ public function getCount() {
+ return $this->count;
+ }
+
+ public function getPageCount() {
+ return $this->pages['count'];
+ }
+
+ public function getPageOffset() {
+ return $this->pages['offset'];
+ }
+
+ public function getPage($page) {
+ // we fetch for the first page get the results
+ $offset = 1 + ($page - 1) * $this->pages["offset"];
+
+ $url = "http://140.136.208.1/search~S0*cht?/Y" . $this->keyword_ucs . "&SORT=D/Y" . $this->keyword_ucs . "&SORT=D&SUBKEY=%E4%B8%AD%E6%96%87/". $offset . "%2C" . $this->count . "%2C" . $this->count . "%2CB/browse";
+
+ $content = $this->_getURLContent($url);
+
+ $matches = [];
+
+ echo "\n\n\n\n";
+ preg_match_all("/browse\?save=([a-z0-9]+)#anchor_[0-9]+/", $content, $matches);
+
+ return $matches[1];
+ }
+}

0 comments on commit 25bcbca

Please sign in to comment.