免費掃書的條形碼API,PHP代碼實例
代碼:
$url='https://api.douban.com/v2/book/isbn/:'.$data['id']; $list=$this->curtd($url); if($list['code'] == 6000){ $this->ajaxReturn(array('code' =>400, 'msg' =>$list['msg'])); }
1、$data['id']:前端調用掃碼功能,把獲得id傳入後臺,PHP接收id
2、這免費的API是豆瓣的,也就是說你掃的書豆瓣沒有就返回code=6000 此書沒找到提示
//訪問API接口公用 public function curtd($url,$data){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$url); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)'); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_AUTOREFERER, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $tmpInfo = curl_exec($ch); if (curl_errno($ch)) { return curl_error($ch); } curl_close($ch); return json_decode($tmpInfo ,true); }