1

I tried to write a Standalone PHP-Skript for CiviCRM and looked after the Simple Example, but unfortunately it doesnt return something.

<?php
require_once '/var/www/html/wp-content/plugins/civicrm/civicrm/civicrm.config.php';
require_once $civicrm_root . '/CRM/Core/Config.php';


$config = CRM_Core_Config::singleton();

$contacts = \Civi\Api4\Contact::get()
  ->addSelect('first_name')
  ->addWhere('contact_type', '=', 'Individual')
  ->setLimit(25)
  ->execute();

foreach ($contacts as $contact) {
    echo $contact;
}

The variable $civicrm_root is loaded by the civicrm.config.php and is the absolute path to my civiCRM root: /var/www/html/wp-content/plugins/civicrm/civicrm/. I looked up both config files, they exist and I crosschecked the querycode against the APIv4 Explorer, and it works, I dont know what the problem is.

System:

  • Debian 11 VM
  • Apache 2.4.54
  • PHP 7.4.30
  • MariaDB: 10.5.15
  • Wordpress: 6.0.1
  • CiviCRM: 5.52.0 I hope there is a Idea for a solution. Thanks in advance!

Edit I

I applied the suggestions and invoke it with ~/buildkit/bin/cv scr myscript.php but no return value either.

<?php
$config = CRM_Core_Config::singleton();

$contacts = \Civi\Api4\Contact::get()
  ->addWhere('contact_type', '=', 'Individual')
  ->setLimit(25)
  ->execute();

foreach ($contacts as $contact) {
    echo $contact['first_name'];
}

EDIT II

I tried cv api4 Contact.get +w 'contact_type = "Individual"' +l 25 and it works, still: whats wrong with the script?

EDIT III

I insert the line echo count($contacts);, and this return 0.

3
  • 1
    By default api4 enforces permissions. When run with cv scr, you either need to pass --user=some_wordpress_username on the command line, or instead of get() use get(FALSE). When run with cv api4, it seems to automatically bypasses permissions - this appears to be on purpose.
    – Demerit
    Commented Aug 14, 2022 at 17:34
  • Did you see my comment above? You are getting 0 because you are running the script as the anonymous user who doesn't have access to contacts.
    – Demerit
    Commented Aug 17, 2022 at 13:33
  • Yes Thanks! It was really helpful! I have to merge my new profile with my unregistred account to post this.
    – gzwg
    Commented Aug 19, 2022 at 13:37

2 Answers 2

1

I wouldn't do it that way. I'd remove those first two lines and install cv - if you are using buildkit you already have it - and then run it with cv scr myscript.php.

That way it runs on all cmses, and you can do other neat stuff like cv scr --user=demo myscript.php to run it as the user named demo to test different permissions, run it with -vvv to get some debugging, etc...

Separate note: $contact is not a string so you can't echo it. You can do something like var_export($contact).

1

In Combination out of Demerits posts i can solve the problem via

cv scr --user=some_wordpress_username skriptname.php

Thanks for the support.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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