圖書信息保存
現(xiàn)在我們已經(jīng)能夠通過調(diào)用微信公眾號的掃碼藏書功能獲取到我們的藏書信息没炒,但是這些信息僅僅在微信公眾號上進(jìn)行了顯示帜矾,沒有保存下來豺型,為了將我們藏書保存后續(xù)查看,還需要將這些圖書信息記錄到數(shù)據(jù)庫中饼齿。
在之前安裝的xampp中包含了mysql數(shù)據(jù)庫饲漾,只需要在xampp-control開啟mysql服務(wù)就可以使用。
打開后在瀏覽器中輸入http://127.0.0.1/phpmyadmin 就可以進(jìn)入mysql的控制界面缕溉。
通過左側(cè)的新建按鈕新建一個數(shù)據(jù)庫wxbook.
在數(shù)據(jù)庫wxbook中增加一個表wb_bookinfo考传,表結(jié)構(gòu)可以使用phpMyAdmin圖形化方式創(chuàng)建,也可以使用SQL語句創(chuàng)建证鸥。
建表SQL語句
CREATE TABLE `wxbook`.`wb_bookinfo` (
`isbn10` VARCHAR(20)NULL DEFAULT NULL ,
`isbn13` VARCHAR(20) NOT NULL ,
`title`VARCHAR(200) NULL DEFAULT NULL ,
`author`VARCHAR(100) NULL DEFAULT NULL ,
`price` FLOAT(4) NULL DEFAULT NULL ,
`image`VARCHAR(256) NULL DEFAULT NULL ,
`translator` VARCHAR(100) NULL DEFAULT NULL,
`publisher` VARCHAR(100) NULL DEFAULT NULL ,
`summary` VARCHAR(1024) NULLDEFAULT NULL ,
`rating` DECIMAL(4) NULL DEFAULT NULL ,
`createtime` DATETIMENOT NULL DEFAULT CURRENT_TIMESTAMP )
ENGINE = InnoDB COMMENT = 'book information’;
在處理scancode_waitmsg的消息中僚楞,增加入庫動作,代碼如下:
case "scancode_waitmsg":
$isbn = substr($object->ScanCodeInfo->ScanResult, 7);
$content = "isbn:" . $isbn;
$douBanObj = new DoubanApi();
$bookInfo = $douBanObj->getDoubanInfo($isbn);
$image = $bookInfo["images"]["large"];
if (is_array($bookInfo['author']) && sizeof($bookInfo['author']) > 0) {
$bookInfo['author'] = $bookInfo['author'][0];
} else {
$bookInfo['author'] = '';
}
$content = array();
$summary = mb_substr($bookInfo['summary'], 0, 200, 'utf-8') . '...';
$content[] = array(
"Title" => "已成功添加:" . $bookInfo["title"],
"Description" => $summary,
"PicUrl" => $image,
"Url" => "",
);
/*將圖書信息入庫處理*/
$db= new Wb_db();
$db->connect("localhost", "root", "");
$db->selectDB('wxbook');
$bi = new Wb_bookinfo($db);
$bi-> insertBookInfo($bookInfo);
$db->closeDB();
處理mysql入庫動作的類代碼如下:
class Wb_db
{
public $conn = null;
public function connect($host, $user, $password)
{
//$con = mysqli_connect("localhost", "root", "");
$con = mysqli_connect($host, $user, $password);
if (!$con) {
die('Could not connect: ' . mysql_error());
}
$this->conn = $con;
return $con;
}
public function selectDB($database)
{
if (!$this->conn) {
die('Could not connect: ' . mysql_error());
return;
}
$program_char = "utf8";
mysqli_select_db($this->conn,$database);
mysqli_query($this->conn,"SET NAMES 'utf8'");
mysqli_set_charset($this->conn,$program_char);
//$charset = mysqli_client_encoding($this->conn);
}
public function queryDB($sql)
{
if (!$this->conn) {
die('Could not connect: ' . mysql_error());
return;
}
$result = mysqli_query($this->conn,$sql);
if (!$result) {
die('Error: ---' . $this->conn->errno.':'.$this->conn->error);
return false;
} else {
echo "excute success";
return $result;
}
}
public function closeDB()
{
if (!$this->conn) {
die('Could not connect: '.mysql_errno.":".mysql_error());
return;
}
mysqli_close($this->conn);
}
}
class Wb_bookinfo
{
public $db = null;
public function __construct($database)
{
$this->db = $database;
}
public function insertBookInfo($bookInfo)
{
if ($bookInfo['title'] == '' or $bookInfo['isbn13'] == '') {
return false;
}
if (is_array($bookInfo['author']) && sizeof($bookInfo['author']) > 0) {
$bookInfo['author'] = $bookInfo['author'][0];
} else {
$bookInfo['author'] = '';
}
if (is_array($bookInfo['translator'])) {
if (count($bookInfo['translator']) > 0) {
$bookInfo['translator'] = $bookInfo['translator'][0];
} else {
$bookInfo['translator'] = '';
}
}
if (mb_strlen($bookInfo['summary'], "UTF-8") > 500) {
$summary = mb_substr($bookInfo['summary'], 0, 499, 'utf-8') . '...';
} else {
$summary = $bookInfo['summary'];
}
#var_dump($bookInfo);
$sql = sprintf("replace INTO wb_bookinfo(isbn10, isbn13,title,author,price,image,translator,
publisher,summary,rating)
VALUES ('%s','%s','%s','%s',%f,'%s','%s','%s','%s',%f)",
$bookInfo["isbn10"],
$bookInfo["isbn13"],
$bookInfo["title"],
$bookInfo["author"],
$bookInfo["price"],
$bookInfo["image"],
$bookInfo["translator"],
$bookInfo["publisher"],
$summary,
$bookInfo['rating']['average']);
$query = $this->db->queryDB($sql);
return $query;
}
}
增加圖書信息入功能后枉层,在掃碼以后就可以將信息同步存入數(shù)據(jù)庫中泉褐,方便后續(xù)使用。