| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- <?php
- class ReverbApi {
- const BASE_URL = 'https://api.reverb.com/api';
- const API_VERSION = '3.0';
- private $token;
- public function __construct($token) {
- $this->token = $token;
- }
- // -------------------------------------------------------------------------
- // Listings
- // -------------------------------------------------------------------------
- public function getListings($page = 1, $per_page = 50) {
- return $this->request('GET', '/my/listings', [
- 'page' => $page,
- 'per_page' => $per_page,
- ]);
- }
- public function createListing(array $data) {
- return $this->request('POST', '/listings', [], $data);
- }
- public function updateListing($listing_id, array $data) {
- return $this->request('PUT', '/listings/' . urlencode($listing_id), [], $data);
- }
- public function endListing($listing_id) {
- return $this->request('PUT', '/listings/' . urlencode($listing_id) . '/end');
- }
- public function uploadPhoto($listing_id, $image_url) {
- return $this->request('POST', '/my/listings/' . urlencode($listing_id) . '/photos', [], [
- 'image_url' => $image_url,
- ]);
- }
- // -------------------------------------------------------------------------
- // Orders
- // -------------------------------------------------------------------------
- public function getOrders($page = 1, $per_page = 50) {
- return $this->request('GET', '/my/orders', [
- 'page' => $page,
- 'per_page' => $per_page,
- ]);
- }
- public function getOrder($order_number) {
- return $this->request('GET', '/my/orders/' . urlencode($order_number));
- }
- // -------------------------------------------------------------------------
- // Metadata
- // -------------------------------------------------------------------------
- public function getCategories() {
- return $this->request('GET', '/categories/flat');
- }
- public function getListingConditions() {
- return $this->request('GET', '/listing_conditions');
- }
- // -------------------------------------------------------------------------
- // Webhooks
- // -------------------------------------------------------------------------
- public function registerWebhook($url, $event) {
- return $this->request('POST', '/webhooks', [], [
- 'url' => $url,
- 'event' => $event,
- ]);
- }
- public function getWebhooks() {
- return $this->request('GET', '/webhooks');
- }
- public function deleteWebhook($webhook_id) {
- return $this->request('DELETE', '/webhooks/' . (int)$webhook_id);
- }
- // -------------------------------------------------------------------------
- // HTTP transport
- // -------------------------------------------------------------------------
- private function request($method, $endpoint, array $params = [], $body = null) {
- $url = self::BASE_URL . $endpoint;
- if (!empty($params)) {
- $url .= '?' . http_build_query($params);
- }
- $headers = [
- 'Authorization: Bearer ' . $this->token,
- 'Accept-Version: ' . self::API_VERSION,
- 'Content-Type: application/hal+json',
- 'Accept: application/json',
- ];
- $ch = curl_init();
- curl_setopt_array($ch, [
- CURLOPT_URL => $url,
- CURLOPT_RETURNTRANSFER => true,
- CURLOPT_HTTPHEADER => $headers,
- CURLOPT_TIMEOUT => 30,
- CURLOPT_SSL_VERIFYPEER => true,
- CURLOPT_USERAGENT => 'OpenCart-Reverb/1.0',
- ]);
- if (in_array($method, ['POST', 'PUT', 'DELETE'])) {
- if ($method === 'POST') {
- curl_setopt($ch, CURLOPT_POST, true);
- } else {
- curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
- }
- if ($body !== null) {
- curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body));
- }
- }
- $response = curl_exec($ch);
- $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
- $curl_error = curl_error($ch);
- curl_close($ch);
- if ($curl_error) {
- throw new RuntimeException('cURL error: ' . $curl_error);
- }
- $decoded = json_decode($response, true);
- if ($http_code >= 400) {
- $message = 'HTTP ' . $http_code;
- if (!empty($decoded['message'])) {
- $message = $decoded['message'];
- } elseif (!empty($decoded['errors'])) {
- $message = implode(', ', (array)$decoded['errors']);
- }
- throw new RuntimeException('Reverb API error: ' . $message, $http_code);
- }
- return $decoded;
- }
- }
|