function getItems4Client() { $DB = new PDO('mysql:host=' . DB_HOST . ';dbname=' . DB_NAME, DB_USERNAME, DB_PASSWORD); $table = DB_TABLE; //检查一下是否存在表 $sql = "SHOW TABLES LIKE '%{$table}%';"; $rc = $DB->query($sql)->rowCount(); if (!$rc) { $sqlFile = file_get_contents('items.sql'); $DB->exec($sqlFile); } $sql = "SELECT * FROM `{$table}` LIMIT 0,10"; $query = $DB->query($sql); $query->setFetchMode(PDO::FETCH_ASSOC); $rt = $query->fetchAll(); if ($rt) { $items = array(); foreach ($rt as $item) { $tmp = array(); $tmp['openid'] = $item['tb_item_id']; $tmp['iid'] = $item['tb_iid']; $tmp['price'] = $item['price'] ? $item['price'] / 100 : $item['reserve_price'] / 100; $tmp['name'] = $item['name']; $tmp['pic'] = $item['pic']; $tmp['itemType'] = $item['is_mall'] + 1; // 客户端判断是 1:集市店,2:天猫店 $items[] = $tmp; } Ajax::go($items); } else { Ajax::error('数据查询失败.'); } }
function justDemo() { /* * 第一步: 接收页面提交上来的参数,简单校验一下 */ // $itemUrl = $_REQUEST['itemUrl']; // $appKey = $_REQUEST['appKey']; // $appSecret = $_REQUEST['appSecret']; $itemUrl = 'https://detail.tmall.com/item.htm?id=520360573194&skuId=3101727199054'; $appKey = '23285002'; $appSecret = '0559365a08a6c5c71c321df373591512'; if (empty($itemUrl) && empty($appKey) && empty($appSecret)) { Ajax::error('请求参数错误!'); } $itemId = getIdInUrl($itemUrl); if (empty($itemId)) { Ajax::error('url错误,未能获取到商品id!'); } /* * 第二步: 调用top接口拿到商品数据 * 接口说明文档地址: * http://api.taobao.com/apidoc/api.htm?path=scopeId:11471-apiId:23731 */ $c = new TopClient(); $c->appkey = $appKey; $c->secretKey = $appSecret; $req = new TaeItemsListRequest(); $req->setFields('num,title,nick,pic_url,location,cid,price,post_fee,promoted_service'); $req->setNumIids($itemId); $resp = $c->execute($req); if (!isset($resp['items'])) { //缺少权限包,则跳转到权限申请页面 if (isset($resp['msg']) && isset($resp['sub_code']) && $resp['msg'] == 'Insufficient isv permissions' && $resp['sub_code'] == 'isv.permission-api-package-limit') { Ajax::error('isv.permission-api-package-limit'); } Ajax::error('top接口获取商品失败'); } /* * 第三步: 整理数据入库 */ $DB = new PDO('mysql:host=' . DB_HOST . ';dbname=' . DB_NAME, DB_USERNAME, DB_PASSWORD); $table = DB_TABLE; if (isset($resp['items']) && isset($resp['items']['x_item']) && isset($resp['items']['x_item'][0])) { $tmp = $resp['items']['x_item'][0]; } //检查一下是否存在表 $sql = "SHOW TABLES LIKE '%{$table}%';"; $rc = $DB->query($sql)->rowCount(); if (!$rc) { $sqlFile = file_get_contents('items.sql'); $DB->exec($sqlFile); } //检查是否存在该商品 $openId = $tmp['open_auction_iid']; $sql = "SELECT * FROM `{$table}` WHERE `tb_item_id` LIKE '{$openId}'"; $query = $DB->query($sql); $query->setFetchMode(PDO::FETCH_ASSOC); $rs = $query->fetchAll(); if ($rs) { Ajax::error('已经存在该商品.'); } else { $sql = "INSERT INTO `{$table}`\n (`id`, `pic`, `reserve_price`, `price`, `tb_item_id`,`tb_iid`, `name`, `is_mall`)\n VALUES\n (NULL,\n '" . $tmp['pic_url'] . "',\n '" . $tmp['reserve_price'] * 100 . "',\n '" . $tmp['price'] * 100 . "',\n '" . $tmp['open_auction_iid'] . "',\n '" . $itemId . "',\n '" . $tmp['title'] . "',\n '" . ($tmp['mall'] ? 1 : 0) . "');"; $rs = $DB->exec($sql); if (!$rs) { Ajax::error('数据入库失败.'); } } /* * 最后: 返回数据,前端可以通过控制台查看数据结果. */ Ajax::go($resp); }