1

I'm trying to query my data for records with a given firstname/lastname pair (see Map firstname-lastname pairs to IDs for some previous context) using a couple different methods, and failing.

API4

The first way is using cv and some PHP code:

<?php

    $result = civicrm_api4('Contact', 'get', [
        'select' => ['id'],
        'where' => [['first_name', '=', 'Ken']],
        'limit' => 2,
    ]);
    echo $result->count(), "\n";

invoked via sudo -u daemon `which cv` scr my_script.php.

This prints 0, though there are several records in my database with the first name 'Ken'.

REST API

The second way I've tried is to hit the REST API through some Python code, like so:

import requests

args = dict(
    entity='Contact',
    action='get',
    json='{"sequential":1,"return":"id","first_name":"Ken"}',
    api_key='my-api-key',
    key='my-site-key'
)

result = requests.get('http://my-site/modules/contrib/civicrm/extern/rest.php', params=args)

This gives me:

b'Could not load the settings file at: /bitnami/drupal/modules/contrib/civicrm/../..//default/civicrm.settings.php\n'

so I'm thinking my REST API might not quite be functional? My settings file is at /bitnami/drupal/sites/default/civicrm.settings.php, which is not what it seems to be looking for.

Question

How can I get one of these methods working?

Details

I'm using CiviCRM 5.42.1 on Drupal 7.82.

1 Answer 1

2

For your cv call either turn off the permission check or specify a user to run with.

So either:

<?php
    $result = civicrm_api4('Contact', 'get', [
        'checkPermissions' => FALSE,
        'select' => ['id'],
        'where' => [['first_name', '=', 'Ken']],
        'limit' => 2,
    ]);
    echo $result->count(), "\n";

or

sudo -u daemon `which cv` scr -U admin my_script.php

where admin is a CMS user with appropriate powers.

FYI, with APIv3 check_permissions defaults to 0 whereas APIv4 checkPermissions defaults to True.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.