CoupangLib.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Support\Facades\Http;
  4. class CoupangLib
  5. {
  6. const SUB_ID = 'AF0305179';
  7. const BASE_URL = 'https://api-gateway.coupang.com';
  8. const BASE_PATH = '/v2/providers/affiliate_open_api/apis/openapi';
  9. protected function getAuthorization(string $path): string
  10. {
  11. date_default_timezone_set("GMT+0");
  12. $datetime = date("ymd") . 'T' . date("His") . 'Z';
  13. $message = $datetime . 'GET' . str_replace("?", "", self::BASE_PATH . $path);
  14. $signature = hash_hmac('sha256', $message, SECRET_KEY);
  15. return sprintf("CEA algorithm=HmacSHA256, access-key=%s, signed-date=%s, signature=%s", ACCESS_KEY, $datetime, $signature);
  16. }
  17. /**
  18. * 카테고리 별 Best 상품 조회
  19. */
  20. public function getBestCategories(int $categoryID): ?array
  21. {
  22. $path = sprintf('/v1/products/bestcategories/%s?limit=%d', $categoryID, 100);
  23. $auth = $this->getAuthorization($path);
  24. $response = Http::withHeaders([
  25. 'Authorization' => $auth,
  26. 'Content-Type' => 'application/json',
  27. 'charset' => 'UTF-8'
  28. ])->get(self::BASE_URL . self::BASE_PATH . $path);
  29. return $response->json();
  30. }
  31. /**
  32. * 카테고리 별 Best 상품 조회
  33. */
  34. public function getSearch(?string $keyword): ?array
  35. {
  36. $path = sprintf('/products/search?keyword=%s&limit=%d&srpLinkOnly=false', urlencode($keyword), 10);
  37. $auth = $this->getAuthorization($path);
  38. $response = Http::withHeaders([
  39. 'Authorization' => $auth,
  40. 'Content-Type' => 'application/json',
  41. 'charset' => 'UTF-8'
  42. ])->get(self::BASE_URL . self::BASE_PATH . $path);
  43. return $response->json();
  44. }
  45. /**
  46. * 쿠팡 추천 상품 조회
  47. */
  48. public function getAllCoupangPL(): ?array
  49. {
  50. $path = '/v1/products/coupangPL?limit=100';
  51. $auth = $this->getAuthorization($path);
  52. $response = Http::withHeaders([
  53. 'Authorization' => $auth,
  54. 'Content-Type' => 'application/json',
  55. 'charset' => 'UTF-8'
  56. ])->get(self::BASE_URL . self::BASE_PATH . $path);
  57. return $response->json();
  58. }
  59. /**
  60. * 쿠팡 추천 상품 조회
  61. */
  62. public function getCoupangPL(int $brandID): ?array
  63. {
  64. $path = sprintf('/v1/products/coupangPL/%s?limit=%d', $brandID, 100);
  65. $auth = $this->getAuthorization($path);
  66. $response = Http::withHeaders([
  67. 'Authorization' => $auth,
  68. 'Content-Type' => 'application/json',
  69. 'charset' => 'UTF-8'
  70. ])->get(self::BASE_URL . self::BASE_PATH . $path);
  71. return $response->json();
  72. }
  73. /**
  74. * 골드박스 상품 조회
  75. */
  76. public function getGoldBox(): ?array
  77. {
  78. $path = '/v1/products/goldbox';
  79. $auth = $this->getAuthorization($path);
  80. $response = Http::withHeaders([
  81. 'Authorization' => $auth,
  82. 'Content-Type' => 'application/json',
  83. 'charset' => 'UTF-8'
  84. ])->get(self::BASE_URL . self::BASE_PATH . $path);
  85. return $response->json();
  86. }
  87. }