October 22, 2024
Chicago 12, Melborne City, USA
PHP

Issues applying JSON body filters in EU F&T Portal SEARCH API with WordPress integration


I am trying to use the SEARCH API service of the EU F&T Portal public REST APIs for retrieving data for further integration with my project, but I am having some problems.

I am using WordPress as CMS with a plugin called ACF|Advanced Custom Fields. The purpose of the system is to make an API call, retrieve all the results and store each parameter in a custom field and not have to constantly make API calls. According to the API documentation (text), it says that ‘Search criteria (query) can be send in the “Form Data” of the POST request in JSON format’ but I am having trouble implementing the filter in WordPress.

The documentation specifies how to get the query for the filter, so what I want to apply would be something like this: {‘bool’:{‘must’:[{‘terms’:{‘type’:[‘1’, ‘2’, ‘8’]}},{‘terms’:{‘status’:[‘31094501’, ‘31094502’]}}]}} So I would only get responses whose status is 31094501 (Forthcoming) or 31094502 (Open for submission). But when I make the API call, sometimes I get no results and sometimes I get results that do not match the filter applied.

I have tried to apply the filter in this way:

add_action('init', 'register_EUopportunity');

function register_EUopportunity() {
    register_post_type('opportunity', [
        'label' => 'EU opportunities',
        'public' => true,
        'capability_type' => 'post',
    ]);
}

add_action('wp_ajax_nopriv_get_opportunities_from_api', 'get_opportunities_from_api');
add_action('wp_ajax_get_opportunities_from_api', 'get_opportunities_from_api');

function get_opportunities_from_api() {
    $current_page = ( ! empty($_POST['current_page']) ) ? $_POST['current_page'] : 1;
    $opportunities = [];
    
    $body = array(
        'bool' => array(
            'must' => array(
                array(
                    'terms' => array(
                        'type' => array('1', '2', '8')
                    )
                ),
                array(
                    'terms' => array(
                        'status' => array('31094501', '31094502')
                    )
                )
            )
        )
    );

    $args = array(
        'headers' => array(
            'Content-Type' => 'application/json'
        ),
        'body' => json_encode($body),
    );

    $response = wp_remote_post('https://api.tech.ec.europa.eu/search-api/prod/rest/search?apiKey=SEDIA&text=***&pageSize=50&pageNumber=" . $current_page, $args);
    
    if (is_wp_error($response)) {
        return false;
    }
    
    $response_body = wp_remote_retrieve_body($response);
    $response_body = json_decode($response_body, true);
    
    if (!is_array($response_body) || empty($response_body["results'])) {
        return false;
    }
    
    $opportunities = $response_body['results'];
    
    foreach ($opportunities as $opportunity) {
        $opportunity_slug = sanitize_title($opportunity['reference']);
        $inserted_opportunity = wp_insert_post([
            'post_name' => $opportunity_slug,
            'post_title' => $opportunity_slug,
            'post_type' => 'opportunity',
            'post_status' => 'publish'
        ]);
        
        if (is_wp_error($inserted_opportunity)) {
            continue;
        }
        
        $fillable = [
            'field_6708df10da74e' => 'reference',
            'field_6708d325a3d11' => 'url',
            'field_6708d52fe3556' => 'summary',
            'field_6708f69748573' => 'frameworkProgramme',
            'field_670901cfc55e8' => 'deadlineDate',
            'field_67090647166a6' => 'callTitle',
            'field_67090684813cd' => 'identifier',
            'field_6709088554ec8' => 'descriptionByte',
            'field_670f704b8482f' => 'status',
        ];
        
        foreach ($fillable as $key => $name) {
            if ($name === 'frameworkProgramme' && !empty($opportunity['metadata']['frameworkProgramme'])) {
                update_field($key, $opportunity['metadata']['frameworkProgramme'][0], $inserted_opportunity); 
            } elseif($name === 'deadlineDate' && !empty($opportunity['metadata']['deadlineDate'])){
                update_field($key, $opportunity['metadata']['deadlineDate'][0], $inserted_opportunity);
            } elseif($name === 'callTitle' && !empty($opportunity['metadata']['callTitle'])){
                update_field($key, $opportunity['metadata']['callTitle'][0], $inserted_opportunity);
            } elseif($name === 'identifier' && !empty($opportunity['metadata']['identifier'])){
                update_field($key, $opportunity['metadata']['identifier'][0], $inserted_opportunity);
            } elseif($name === 'descriptionByte' && !empty($opportunity['metadata']['descriptionByte'])){
                update_field($key, $opportunity['metadata']['descriptionByte'][0], $inserted_opportunity);
            } elseif($name === 'status' && !empty($opportunity['metadata']['status'])){
                update_field($key, $opportunity['metadata']['status'][0], $inserted_opportunity);
            } elseif (!empty($opportunity[$name])) {
                update_field($key, $opportunity[$name], $inserted_opportunity);
            }
        }
    }
    
    $current_page = $current_page + 1;
    wp_remote_post(admin_url('admin-ajax.php?action=get_opportunities_from_api'), [
        'blocking' => false,
        'sslverify' => false,
        'body' => [
            'current_page' => $current_page
        ]
    ]);
}

If I do not apply any filters, the code seems to work fine. The results are received, added as posts and the custom fields are filled in. But when I apply the filters, what I should get are only results whose status is 31094501 or 31094502, but as I said before, it seems that the filter does not work and sometimes I get no results and sometimes I get results whose status is different from 31094501 or 31094502.

What could be happening? I have also tried using exactly the same filter in POSTMAN. There everything works fine and the expected results are obtained. But in WordPress it does not seem to work.



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video