Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function get_airtable_config() {
- return array(
- 'apiKey' => 'API_KEY',
- 'baseId' => 'BASE_ID',
- 'tableId' => 'TABLE_ID'
- );
- }
- // Esta función será el endpoint seguro para obtener los datos de Airtable
- function get_taxistas_data() {
- try {
- // Verificar que la petición viene de tu sitio
- $referer = isset($_SERVER['HTTP_REFERER']) ? parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST) : '';
- if ($referer !== $_SERVER['HTTP_HOST']) {
- wp_send_json_error('Acceso no autorizado', 403);
- }
- // Obtener configuración de Airtable
- $config = get_airtable_config();
- // Hacer la petición a Airtable usando WordPress HTTP API
- $response = wp_remote_get("https://api.airtable.com/v0/{$config['baseId']}/{$config['tableId']}", array(
- 'headers' => array(
- 'Authorization' => "Bearer {$config['apiKey']}"
- )
- ));
- if (is_wp_error($response)) {
- throw new Exception($response->get_error_message());
- }
- $body = wp_remote_retrieve_body($response);
- $data = json_decode($body, true);
- if (!$data) {
- throw new Exception('Error al decodificar la respuesta de Airtable');
- }
- wp_send_json_success($data);
- } catch (Exception $e) {
- wp_send_json_error($e->getMessage());
- }
- }
- add_action('wp_ajax_get_taxistas_data', 'get_taxistas_data');
- add_action('wp_ajax_nopriv_get_taxistas_data', 'get_taxistas_data');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement