别找了,小程序实现isbn扫码获取图书信息2022最新可用(保姆级教程内附接口)

您所在的位置:网站首页 微信小程序扫一扫代码怎么扫 别找了,小程序实现isbn扫码获取图书信息2022最新可用(保姆级教程内附接口)

别找了,小程序实现isbn扫码获取图书信息2022最新可用(保姆级教程内附接口)

2024-07-09 05:21| 来源: 网络整理| 查看: 265

关注、收藏、点赞3连😀!!! 关注、收藏、点赞3连😀!!!在这里插入图片描述

文章目录 前言一、开始抓某网站isbn接口(由于版权原因,分析直接跳过)二、某接口最新可用Tp5代码1.后端部署代码2.使用方法 三、对接小程序实现扫码获取图书详情!1.预览2.代码介绍2.完整Wxml代码3.完整js代码4.完整wxss代码 总结

前言

大家好,这篇文章详细的介绍了isbn接口的使用,可以实现自给自足,不用去网上买接口,博主最近在开发芒果校园参加比赛,二手市场若能直接扫描isbn获取图书详情那真的是很方便,于是在网上看到了很多接口,我们来看一下我的踩坑经历:由于版权原因 现在直接上成品 目前有以下获取到isbn接口的办法:

1、某数据,我这里不放链接了,要花钱,那我肯定不干,众所周知我是个白嫖怪,能动手绝不花钱!! 在这里插入图片描述 2、某瓣,免费还有各种参数,没错是我想要的 在这里插入图片描述

提示:以下是本篇文章正文内容,介绍如何抓取某瓣的接口,由于版权原因,分析直接跳过

一、开始抓某网站isbn接口(由于版权原因,分析直接跳过)

由于版权原因,分析直接跳过

由于版权原因,分析直接跳过

二、某接口最新可用Tp5代码 1.后端部署代码 //图书接口测试 public function getBookInfo() { $isbn = $_GET['isbn']; try { $surl = 'https://book.douban.com/isbn/' . $isbn . '/'; $headers = json_encode(get_headers($surl), true); $headers = json_encode($headers, true); $surl = $this->cut($headers, 'Location: ', '"'); $surl = str_replace('\\', '', $surl);//302地址 $data = $this->getIsbn($surl); $data_1 = $this->cut($data, 'application/ld+json">', ''); $data_1 = json_decode($data_1, true); $res['title'] = $data_1['name'];//书名 $res['logo'] = $this->cut($data, 'data-pic="', '"');//图标 $author = $data_1['author']; if (!isset($author[0]) || $author[0] == '') { $author[0]['name'] = '未知'; } $res['author'] = $author;//作者 //相关书籍推荐 $publisher = $this->cut($data, '出版社:', ''); if ($publisher == '') { $publisher = '未知'; } $res['publisher'] = $publisher;//出版社 $author_desc = $this->cut($data, 'class="indent ">', ''); $res['author_desc'] = $this->cut($author_desc, '

', '

'); if ($res['author_desc'] == "") { $res['author_desc'] = '未知'; } $res['author_desc'] = $author_desc;//作者简介 $published = $this->cut($data, '出版年:', ''); if ($published == '') { $published = '未知'; } $res['published'] = $published;//出版年 $page = $this->cut($data, '页数:', ''); if ($page == '') { $page = '未知'; } $res['page'] = $page;//页数 $price = $this->cut($data, '定价:', ''); if ($price == '') { $price = '未知'; } $res['price'] = $price;//定价 $designed = $this->cut($data, '装帧:', ''); if ($designed == '') { $designed = '未知'; } $res['designed'] = $designed;//装帧 $description = $this->cut($data, 'class="intro">', '

'); if ($description == '') { $description = '未进行描述'; } else { $description = explode('

', $description)[1]; } $res['description'] = $description;//简介 // return_msg(200, '请求成功', $res); return json($res); } catch (Exception $e) { return_msg(500, '服务器内部错误', $e); } } private function cut($content, $start, $end) { $r = explode($start, $content); if (isset($r[1])) { $r = explode($end, $r[1]); return $r[0]; } return ''; } private function getIsbn($url) //curl get请求 { $postUrl = $url; $curlPost = 'GET'; $curl = curl_init();//初始化curl curl_setopt($curl, CURLOPT_URL, $postUrl);//抓取指定网页 curl_setopt($curl, CURLOPT_HEADER, 0);//设置header curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上 curl_setopt($curl, CURLOPT_POST, 1);//post提交方式 curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); //不验证证书下同 curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); $data = curl_exec($curl);//运行curl curl_close($curl); return $data; } 2.使用方法

访问:https://你的域名/Index/Api/getBookInfo?isbn=97****96898

上面的是我的目录结构,具体自己放在哪,改一下即可!! 然后看一下运行结果: 在这里插入图片描述

ok,没问题,有一点不足,就是会吃内存资源,如果只是需要对数据库中的isbn做一次采集,可以用python加一个代理池即可(频繁访问会被拉黑!)

三、对接小程序实现扫码获取图书详情! 1.预览

在这里插入图片描述

2.代码介绍

通过wxml中点击scanCode的方法

一键扫描识别

来到js方法中:

scanCode: function (event) { console.log(1) let that=this; // 允许从相机和相册扫码 wx.scanCode({ onlyFromCamera:true, scanType:['barCode'], success:res=>{ console.log(res.result) that.get_isbn(res.result); }, fail:err=>{ console.log(err); } }) },

微信提供 wx.scanCode接口调用即可获取扫码信息,下图是控制台输出的结果 在这里插入图片描述 获取到后调用方法get_isbn()

that.get_isbn(res.result);

最后对刚刚的接口做一次requests访问请求,写入AppData即可

get_isbn:function(ee){ let that=this; wx.request({ url: 'https://你的域名/Index/Api/getBookInfo', //仅为示例,并非真实的接口地址 data: { isbn:ee }, header: { 'content-type': 'application/json' // 默认值 }, success (res) { console.log(res.data) that.setData({ tp_list:res.data }) } }) }, 2.完整Wxml代码


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3