宝塔机新建一个网站:
- php版本,切换7.3。
网站根目录下新建一个index.php文件。
index.php源代码:
-<?php //剪切 function cut($content,$start,$end) { $r = explode($start, $content); if (isset($r[1])) { $r = explode($end, $r[1]); return $r[0]; } return ''; } //模拟get请求 function get($url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.7 (KHTML, like Gecko) Chrome/20.0.1099.0 Safari/536.7 QQBrowser/6.14.15493.201'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $result = curl_exec($ch); curl_close($ch); return $result; } $isbn = @$_GET['isbn']; $surl = 'https://book.douban.com/isbn/'.$isbn.'/'; $headers = json_encode(get_headers($surl),true); $headers = json_encode($headers,true); $surl = cut($headers,'Location: ','"'); $surl = str_replace('\\','' ,$surl);//302地址 $data = get($surl); $data_1=cut($data,'application/ld+json">','</script>'); $data_1 = json_decode($data_1,true); $res['name'] = $data_1['name'];//书名 - $author = $data_1['author']; if($author[0]==''){ $author[0]['name'] = '未知'; } $res['author'] =$author[0]['name'];//作者 $res['photoUrl'] = cut($data,'data-pic="','"');//图标 $publishing = cut($data,'出版社:</span>','<br/>'); $publishing = cut($publishing,'">','</a>'); if($publishing==''){ $publishing ='未知'; } $res['publishing'] =$publishing;//出版社 $published = cut($data,'出版年:</span>','<br/>'); if($published==''){ $published ='未知'; } $res['published'] =$published;//出版年 $pages = cut($data,'页数:</span>','<br/>'); if($pages==''){ $pages ='未知'; } $res['pages'] =$pages;//页数 $price = cut($data,'定价:</span>','<br/>'); if($price==''){ $price ='未知'; } $res['price'] =$price;//定价 $designed = cut($data,'装帧:</span>','<br/>'); if($designed==''){ $designed ='未知'; } $res['designed'] =$designed;//装帧 $description = cut($data,'class="intro">','</p>'); $description = explode('<p>',$description)[1]; if($description==''){ $description ='未知'; } $res['description'] =$description;//简介 $res = json_encode($res,JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); header('Content-Type: application/json'); echo $res; ?>
-
GET API接口调试:
- 在线http post,get接口测试请求工具online,在线post,在线get,在线接口测试,api在线,生成文档,生成markdown文档,utf,gbk,转码-在线工具-postjson
- {:height 820, :width 837}
-
注意事项
- 在获取API返回数据的过程中,通过IOS的快捷指令,发现获取URL的词典值是空的。查看返回值JOSN格式的原始Response有问题。
解决方式是在index.php代码的json_encode函数上增加JSON_UNESCAPED_UNICODE和JSON_UNESCAPED_SLASHES两个参数。