Mautic Community Forums

How to save the $auth for each request?

Hello! I’m new in Mautic and I’m having some trouble trying to understand how to do the requests on the API.



When the user enter in my page, the authentication begins and I receive the access_token normally.

Later, I created another file( invitation.php) which is responsible for create a new contact inside Mautic.



This invitation, I call with AJAX from JS.

Code:
<?php
include_once __DIR__ . '/mauticapi/vendor/autoload.php';
use MauticMauticApi;

$data = array(
    'firstname' => '',
    'lastname'  => '',
    'email'     => $_POST['email'],
    'ipAddress' => $_SERVER['REMOTE_ADDR']
);

$api = new MauticApi();
$contactApi = $api->newApi('contacts', $auth, $apiUrl);
$contact = $contactApi->create($data);

?>


The problem is that I loose the reference to $auth.
Could someone explain me the better way to do this?

Thanks so much!

Hello! I’m new in Mautic and I’m having some trouble trying to understand how to do the requests on the API.

When the user enter in my page, the authentication begins and I receive the access_token normally.
Later, I created another file( invitation.php) which is responsible for create a new contact inside Mautic.

This invitation, I call with AJAX from JS.

[code]<?php

include_once __DIR__ . '/mauticapi/vendor/autoload.php';
use MauticMauticApi;

$data = array(
    'firstname' => '',
    'lastname'  => '',
    'email'     => $_POST['email'],
    'ipAddress' => $_SERVER['REMOTE_ADDR']
);

$api = new MauticApi();
$contactApi = $api->newApi('contacts', $auth, $apiUrl);
$contact = $contactApi->create($data);

?>[/code]

The problem is that I loose the reference to $auth.
Could someone explain me the better way to do this?

Thanks so much!

Hi @italoborges you need to save the tokens each time because if you are using OAuth2 it will change at some point!. See the main API page https://github.com/mautic/api-library it shows you code that you need to use to check if the tokens have changed. If they have ,you need to save it for next time. The API does not provide any storage for the tokens so you need to look after them.

As an alternative, you could consider using BasicAuth. This should be available in the next API release.

Thank you very much MarkLL!!! It helped a lot!
I’m going to store the token on database.