Mautic Community Forums

For hosts with no Composer. How to add the API to a php script with require?

Hey, everyone.

Our shared hosting does not provide Composer as a part of the account so we manually require all the plugins ourselves.

What do we need to require("") to load mautic’s api?
I tried searching the folders for api directory but could not find anything relevant I could autoload.

Hi, i’m also starting to learn how to work with the API. I’d like to send mails with slightly changing content to a segment. Therefore i gather some infos from a external form and trigger sending via API.

As far as i understand one needs to gather the api-libary, which can also be done by downloading the source from the git repository

and unzip ist. Rename the folder created into “lib” and point your require into this.

Composer builds a autoload package and download not. So you may need to include more sources…

I have been wrestling with this here, dragged into Composer hell, still stuck.

I use API without the API library. So much easier.

How does that work? All the Mautic API documentation that I have been able to find refers to a library that has to be installed and/or an /api/ directory that does not exist in my current Mautic 4 installation.

How to Use the Mautic Rest API
Mautic Developer Documentation

Is there a basic ‘hello world’ Mautic API code example anywhere? I have not been able to piece it together with the clues in the documentation.

Either way, is the library required? Optional? Now integrated in Mautic 4? Can someone please clarify. I am not the only one confused about this.

I make a video for you.

Never thought to hear you saying that Joey :wink: But glad to know i’m not the only one doing so. To be honest i’ve written a small class having all i need in my tools/scripts as abstract functions and also found it much easier to use than the official PHP lib.

I won’t do that in the video, but maybe 3-4 exaples would be nice.
However this is what happens 99% of the time:

  1. Enable API and add credentials
  2. Test api and realize it doesn’t work. 404 Error.
  3. Check the folder structure, and realize there is no /api/ folder
  4. Read the docs, watch videos, read the docs
  5. Go through all the 5 stages of grief
  6. Find a post, that finally mentions that YOU ALWAYS CLEAR CACHE
  7. Clear Cache
  8. API works
1 Like

How do I test the API? A ‘hello world’ example would be helpful.

Do you use Postman for API testing?
Or a PHP snipplet would brighten your day?

1 Like

Yes, a PHP snipplet would please us. Never heard of Postman - wasn’t that an email client?

Postman is an API testing tool. It is a true delight to use.
https://www.postman.com/

Snipplet to create a contact with simple identification:

Start your code with defining the variables:

<?php
$loginname = 'apiuser';
// Loginname of your API user
$password = 'yourpasswordhere';
// This is the password of the API user
$siteurl = yoursiteurl.tld;
// example: mymautic.com

Add the payload:

$email = 'captain_enterprise-d@starfleet.com';
$firstname = 'Jean-Luc';
$lastname = 'Picard';
$tag = 'purchased';

And execute the API call:

$curl = curl_init();
// Set some options - we are passing in a user agent too here
  curl_setopt_array($curl, array(
  CURLOPT_RETURNTRANSFER => 1,
  CURLOPT_URL => "https://".$loginname.":".$password."@".$siteurl."/api/contacts/new",
  CURLOPT_USERAGENT => 'Mautic Connector by Joey',
  CURLOPT_POST => 1,
// posting the payload
  CURLOPT_POSTFIELDS => array(
    'firstname' => $firstname,
    'lastname' => $lastname,
    'email' => $email,
    'tags' => $tag
  )
));
curl_exec($curl);

That’s it.

If you go with Postman, here are a bunch of API scripts as a Postman collection:

1 Like

Thanks! That looks like something I can work with. Will try this tonight and report back… (I won’t get to this until the weekend…)

Super important: your custom fields have to be publicly updateable in order to update from API

image

@joeyk, what is ‘the API user’? The Mautic admin or other system users? Or should I set up ‘the API user’ somewhere else?

Should the API credentials not be used anywhere here?

I still don’t have a folder /api/ - clearing the cache does not fix that of course, so I don’t understand that point in your instructions.

What is that ‘user agent’? Is it required? Totally random?

Here is what I put into a template file of my other site - it should fire if you put ?accesscode=something in the URL and then change the firstname of an existing contact identified by email to ‘Ronnie’:

<?php
if ( isset( $_GET['accesscode'] ) ) {
echo $_GET['accesscode'];

// Loginname of your API user
$loginname = 'administrator';
// This is the password of the API user
$password = 'passwordformauticadmin';
// example: mymautic.com
$siteurl = 'https://mywebsite.com/mautic';

$email = 'existing@emailaddress.com';
$firstname = 'Ronnie';

$curl = curl_init();
// Set some options - we are passing in a user agent too here
  curl_setopt_array($curl, array(
  CURLOPT_RETURNTRANSFER => 1,
  CURLOPT_URL => "https://".$loginname.":".$password."@".$siteurl."/api/contacts/new",
  CURLOPT_USERAGENT => 'Modified Mautic Connector',
  CURLOPT_POST => 1,
// posting the payload
  CURLOPT_POSTFIELDS => array(
    'firstname' => $firstname,
    'email' => $email,
  )
));
curl_exec($curl);
}

The accesscode gets echoed, but nothing else happens; the name of the contact is not changed to Ronnie.