test querying showing collective acess datas

This commit is contained in:
Antoine 2025-03-06 10:11:59 +01:00
parent 40f5985f6b
commit de169dba95
2 changed files with 231 additions and 0 deletions

View File

@ -0,0 +1,130 @@
<?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));
}

View File

@ -0,0 +1,101 @@
<?php
/**
* Template Name: CollectiveAccess Test
*
* This is a template to test and display CollectiveAccess API results
*/
get_header();
?>
<div class="container mx-auto px-4 py-8">
<h1 class="text-3xl font-bold mb-8">Test de l'API CollectiveAccess</h1>
<?php
// Test getting objects
$objects = carhop_ca_get_objects([
'limit' => 10,
'page' => 1
]);
if (is_wp_error($objects)) {
echo '<div class="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded relative" role="alert">';
echo '<strong class="font-bold">Erreur:</strong> ';
echo '<span class="block sm:inline">' . esc_html($objects->get_error_message()) . '</span>';
echo '</div>';
} else {
echo '<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">';
foreach ($objects as $object) {
echo '<div class="bg-white rounded-lg shadow-md overflow-hidden">';
// Display object image if available
if (isset($object['representations'][0]['urls']['preview'])) {
echo '<img src="' . esc_url($object['representations'][0]['urls']['preview']) . '"
alt="' . esc_attr($object['preferred_labels']['name']) . '"
class="w-full h-48 object-cover">';
}
echo '<div class="p-4">';
// Display object title
if (isset($object['preferred_labels']['name'])) {
echo '<h2 class="text-xl font-semibold mb-2">' . esc_html($object['preferred_labels']['name']) . '</h2>';
}
// Display object ID
echo '<p class="text-gray-600 text-sm mb-2">ID: ' . esc_html($object['object_id']) . '</p>';
// Display object type if available
if (isset($object['type'])) {
echo '<p class="text-gray-600 text-sm">Type: ' . esc_html($object['type']) . '</p>';
}
echo '</div>'; // End of card content
echo '</div>'; // End of card
}
echo '</div>'; // End of grid
}
?>
<div class="mt-8">
<h2 class="text-2xl font-bold mb-4">Collections</h2>
<?php
// Test getting collections
$collections = carhop_ca_get_collections([
'limit' => 5,
'page' => 1
]);
if (is_wp_error($collections)) {
echo '<div class="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded relative" role="alert">';
echo '<strong class="font-bold">Erreur:</strong> ';
echo '<span class="block sm:inline">' . esc_html($collections->get_error_message()) . '</span>';
echo '</div>';
} else {
echo '<div class="grid grid-cols-1 md:grid-cols-2 gap-6">';
foreach ($collections as $collection) {
echo '<div class="bg-white rounded-lg shadow-md p-4">';
// Display collection name
if (isset($collection['preferred_labels']['name'])) {
echo '<h3 class="text-lg font-semibold mb-2">' . esc_html($collection['preferred_labels']['name']) . '</h3>';
}
// Display collection ID
echo '<p class="text-gray-600 text-sm">ID: ' . esc_html($collection['collection_id']) . '</p>';
echo '</div>';
}
echo '</div>';
}
?>
</div>
</div>
<?php
get_footer();