Download image project private

Hello, very good day.

I have the following code that I’m using to query data through the Epicollect5 API for private projects, but I want to know how to download the images I’m querying to a folder on my system. With that in mind, I’m sharing the code so you can help me with this. Thank you.

<?php
require 'vendor/autoload.php'; // Asegúrate de tener Guzzle instalado vía Composer

use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Client;

// Configuración
$client_id = '';
$client_secret = '';
$project_slug = '';
$main_form_ref = ''; // Ref del formulario principal
$branch_ref = ''; // Ref de la rama EVIDENCIAS
$base_url = 'https://five.epicollect.net';

function getAccessToken($client_id, $client_secret, $base_url) {
    $client = new Client();
    $token_url = $base_url . '/api/oauth/token';
    try {
        $response = $client->post($token_url, [
            'headers' => ['Content-Type' => 'application/vnd.api+json'],
            'body' => json_encode([
                'grant_type' => 'client_credentials',
                'client_id' => $client_id,
                'client_secret' => $client_secret
            ])
        ]);
        $body = $response->getBody();
        $obj = json_decode($body);
        return $obj->access_token;
    } catch (RequestException $e) {
        if ($e->hasResponse()) {
            $error_body = $e->getResponse()->getBody();
            die("Error al obtener el token de acceso: " . $error_body);
        } else {
            die("Error al obtener el token de acceso: " . $e->getMessage());
        }
    }
}

function getEntries($client, $base_url, $project_slug, $access_token, $form_ref = null, $branch_ref = null, $parent_uuid = null) {
    $entries_url = $base_url . "/api/export/entries/$project_slug";
    $query = [
        'per_page' => 1000,
        'page' => 1,
        'format' => 'json',
        'headers' => 'true',
    ];
    if ($form_ref) {
        $query['form_ref'] = $form_ref;
    }
    if ($branch_ref) {
        $query['branch_ref'] = $branch_ref;
    }
    if ($parent_uuid) {
        $query['parent_uuid'] = $parent_uuid;
    }
    $full_url = $entries_url . '?' . http_build_query($query);
    echo "\nSolicitando URL: $full_url\n";
    try {
        $response = $client->get($full_url, [
            'headers' => [
                'Authorization' => 'Bearer ' . $access_token,
                'Accept' => 'application/json'
            ]
        ]);
        $body = $response->getBody();
        $obj = json_decode($body, true);
        return $obj;
    } catch (RequestException $e) {
        if ($e->hasResponse()) {
            $error_body = $e->getResponse()->getBody();
            die("Error al obtener las entradas: " . $error_body);
        } else {
            die("Error al obtener las entradas: " . $e->getMessage());
        }
    }
}

function displayEvidencias($evidencias) {
    echo "<h2>Evidencias:</h2>";
    echo "<table border='1' cellpadding='5' cellspacing='0'>";
    echo "<tr>
            <th>FechaRegistro</th>
            <th>NumeroIdentificacionEncuestador</th>
            <th>NumeroIdentificacionEncuestado</th>
            <th>NombreEvidencia</th>
            <th>TomarEvidencia</th>
          </tr>";
    foreach ($evidencias as $evidencia) {
        // Ajusta los nombres de los campos según tus datos
        $fechaRegistro = isset($evidencia['71_FechaRegistro']) ? htmlspecialchars($evidencia['71_FechaRegistro']) : 'N/A';
        $numIdentEncuestador = isset($evidencia['72_NumeroIdentificac']) ? htmlspecialchars($evidencia['72_NumeroIdentificac']) : 'N/A';
        $numIdentEncuestado = isset($evidencia['73_NumeroIdentificac']) ? htmlspecialchars($evidencia['73_NumeroIdentificac']) : 'N/A';
        $nombreEvidencia = isset($evidencia['74_NombreEvidencia']) ? htmlspecialchars($evidencia['74_NombreEvidencia']) : 'N/A';
        $tomarEvidencia = isset($evidencia['75_TomarEvidencia']) ? htmlspecialchars($evidencia['75_TomarEvidencia']) : 'N/A';

        echo "<tr>
                <td>$fechaRegistro</td>
                <td>$numIdentEncuestador</td>
                <td>$numIdentEncuestado</td>
                <td>$nombreEvidencia</td>
                <td>$tomarEvidencia</td>
              </tr>";
    }
    echo "</table>";
}

// Flujo Principal

// 1. Obtener el token de acceso
$access_token = getAccessToken($client_id, $client_secret, $base_url);

// 2. Crear un cliente Guzzle para las solicitudes de entradas
$client = new Client();

// 3. Obtener las entradas principales del formulario principal
$main_entries_response = getEntries($client, $base_url, $project_slug, $access_token, $main_form_ref);

// Verificar si existen entradas principales
if (!isset($main_entries_response['data']['entries'])) {
    die("No se encontraron entradas principales.");
}

$main_entries = $main_entries_response['data']['entries'];

// Array para almacenar todas las entradas de EVIDENCIAS
$all_evidencias = [];

foreach ($main_entries as $main_entry) {
    $parent_uuid = $main_entry['ec5_uuid'];
    echo "\nObteniendo EVIDENCIAS para parent_uuid: $parent_uuid con branch_ref: $branch_ref\n";
    // Obtener las entradas de la rama EVIDENCIAS asociadas a esta entrada principal
    $evidencias_response = getEntries($client, $base_url, $project_slug, $access_token, $main_form_ref, $branch_ref, $parent_uuid);
    if (isset($evidencias_response['data']['entries'])) {
        foreach ($evidencias_response['data']['entries'] as $evidencia) {
            $all_evidencias[] = $evidencia;
        }
    }
}

// 4. Mostrar las entradas de EVIDENCIAS si existen
if (!empty($all_evidencias)) {
    displayEvidencias($all_evidencias);
} else {
    echo "<p>No se encontraron evidencias.</p>";
}
?>

Here you can find a PHP example →

1 Like

Dear Support Team,

Thank you very much for your prompt response; it has been incredibly valuable, and I have successfully applied it to basic projects. However, I am facing a challenge when trying to replicate this process in more complex projects that include groups and branches.

Specifically, I need to access the project’s image data stored within a specific branch. I have tried various approaches, but I consistently receive a message indicating that the information is unavailable. I suspect I may be missing a crucial step or misconfiguring something.

I would greatly appreciate a detailed example of how to access data when files are located within a branch rather than in the main repository. I am confident that this guidance will help me progress effectively.

Thank you in advance for your assistance.

Could you please provide a reproducible code example? It would be very helpful if you could create a sample public project with a single photo in a branch.

1 Like

It appears that no additional details have been provided regarding the current thread.

In the absence of further information, we are unable to proceed with the discussion.

If you have any additional details to share or questions to ask, please feel free to start a new thread.

Thank you for your understanding.