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.
cv scr
, you either need to pass--user=some_wordpress_username
on the command line, or instead ofget()
useget(FALSE)
. When run withcv api4
, it seems to automatically bypasses permissions - this appears to be on purpose.