Mautic Community Forums

Problem with Mautic Api

Please, I have done this code, but it didn’t work. could someone help me understand what I have done wrong?



this shoud be reading some Json file in a folder and upload the content to the app as Contact (or lead).

Code:
<?php

require DIR . ‘/./vendor/autoload.php’;

use MauticAuthApiAuth;
use MauticMauticApi;

define(“PATH”,"./Json/");
define(“PATH_DONE”,"./Json/done/");

session_start();
$auth=GetAuth();
while (true){
echo serialize($auth);
ScanFolder($auth);
sleep(5000);
};

function ReadJsonAndSendToMautic($json,$auth)
{
$string = file_get_contents($json);
$json_a = (array)json_decode($string, true);

for($idx = 0; $idx < count($json_a); $idx++){
    //echo (string)$json_a[$idx]['email']."       ";
    SendContactToCampaig($json_a[$idx]['email'], $json_a[$idx]['id'], $json_a[$idx]['campaign'],$auth);
}

}

function ScanFolder($auth){
$files = glob(PATH.’*.json’, GLOB_BRACE);
$index=0;
foreach($files as $file) {
ReadJsonAndSendToMautic($file,$auth);
rename(PATH.basename($file),PATH_DONE.basename($file));
$index++;
}
}

function GetAuth(){
$settings = array(
‘baseUrl’ => ‘https://wifiexpress.mautic.net’,
‘version’ => ‘OAuth1a’,
‘clientKey’ => ‘44d1a6vro6kgw0cg8oscooc4cwks0kwkwkw4cso8ow0048s0gw’,
‘clientSecret’ => ‘61o3syyw6g84848og4k0k8wccwcsc4s8k4og40cwkc488gg84c’,
‘callback’ => ‘’
);

$initAuth = new ApiAuth();
$auth = $initAuth->newAuth($settings);
try {
    if ($auth->validateAccessToken()) {
        if ($auth->accessTokenUpdated()) {
            $accessTokenData = $auth->getAccessTokenData();
            echo serialize($accessTokenData);
        }
    }
} catch (Exception $e) {
    die($e->message);
}
echo serialize($auth);
return $auth;

}

function SendContactToCampaig($emailContact,$idContact,$campaign,$auth){
$baseUrl=‘https://wifiexpress.mautic.net’;
$contactApi = MauticApi::newApi(“contacts”, $auth, $baseUrl);
$createIfNotFound = true;

$data = array(
    'firstname' => $emailContact,
    'email'     => $emailContact,
    'tags'      => $campaign
);
$contact = $contactApi->edit($idContact, $data, $createIfNotFound);

}



My actual result is this:
Code:
Process finished with exit code 0

Please, I have done this code, but it didn’t work. could someone help me understand what I have done wrong?

this shoud be reading some Json file in a folder and upload the content to the app as Contact (or lead).

[code]<?php

require DIR . ‘/./vendor/autoload.php’;

use MauticAuthApiAuth;
use MauticMauticApi;

define(“PATH”,"./Json/");
define(“PATH_DONE”,"./Json/done/");

session_start();
$auth=GetAuth();
while (true){
echo serialize($auth);
ScanFolder($auth);
sleep(5000);
};

function ReadJsonAndSendToMautic($json,$auth)
{
$string = file_get_contents($json);
$json_a = (array)json_decode($string, true);

for($idx = 0; $idx < count($json_a); $idx++){
    //echo (string)$json_a[$idx]['email']."       ";
    SendContactToCampaig($json_a[$idx]['email'], $json_a[$idx]['id'], $json_a[$idx]['campaign'],$auth);
}

}

function ScanFolder($auth){
$files = glob(PATH.’*.json’, GLOB_BRACE);
$index=0;
foreach($files as $file) {
ReadJsonAndSendToMautic($file,$auth);
rename(PATH.basename($file),PATH_DONE.basename($file));
$index++;
}
}

function GetAuth(){
$settings = array(
‘baseUrl’ => ‘https://wifiexpress.mautic.net’,
‘version’ => ‘OAuth1a’,
‘clientKey’ => ‘44d1a6vro6kgw0cg8oscooc4cwks0kwkwkw4cso8ow0048s0gw’,
‘clientSecret’ => ‘61o3syyw6g84848og4k0k8wccwcsc4s8k4og40cwkc488gg84c’,
‘callback’ => ‘’
);

$initAuth = new ApiAuth();
$auth = $initAuth->newAuth($settings);
try {
    if ($auth->validateAccessToken()) {
        if ($auth->accessTokenUpdated()) {
            $accessTokenData = $auth->getAccessTokenData();
            echo serialize($accessTokenData);
        }
    }
} catch (Exception $e) {
    die($e->message);
}
echo serialize($auth);
return $auth;

}

function SendContactToCampaig($emailContact,$idContact,$campaign,$auth){
$baseUrl=‘https://wifiexpress.mautic.net’;
$contactApi = MauticApi::newApi(“contacts”, $auth, $baseUrl);
$createIfNotFound = true;

$data = array(
    'firstname' => $emailContact,
    'email'     => $emailContact,
    'tags'      => $campaign
);
$contact = $contactApi->edit($idContact, $data, $createIfNotFound);

}[/code]

My actual result is this:

Process finished with exit code 0