jonathanbak / navershopep
Naver Shopping EP3 PHP Library
v1.0.3
2021-05-18 21:33 UTC
Requires
- php: >=5.6.0
Requires (Dev)
- phpunit/phpunit: ~9.5
README
네이버쇼핑 상품정보연동 EP3.0 파일 생성 라이브러리 입니다. 국내에서 제공되는 php 연동 API 샘플이 다른 개발 언어에 비해 항상 부족한것 같아 php 동지들을 위해 부족하지만 공개해봅니다.
Install
$ composer require jonathanbak/navershopep
Start
네이버쇼핑 연동시 사용할 EP 테이블 생성
CREATE TABLE `nshop_epitem` ( `ne_uid` int(11) NOT NULL COMMENT '고유 상품 번호', `ne_item_hash` varchar(255) DEFAULT NULL COMMENT '변경체크값', `ne_send_date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00' COMMENT '전송 날짜', `ne_reg_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '전송데이터 등록일', PRIMARY KEY (`ne_uid`), KEY `ix_nshop_epitem_ne_uid_hash` (`ne_uid`,`ne_item_hash`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Sample Test
샘플 소스 복사하여 참고하세요.
$ cp -rp ./vendor/jonathanbak/navershopep/samples ./samples $ cat ./samples/README.md
Usage
- 각 쇼핑몰에 맞게 NaverShopEp\Shop\AbstractLists, NaverShopEp\Shop\AbstractItem 을 상속받아 클래스 작성해주세요.
- 지정된 연동시간에 맞춰 실행할 EP생성 스크립트 파일을 만들고 cron 등에 등록하여 ep파일을 생성합니다.
- 네이버쇼핑 "쇼핑몰 상품DB URL" 에 등록할 파일 만드셔서 ep파일을 읽어 데이터를 제공합니다.
1. 각 쇼핑몰 에 맞게 커스텀 클래스 생성하기
- Lists 만들기, NaverShopEp\Shop\AbstractLists 를 상속
use NaverShopEp\Shop\AbstractLists; class ShopLists extends AbstractLists { /** * 주문판매상태 고려 전송하고자 하는 상품 선택 쿼리 * 전송되는 정보 고려 (변경시 업데이트 필요) item_hash 작성 * @return string */ public function queryAll() { $query = "SELECT s_idx as uid, s_goods_name, s_img_url, s_price, IF(s_stock>0,1,0) as is_stock, s_status, s_last_update, md5(CONCAT(s_goods_name, s_img_url, s_price, s_status, IF(s_stock>0,1,0))) as item_hash FROM shop_item WHERE s_status = 1 AND s_nshop_flag = 'Y' LIMIT 5 "; return $query; } }
- Item 만들기, NaverShopEp\Shop\AbstractItem 을 상속
/** * Class ShopLists * 각 쇼핑몰에 맞게 NaverShopEp\Shop\AbstractItem 확장하여 데이터 맞춰주시면 됩니다. */ class ShopItem extends AbstractItem { public function getId() { return $this->data['uid']; } public function getCategory_name1() { return '카테고리1'; } public function getImage_link() { return 'http://sample.com'.$this->data['s_img_url']; } public function getLink() { return 'http://sample.com/goods/detail?gno='.$this->data['uid']; } public function getMobile_link() { return 'http://sample.com/goods/detail?gno='.$this->data['uid']; } public function getPrice_pc() { return $this->data['s_price']; } public function getShipping() { return 0; } public function getTitle() { return $this->data['s_goods_name']; } }
2. ep 파일 만들기
- 전체 상품 EP 파일 생성
use NaverShopEp\Ep\Full; use NaverShopEp\Exception; use NaverShopEp\ItemManage; use NaverShopEp\MakeHandler; use NaverShopEp\Product; //db커넥 $dbConn = new \mysqli($host, $user, $password, $dbName, $dbPort); if($dbConn->connect_errno){ echo '[연결실패] : '.$dbConn->connect_error.''; } else { //echo '[연결성공]'; } $MakeHandler = new MakeHandler(); $MakeHandler->setEpDir("tmp"); $MakeHandler->createFileStart(); $ShopLists = new ShopLists(); $ItemManager = new ItemManage(); $query = $ItemManager->getAllQuery($ShopLists); //1. 쿼리 실행 $result = $dbConn->query($query) or die($dbConn->error); $ProductProxy = new Product(); //전체 목록 체크하여 데이터 생성 while($data = $result->fetch_array()) { try { $NaverShopEpProduct = new Full(); //EP 풀버전 $ProductProxy->setEp($NaverShopEpProduct); $ShopItem = new ShopItem($data); $ProductProxy->setShop($ShopItem); $ProductProxy->match(); $MakeHandler->createFileContent($ProductProxy->get()); $registEpItemData = ['ne_uid'=>$data['uid'], 'ne_item_hash'=>$data['item_hash']]; $addQuery = $ItemManager->add($registEpItemData); $dbConn->query($addQuery); //쿼리 실행 } catch (Exception $e) { var_dump($e->getMessage()); } }
- 요약 EP 파일 생성
use NaverShopEp\Ep\Simple; use NaverShopEp\Exception; use NaverShopEp\ItemManage; use NaverShopEp\MakeHandler; use NaverShopEp\Product; //db커넥 $dbConn = new \mysqli($host, $user, $password, $dbName, $dbPort); if($dbConn->connect_errno){ echo '[연결실패] : '.$dbConn->connect_error.''; } else { //echo '[연결성공]'; } $MakeHandler = new MakeHandler(MakeHandler::TYPE_SIMPLE); $MakeHandler->setEpDir("tmp"); $MakeHandler->createFileStart(); $ShopLists = new ShopLists(); $ItemManager = new ItemManage(); //1. 변경된 상품 불러오기 $query = $ItemManager->getChangeListQuery($ShopLists); $result = $dbConn->query($query) or die($dbConn->error); $ProductProxy = new Product(); $createDateTime = date("Y-m-d H:i:s"); //상품정보 생성 시각 while($data = $result->fetch_array()) { try { $NaverShopEpProduct = new Simple(); //EP 요약버전 $ProductProxy->setEp($NaverShopEpProduct); if ($data['is_stock'] == 0) { //품절된 상품 삭제 $ShopItem = new ShopItem($data); $ProductProxy->setShop($ShopItem); $ProductProxy->delete($createDateTime);//상품정보 생성 시각 $ProductProxy->match(); echo $ProductProxy->get(); $MakeHandler->createFileContent($ProductProxy->get()); $registEpItemData = ['ne_uid'=>$data['uid'], 'ne_item_hash'=>$data['item_hash']]; $addQuery = $ItemManager->add($registEpItemData); $dbConn->query($addQuery); } else { //변경된 상품 업데이트 $ShopItem = new ShopItem($data); $ProductProxy->setShop($ShopItem); $ProductProxy->update($createDateTime);//상품정보 생성 시각 $ProductProxy->match(); echo $ProductProxy->get(); $MakeHandler->createFileContent($ProductProxy->get()); $registEpItemData = ['ne_uid'=>$data['uid'], 'ne_item_hash'=>$data['item_hash']]; $addQuery = $ItemManager->add($registEpItemData); $dbConn->query($addQuery); } //쿼리 실행 } catch (Exception $e) { var_dump($e->getMessage()); } } //2. 추가된 상품 불러오기 $query = $ItemManager->getNewListQuery($ShopLists); $result = $dbConn->query($query) or die($dbConn->error); while($data = $result->fetch_array()) { try { $NaverShopEpProduct = new Simple(); //EP 요약버전 $ProductProxy->setEp($NaverShopEpProduct); //변경된 상품 업데이트 $ShopItem = new ShopItem($data); $ProductProxy->setShop($ShopItem); $ProductProxy->insert($createDateTime);//상품정보 생성 시각 $ProductProxy->match(); echo $ProductProxy->get(); $MakeHandler->createFileContent($ProductProxy->get()); $registEpItemData = ['ne_uid'=>$data['uid'], 'ne_item_hash'=>$data['item_hash']]; $addQuery = $ItemManager->add($registEpItemData); $dbConn->query($addQuery); //쿼리 실행 } catch (Exception $e) { var_dump($e->getMessage()); } }
EP 파일 읽기
실제 네이버쇼핑 상품DB URL 에 연동할 EP 읽기 소스 입니다.
use NaverShopEp\Ep\Full; use NaverShopEp\Ep\Simple; use NaverShopEp\MakeHandler; try{ //요청 시간에 따라 전체 EP, 요약 EP로 나눠서 뿌려준다 EP3.0에서 변경된 요소 if(date('G')>=10){ $NhnEpMakeHandler = new MakeHandler( MakeHandler::TYPE_SIMPLE ); $NhnEpMakeHandler->setEpDir("tmp"); $NhnEpMakeHandler->setFileHeader(new Simple()); $contents = $NhnEpMakeHandler->readFile(); if($contents){ echo $contents; } }else{ $NhnEpMakeHandler = new MakeHandler( MakeHandler::TYPE_FULL ); $NhnEpMakeHandler->setEpDir("tmp"); $NhnEpMakeHandler->setFileHeader(new Full()); $contents = $NhnEpMakeHandler->readFile(); if($contents){ echo $contents; } } }catch(Exception $e){ var_dump($e->getMessage()); }