Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function get_facebook_reactions() {
- // Asignar la URL del post de Facebook, API key de Apify y el ID del actor a variables
- $facebook_post_url = 'https://www.facebook.com/julian9826G/posts/pfbid0tQCukmnsuJmkXShgJfNpjrKwcdJ8ymE1AkcLLhPWNfJzArgMZZx7c6RNP9Cg8YhDl';
- $apify_api_key = 'apify_api_VudNw3Y940vAhBlw5GptSCGqGLtdsL34gHug';
- $apify_actor_id = 'XKQI0Jr9lTVSx0BiH';
- // API URL para ejecutar el actor en Apify
- $apify_url = "https://api.apify.com/v2/acts/$apify_actor_id/runs";
- // Crear el cuerpo de la solicitud
- $body = json_encode(array(
- 'startUrls' => array(
- array('url' => $facebook_post_url)
- )
- ));
- echo "<pre>";
- print_r($body);
- echo "</pre><br><br><br>";
- // Hacer la solicitud a la API de Apify
- $response = wp_remote_post($apify_url, array(
- 'body' => $body,
- 'timeout' => 60, // Tiempo de espera
- 'headers' => array(
- 'Authorization' => 'Bearer ' . $apify_api_key,
- 'Content-Type' => 'application/json',
- ),
- ));
- // Verificar si la respuesta contiene errores
- if (is_wp_error($response)) {
- // Print the error in case the request fails
- echo 'Error contacting Apify: ' . $response->get_error_message().'<br><br><br>';
- return;
- }
- // Obtener el cuerpo de la respuesta
- $response_body = wp_remote_retrieve_body($response);
- // Check if the response body is not empty
- if (!empty($response_body)) {
- // Decode the JSON response
- $likes_data = json_decode($response_body, true);
- // Display the full result to inspect the structure
- echo 'Full result from Apify: <br>';
- echo "<pre>";
- print_r($likes_data);
- echo "</pre><br><br><br>";
- // If the likes/reactions data exists, print it
- if (isset($likes_data[0]['likes'])) {
- $total_likes = intval($likes_data[0]['likes']);
- echo '<br><br><br>Total reactions/likes: ' . $total_likes;
- } else {
- echo '<br><br><br>No likes/reactions were found in the response.';
- }
- } else {
- // If no valid response was received, print a message
- echo 'No valid response received from Apify.';
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment