cafe24corp / recipe-channel
recipe channel API
0.1.13
2019-03-07 08:14 UTC
Requires
- php: >=7.0.0
- ext-json: ^1.6
- guzzlehttp/guzzle: ~6.0
This package is auto-updated.
Last update: 2024-09-07 21:19:59 UTC
README
提供用于制作食谱服务通道所需的API功能。
安装
Recipe Channel API可在Packagist上找到(cafe24corp/recipe-channel),因此可以通过Composer进行安装。
composer require cafe24corp/recipe-channel
如果您不使用Composer,可以从GitHub获取代码,并使用任何PSR-0兼容的自动加载器(例如Symfony2 ClassLoader组件)来加载Recipe Channel API类。
用法
0. 环境变量
要使用Recipe Channel API,您需要设置环境变量。可以使用Apache / Nginx的环境变量,或使用.env(https://github.com/symfony/dotenv)等。CHANNEL_NO、CHANNEL_ID、CHANNEL_SECRET可以在开发者中心(https://developer.cafe24.com)获取,这些是在调用Recipe API时所需的。
0.1. 可用变量
1. 触发字段
<?php use Cafe24corp\Recipe; $recipe = new Recipe(); $url = __DIR__ . '/json/trigger.json'; /* * 트리거 필드 데이터 양식 샘플 { "fields": [ { "data_type": "number", "type": "select", "required": true, "dynamic": true, "label": "쇼핑몰", "name": "shop_no" }, { "data_type": "string", "label": "상품명", "name": "product_name" }, { "data_type": "string", "type": "select", "dynamic": true, "label": "상품분류", "name": "collection" }, { "data_type": "string", "type": "select", "label": "진열상태", "name": "display", "options": [ {"label": "진열함", "value": "T"}, {"label": "진열안함", "value": "F"} ] }, ... ], "ingredients": [ { "label": "상품명", "name": "product_name", "description": "" } { "label": "소비자가", "name": "retail_price", "description": "1000" } ], "meta": { "data_selector_field": "shop_no" } } */ $result = $recipe->getTriggerData($url); // 결과 리턴 echo json_encode($result);
2. 动态触发字段
<?php /** * 트리거 동적 필드데이터를 레시피에 전달 합니다. * 앱스토어 > 레시피관리 > 채널관리 에서 트리거 동적 필드 데이터 전달 URL 을 설정할 수 있습니다. * * 해당 필드는 dynamic 을 true 로 설정하여야 합니다. * 요청은 POST 로 전달되면 동적 필드의 데이터를 로드하여 전달하셔야 합니다. */ use Cafe24corp\Recipe; $recipe = new Recipe(); // 동적 필드 데이터 요청 /* { "data": [ { "name": "collection" } ... ] } */ $data = $_POST['data']; $dynamicFieldData = []; for ($i = 0 ; $i < count($data) ; $i++){ $name = $data[$i]['name']; //동적 데이터 생성 $dynamicFieldData = array_push ( $dynamicFieldData, doGetDynamicFieldData($name) ); } // 레시피로 전달 /* 전달 데이터 샘플 { "data": [ { "name": "collection", "options": [ { "label": "Recommend Products", "value": "123" }, { "label": "Hit Products", "value": "456" }, ... ] }, ... ] } */ // 결과 리턴 echo json_encode(['data' => $dynamicFieldData ]);
3. 通知已激活的触发变化
<?php /** * 각 채널에서 채널 사용자별로 "카페24 Recipe"에 전달해야 하는 * 트리거 이벤트 리스트가 변경되었다는 알림을 받기 위한 프로토콜입니다. * * 변경알림을 전달 받으면 전달받은 유저아이디로 * 유저별 트리거 리스트를 검색하여 결과 값을 채널쪽에 저장합니다. */ use Cafe24corp\Recipe; use Cafe24corp\Exception; $recipe = new Recipe(); try { //채널 사용자 ID별 활성 트리거 변경 알림 $data = $_POST['data']; /* * POST 데이터 샘플 { "data": [ { "user_id": "userid1" }, { "user_id": "userid2" }, ... ] } */ $result = []; for ($i = 0 ; $i < count($data) ; $i++) { $user_id = $data[$i]['user_id']; $triggerData = $recipe->getActiveTriggerList($user_id); /* * 응답 데이터 샘플 { "triggers": [ 100001, 100002, 100004 ] } */ // 채널 측에 결과 저장 // doStoreTrigger() $result[] = ['result' => true]; } // 결과 리턴 echo json_encode($result); }catch(Exception\RecipeException $e){ echo $e->getMessage(); exit; }
4. 发送触发事件
<?php /** * 트리거 이벤트 전달 */ use Cafe24corp\Recipe; use Cafe24corp\Exception; $recipe = new Recipe(); try { // 트리거 아이디 $trigger_id = 100010; // 채널측 유저 아이디 $user_id = 'jylee08'; //필요하지 않을 경우 전달하지 않아도 된다. // 트리거 데이터 생성 (전달할 갯수만큼 루프 $triggerData = []; for ($i = 0 ; $i < 1 ; $i++) { //데이터 예시 - 트리거 필드에 설정된 ID 값으로 전달 한다. $value = [ 'id' => 'id', 'message' => 'message' ]; $triggerData[] = $recipe->makeTriggerData($trigger_id, $value, $user_id); } // 트리거 이벤트 레시피로 전달 $recipe->sendTriggerEvent($triggerData); }catch(Exception\RecipeException $e){ echo $e->getMessage(); exit; }
5. 动作字段
<?php use Cafe24corp\Recipe; $recipe = new Recipe(); // 엑션 필드 정보 리턴 $url = __DIR__ . '/json/action.json'; echo $recipe->getActionData($url); // 결과 리턴 echo json_encode($result);
6. 动态动作字段
<?php use Cafe24corp\Recipe; $recipe = new Recipe(); // 동적 필드 데이터 요청 $data = $_POST['data']; $dynamicFieldData = []; for ($i = 0 ; $i < count($data) ; $i++){ $name = $data[$i]['name']; //동적 데이터 생성 $dynamicFieldData = array_push ( $dynamicFieldData, doGetDynamicFieldData($name) ); } // 결과 리턴 echo json_encode(['data' => $dynamicFieldData ]);
7. 执行动作
<?php /** * 엑션 실행 * 앱스토어 > 레시피관리 > 채널관리 에서 액션에 사용하는 URL 을 설정할 수 있습니다. * */ use Cafe24corp\Recipe; $recipe = new Recipe(); $data = $_POST; /* { "product_name": "상품명", "category_no": "1" } */ //Access Token 로드 - 엑션 처리시 사용 $access_token = $recipe->getBearerToken(); // 엑션처리 // $result = doAction($access_token, $data); // 재료 반환 데이터 생성 /* 재료 반환 데이터 샘플 { "data": [ { "product_code": "P00000N", "product_no": 100 } ] } */ // 기본 재료 (엑션 데이터 재료 ID 를 key 로 사용한다. - 날짜, 시간 등 $ingredients = $recipe->makeIngredients(); echo json_encode($ingredients);