ReverbApi.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. class ReverbApi {
  3. const BASE_URL = 'https://api.reverb.com/api';
  4. const API_VERSION = '3.0';
  5. private $token;
  6. public function __construct($token) {
  7. $this->token = $token;
  8. }
  9. // -------------------------------------------------------------------------
  10. // Listings
  11. // -------------------------------------------------------------------------
  12. public function getListings($page = 1, $per_page = 50) {
  13. return $this->request('GET', '/my/listings', [
  14. 'page' => $page,
  15. 'per_page' => $per_page,
  16. ]);
  17. }
  18. public function createListing(array $data) {
  19. return $this->request('POST', '/listings', [], $data);
  20. }
  21. public function updateListing($listing_id, array $data) {
  22. return $this->request('PUT', '/listings/' . urlencode($listing_id), [], $data);
  23. }
  24. public function endListing($listing_id) {
  25. return $this->request('PUT', '/listings/' . urlencode($listing_id) . '/end');
  26. }
  27. public function uploadPhoto($listing_id, $image_url) {
  28. return $this->request('POST', '/listings/' . urlencode($listing_id) . '/photos', [], [
  29. 'image_url' => $image_url,
  30. ]);
  31. }
  32. // -------------------------------------------------------------------------
  33. // Orders
  34. // -------------------------------------------------------------------------
  35. public function getOrders($page = 1, $per_page = 50) {
  36. return $this->request('GET', '/my/orders', [
  37. 'page' => $page,
  38. 'per_page' => $per_page,
  39. ]);
  40. }
  41. public function getOrder($order_number) {
  42. return $this->request('GET', '/my/orders/' . urlencode($order_number));
  43. }
  44. // -------------------------------------------------------------------------
  45. // Metadata
  46. // -------------------------------------------------------------------------
  47. public function getCategories() {
  48. return $this->request('GET', '/categories/flat');
  49. }
  50. public function getListingConditions() {
  51. return $this->request('GET', '/listing_conditions');
  52. }
  53. // -------------------------------------------------------------------------
  54. // Webhooks
  55. // -------------------------------------------------------------------------
  56. public function registerWebhook($url, $event) {
  57. return $this->request('POST', '/webhooks', [], [
  58. 'url' => $url,
  59. 'event' => $event,
  60. ]);
  61. }
  62. public function getWebhooks() {
  63. return $this->request('GET', '/webhooks');
  64. }
  65. public function deleteWebhook($webhook_id) {
  66. return $this->request('DELETE', '/webhooks/' . (int)$webhook_id);
  67. }
  68. // -------------------------------------------------------------------------
  69. // HTTP transport
  70. // -------------------------------------------------------------------------
  71. private function request($method, $endpoint, array $params = [], $body = null) {
  72. $url = self::BASE_URL . $endpoint;
  73. if (!empty($params)) {
  74. $url .= '?' . http_build_query($params);
  75. }
  76. $headers = [
  77. 'Authorization: Bearer ' . $this->token,
  78. 'Accept-Version: ' . self::API_VERSION,
  79. 'Content-Type: application/hal+json',
  80. 'Accept: application/json',
  81. ];
  82. $ch = curl_init();
  83. curl_setopt_array($ch, [
  84. CURLOPT_URL => $url,
  85. CURLOPT_RETURNTRANSFER => true,
  86. CURLOPT_HTTPHEADER => $headers,
  87. CURLOPT_TIMEOUT => 30,
  88. CURLOPT_SSL_VERIFYPEER => true,
  89. CURLOPT_USERAGENT => 'OpenCart-Reverb/1.0',
  90. ]);
  91. if (in_array($method, ['POST', 'PUT', 'DELETE'])) {
  92. if ($method === 'POST') {
  93. curl_setopt($ch, CURLOPT_POST, true);
  94. } else {
  95. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
  96. }
  97. if ($body !== null) {
  98. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body));
  99. }
  100. }
  101. $response = curl_exec($ch);
  102. $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  103. $curl_error = curl_error($ch);
  104. curl_close($ch);
  105. if ($curl_error) {
  106. throw new RuntimeException('cURL error: ' . $curl_error);
  107. }
  108. $decoded = json_decode($response, true);
  109. if ($http_code >= 400) {
  110. $message = 'HTTP ' . $http_code;
  111. if (!empty($decoded['message'])) {
  112. $message = $decoded['message'];
  113. } elseif (!empty($decoded['errors'])) {
  114. $message = implode(', ', (array)$decoded['errors']);
  115. }
  116. throw new RuntimeException('Reverb API error: ' . $message, $http_code);
  117. }
  118. return $decoded;
  119. }
  120. }