jonathanbak/navershopep

Naver Shopping EP3 PHP 库

v1.0.3 2021-05-18 21:33 UTC

This package is not auto-updated.

Last update: 2024-09-18 13:35:08 UTC


README

这是生成 Naver Shopping EP3.0 商品信息同步文件的库。由于国内提供的 PHP 连接 API 样本与其他开发语言相比总是不足,因此为了 PHP 开发者,尽管有些不足,但仍然公开。

安装

$ composer require jonathanbak/navershopep

开始

创建 Naver Shopping 连接时使用的 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;

示例测试

请复制示例源代码以供参考。

$ cp -rp ./vendor/jonathanbak/navershopep/samples ./samples

$ cat ./samples/README.md

用法

  1. 请为每个购物商城创建一个继承自 NaverShopEp\Shop\AbstractLists 和 NaverShopEp\Shop\AbstractItem 的类。
  2. 创建一个将在指定连接时间运行的 EP 生成脚本文件,并将其注册到 cron 等中,以生成 ep 文件。
  3. 创建一个用于将文件注册到 Naver Shopping "购物商城商品数据库 URL" 的文件,以读取 ep 文件并提供数据。

1. 为每个购物商城创建自定义类

  1. 创建列表,继承 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;
    }
}
  1. 创建项目,继承 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 文件

  1. 生成所有商品 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());
    }
}
  1. 生成摘要 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 文件

这是实际连接到 Naver Shopping 商品数据库 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());
}