$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)); } /** * Test and display the API endpoint URL */ function carhop_test_ca_endpoint() { $base_url = 'https://collections.musee-mccord.qc.ca'; $endpoint = 'objects'; $params = ['limit' => 1]; $url = trailingslashit($base_url) . 'api/' . ltrim($endpoint, '/'); if (!empty($params)) { $url = add_query_arg($params, $url); } echo '
'; echo '

API Endpoint URL: ' . esc_url($url) . '

'; // Test the connection $response = wp_remote_get($url); if (is_wp_error($response)) { echo '

Connection Error: ' . esc_html($response->get_error_message()) . '

'; } else { $status = wp_remote_retrieve_response_code($response); echo '

Response Status: ' . esc_html($status) . '

'; } echo '
'; }