Mautic API Call – Contacts

[insert_php]

$string = file_get_contents(“investors.json”);
$json_a = json_decode($string, true);

$username=$_SERVER[“PHP_AUTH_USER”];
if(isset($json_a[$username])){
echo “
User: “.$json_a[$username];
}
else{
require_once ‘/home/endofpet/public_html/wp-content/themes/Divi/api-library-master/vendor/autoload.php’;
//use Mautic\Auth\ApiAuth;
//use Mautic\MauticApi;

$settings = array(
‘userName’ => ‘navid@endofpetroleum.com’,
‘password’ => ‘Testing4InsideBid’
);

// Initiate the auth object specifying to use BasicAuth
$initAuth = new Mautic\Auth\ApiAuth();
$auth = $initAuth->newAuth($settings, ‘BasicAuth’);

$apiUrl = “https://investor:investor@endofpetroleum.com/mautic/”;
$api = new Mautic\MauticApi();

$contactApi = $api->newApi(“contacts”, $auth, $apiUrl);

//$searchFilter, $start, $limit, $orderBy, $orderByDir, $publishedOnly, $minimal
$contacts = $contactApi->getList(”, 0, 100, ”, ”, true, ”);
//echo json_encode($contacts).”

“;
foreach($contacts[‘contacts’] as $contact){
//echo json_encode($contact).”

“;
$email=$contact[‘fields’][‘core’][’email’][‘value’];
$x=$contact[‘fields’][‘core’][‘investor_username’][‘value’];
if($x==$username){
$json_a[$username]=$email;
$jsondata = json_encode($json_a, JSON_PRETTY_PRINT);

if(file_put_contents(“investors.json”, $jsondata)) {
echo ‘Data successfully saved’;
}
else{
echo “error”;
}
}
}
}

[/insert_php]