function get_goods($ids, $open_iid) { global $_G; $req = new TaeItemsListRequest(); $field = 'title,num,nick,pic_url,price,location,shop_name,post_fee'; if (is_string($ids)) { $ids = trim($ids, ','); } else { if (is_array($ids)) { $ids = implode(',', $ids); } } $req->setFields($field); $req->setNumIids($ids); $req->setOpenIids($open_iid); $resp = $_G['TOP']->execute($req); top_check_error($resp, true); $arr = array(); $item = $resp->items->x_item; if (count($resp->items->x_item) == 1) { $rs = $resp->items->x_item[0]; $arr = $this->parse_goods($rs); return $arr; } else { $arr = array(); foreach ($resp->items->x_item as $k => $v) { $numiid = (string) $v->open_id; $arr[$numiid] = $this->parse_goods($v); } return $arr; } return false; }
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); }