# Problem with Mautic Api

**URL:** <https://forum.mautic.org/t/problem-with-mautic-api/6081>\
**Category:** Product Support\
**Created:** [October 4, 2016, 2:24pm UTC](https://forum.mautic.org/t/problem-with-mautic-api/6081 "2016-10-04T14:24:16Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![wifiexpress](https://avatars.discourse-cdn.com/v4/letter/w/5fc32e/32.png) [@wifiexpress](https://forum.mautic.org/u/wifiexpress)\
**Post date:** [October 4, 2016, 2:24pm UTC](https://forum.mautic.org/t/problem-with-mautic-api/6081/1 "2016-10-04T14:24:16Z")

</div>

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](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](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

---

<div class="post-metadata">

**Author:** ![wifiexpress](https://avatars.discourse-cdn.com/v4/letter/w/5fc32e/32.png) [@wifiexpress](https://forum.mautic.org/u/wifiexpress)\
**Post date:** [October 4, 2016, 2:24pm UTC](https://forum.mautic.org/t/problem-with-mautic-api/6081/2 "2016-10-04T14:24:16Z")

</div>

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](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](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:

```auto
Process finished with exit code 0
```
