Advertisement
oscarviedma

Función Airtable Config Taxi Directorio - OV

Dec 23rd, 2024
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. function get_airtable_config() {
  2. return array(
  3. 'apiKey' => 'API_KEY',
  4. 'baseId' => 'BASE_ID',
  5. 'tableId' => 'TABLE_ID'
  6. );
  7. }
  8.  
  9. // Esta función será el endpoint seguro para obtener los datos de Airtable
  10. function get_taxistas_data() {
  11. try {
  12. // Verificar que la petición viene de tu sitio
  13. $referer = isset($_SERVER['HTTP_REFERER']) ? parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST) : '';
  14. if ($referer !== $_SERVER['HTTP_HOST']) {
  15. wp_send_json_error('Acceso no autorizado', 403);
  16. }
  17.  
  18. // Obtener configuración de Airtable
  19. $config = get_airtable_config();
  20.  
  21. // Hacer la petición a Airtable usando WordPress HTTP API
  22. $response = wp_remote_get("https://api.airtable.com/v0/{$config['baseId']}/{$config['tableId']}", array(
  23. 'headers' => array(
  24. 'Authorization' => "Bearer {$config['apiKey']}"
  25. )
  26. ));
  27.  
  28. if (is_wp_error($response)) {
  29. throw new Exception($response->get_error_message());
  30. }
  31.  
  32. $body = wp_remote_retrieve_body($response);
  33. $data = json_decode($body, true);
  34.  
  35. if (!$data) {
  36. throw new Exception('Error al decodificar la respuesta de Airtable');
  37. }
  38.  
  39. wp_send_json_success($data);
  40.  
  41. } catch (Exception $e) {
  42. wp_send_json_error($e->getMessage());
  43. }
  44. }
  45. add_action('wp_ajax_get_taxistas_data', 'get_taxistas_data');
  46. add_action('wp_ajax_nopriv_get_taxistas_data', 'get_taxistas_data');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement