ReverbApi.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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', '/my/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, $updated_start_date = null) {
  36. $params = ['page' => $page, 'per_page' => $per_page];
  37. if ($updated_start_date) {
  38. $params['updated_start_date'] = $updated_start_date;
  39. }
  40. return $this->request('GET', '/my/orders/selling/all', $params);
  41. }
  42. public function getOrder($order_number) {
  43. return $this->request('GET', '/my/orders/selling/' . urlencode($order_number));
  44. }
  45. // -------------------------------------------------------------------------
  46. // Metadata
  47. // -------------------------------------------------------------------------
  48. public function getCategories() {
  49. return $this->request('GET', '/categories/flat');
  50. }
  51. public function getListingConditions() {
  52. return $this->request('GET', '/listing_conditions');
  53. }
  54. // -------------------------------------------------------------------------
  55. // Webhooks
  56. // -------------------------------------------------------------------------
  57. public function registerWebhook($url, $event) {
  58. return $this->request('POST', '/webhooks', [], [
  59. 'url' => $url,
  60. 'event' => $event,
  61. ]);
  62. }
  63. public function getWebhooks() {
  64. return $this->request('GET', '/webhooks');
  65. }
  66. public function deleteWebhook($webhook_id) {
  67. return $this->request('DELETE', '/webhooks/' . (int)$webhook_id);
  68. }
  69. // -------------------------------------------------------------------------
  70. // HTTP transport
  71. // -------------------------------------------------------------------------
  72. private function request($method, $endpoint, array $params = [], $body = null) {
  73. $url = self::BASE_URL . $endpoint;
  74. if (!empty($params)) {
  75. $url .= '?' . http_build_query($params);
  76. }
  77. $headers = [
  78. 'Authorization: Bearer ' . $this->token,
  79. 'Accept-Version: ' . self::API_VERSION,
  80. 'Content-Type: application/hal+json',
  81. 'Accept: application/json',
  82. ];
  83. $ch = curl_init();
  84. curl_setopt_array($ch, [
  85. CURLOPT_URL => $url,
  86. CURLOPT_RETURNTRANSFER => true,
  87. CURLOPT_HTTPHEADER => $headers,
  88. CURLOPT_TIMEOUT => 30,
  89. CURLOPT_SSL_VERIFYPEER => true,
  90. CURLOPT_USERAGENT => 'OpenCart-Reverb/1.0',
  91. ]);
  92. if (in_array($method, ['POST', 'PUT', 'DELETE'])) {
  93. if ($method === 'POST') {
  94. curl_setopt($ch, CURLOPT_POST, true);
  95. } else {
  96. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
  97. }
  98. if ($body !== null) {
  99. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body));
  100. }
  101. }
  102. $response = curl_exec($ch);
  103. $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  104. $curl_error = curl_error($ch);
  105. curl_close($ch);
  106. if ($curl_error) {
  107. throw new RuntimeException('cURL error: ' . $curl_error);
  108. }
  109. $decoded = json_decode($response, true);
  110. if ($http_code >= 400) {
  111. $message = 'HTTP ' . $http_code;
  112. if (!empty($decoded['message'])) {
  113. $message = $decoded['message'];
  114. } elseif (!empty($decoded['errors'])) {
  115. $message = implode(', ', (array)$decoded['errors']);
  116. }
  117. throw new RuntimeException('Reverb API error: ' . $message, $http_code);
  118. }
  119. return $decoded;
  120. }
  121. }