| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- <?php
- namespace App\Models;
- use Illuminate\Support\Facades\Http;
- class CoupangLib
- {
- const SUB_ID = 'AF0305179';
- const BASE_URL = 'https://api-gateway.coupang.com';
- const BASE_PATH = '/v2/providers/affiliate_open_api/apis/openapi';
- protected function getAuthorization(string $path): string
- {
- date_default_timezone_set("GMT+0");
- $datetime = date("ymd") . 'T' . date("His") . 'Z';
- $message = $datetime . 'GET' . str_replace("?", "", self::BASE_PATH . $path);
- $signature = hash_hmac('sha256', $message, SECRET_KEY);
- return sprintf("CEA algorithm=HmacSHA256, access-key=%s, signed-date=%s, signature=%s", ACCESS_KEY, $datetime, $signature);
- }
- /**
- * 카테고리 별 Best 상품 조회
- */
- public function getBestCategories(int $categoryID): ?array
- {
- $path = sprintf('/v1/products/bestcategories/%s?limit=%d', $categoryID, 100);
- $auth = $this->getAuthorization($path);
- $response = Http::withHeaders([
- 'Authorization' => $auth,
- 'Content-Type' => 'application/json',
- 'charset' => 'UTF-8'
- ])->get(self::BASE_URL . self::BASE_PATH . $path);
- return $response->json();
- }
- /**
- * 카테고리 별 Best 상품 조회
- */
- public function getSearch(?string $keyword): ?array
- {
- $path = sprintf('/products/search?keyword=%s&limit=%d&srpLinkOnly=false', urlencode($keyword), 10);
- $auth = $this->getAuthorization($path);
- $response = Http::withHeaders([
- 'Authorization' => $auth,
- 'Content-Type' => 'application/json',
- 'charset' => 'UTF-8'
- ])->get(self::BASE_URL . self::BASE_PATH . $path);
- return $response->json();
- }
- /**
- * 쿠팡 추천 상품 조회
- */
- public function getAllCoupangPL(): ?array
- {
- $path = '/v1/products/coupangPL?limit=100';
- $auth = $this->getAuthorization($path);
- $response = Http::withHeaders([
- 'Authorization' => $auth,
- 'Content-Type' => 'application/json',
- 'charset' => 'UTF-8'
- ])->get(self::BASE_URL . self::BASE_PATH . $path);
- return $response->json();
- }
- /**
- * 쿠팡 추천 상품 조회
- */
- public function getCoupangPL(int $brandID): ?array
- {
- $path = sprintf('/v1/products/coupangPL/%s?limit=%d', $brandID, 100);
- $auth = $this->getAuthorization($path);
- $response = Http::withHeaders([
- 'Authorization' => $auth,
- 'Content-Type' => 'application/json',
- 'charset' => 'UTF-8'
- ])->get(self::BASE_URL . self::BASE_PATH . $path);
- return $response->json();
- }
- /**
- * 골드박스 상품 조회
- */
- public function getGoldBox(): ?array
- {
- $path = '/v1/products/goldbox';
- $auth = $this->getAuthorization($path);
- $response = Http::withHeaders([
- 'Authorization' => $auth,
- 'Content-Type' => 'application/json',
- 'charset' => 'UTF-8'
- ])->get(self::BASE_URL . self::BASE_PATH . $path);
- return $response->json();
- }
- }
|