131 lines
3.4 KiB
PHP
131 lines
3.4 KiB
PHP
<?php
|
|
|
|
/**
|
|
* CollectiveAccess API Integration
|
|
*
|
|
* This file contains functions to interact with a CollectiveAccess instance via its API.
|
|
*/
|
|
|
|
if (!defined('ABSPATH')) {
|
|
exit; // Exit if accessed directly
|
|
}
|
|
|
|
/**
|
|
* Make a request to the CollectiveAccess API
|
|
*
|
|
* @param string $endpoint The API endpoint to request
|
|
* @param array $params Query parameters for the request
|
|
* @param string $method HTTP method (GET, POST, etc.)
|
|
* @return array|WP_Error Response data or WP_Error on failure
|
|
*/
|
|
function carhop_ca_api_request($endpoint, $params = array(), $method = 'GET')
|
|
{
|
|
// Base URL of the McCord Museum's CollectiveAccess instance
|
|
$base_url = 'https://collections.musee-mccord.qc.ca';
|
|
|
|
// Build the full URL
|
|
$url = trailingslashit($base_url) . 'api/' . ltrim($endpoint, '/');
|
|
|
|
// Add query parameters if they exist
|
|
if (!empty($params)) {
|
|
$url = add_query_arg($params, $url);
|
|
}
|
|
|
|
// Set up the request arguments
|
|
$args = array(
|
|
'method' => $method,
|
|
'timeout' => 30,
|
|
'headers' => array(
|
|
'Accept' => 'application/json',
|
|
'Content-Type' => 'application/json',
|
|
),
|
|
);
|
|
|
|
// Make the request
|
|
$response = wp_remote_request($url, $args);
|
|
|
|
// Check for errors
|
|
if (is_wp_error($response)) {
|
|
return $response;
|
|
}
|
|
|
|
// Get the response body
|
|
$body = wp_remote_retrieve_body($response);
|
|
|
|
// Decode the JSON response
|
|
$data = json_decode($body, true);
|
|
|
|
// Check if JSON decoding failed
|
|
if (json_last_error() !== JSON_ERROR_NONE) {
|
|
return new WP_Error('json_decode_error', 'Failed to decode JSON response');
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
|
|
/**
|
|
* Get a list of objects from CollectiveAccess
|
|
*
|
|
* @param array $params Query parameters for filtering results
|
|
* @return array|WP_Error List of objects or WP_Error on failure
|
|
*/
|
|
function carhop_ca_get_objects($params = array())
|
|
{
|
|
return carhop_ca_api_request('objects', $params);
|
|
}
|
|
|
|
/**
|
|
* Get a single object by ID from CollectiveAccess
|
|
*
|
|
* @param int $object_id The ID of the object to retrieve
|
|
* @param array $params Additional query parameters
|
|
* @return array|WP_Error Object data or WP_Error on failure
|
|
*/
|
|
function carhop_ca_get_object($object_id, $params = array())
|
|
{
|
|
return carhop_ca_api_request("objects/{$object_id}", $params);
|
|
}
|
|
|
|
/**
|
|
* Get a list of collections from CollectiveAccess
|
|
*
|
|
* @param array $params Query parameters for filtering results
|
|
* @return array|WP_Error List of collections or WP_Error on failure
|
|
*/
|
|
function carhop_ca_get_collections($params = array())
|
|
{
|
|
return carhop_ca_api_request('collections', $params);
|
|
}
|
|
|
|
/**
|
|
* Get a single collection by ID from CollectiveAccess
|
|
*
|
|
* @param int $collection_id The ID of the collection to retrieve
|
|
* @param array $params Additional query parameters
|
|
* @return array|WP_Error Collection data or WP_Error on failure
|
|
*/
|
|
function carhop_ca_get_collection($collection_id, $params = array())
|
|
{
|
|
return carhop_ca_api_request("collections/{$collection_id}", $params);
|
|
}
|
|
|
|
/**
|
|
* Example usage function to test the API
|
|
*/
|
|
function carhop_test_ca_api()
|
|
{
|
|
// Test getting objects
|
|
$objects = carhop_ca_get_objects([
|
|
'limit' => 5,
|
|
'page' => 1
|
|
]);
|
|
|
|
if (is_wp_error($objects)) {
|
|
error_log('CollectiveAccess API Error: ' . $objects->get_error_message());
|
|
return;
|
|
}
|
|
|
|
// Log the results
|
|
error_log('CollectiveAccess API Test Results: ' . print_r($objects, true));
|
|
}
|