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

Following your instructions, it looks like Composer installed something somewhere:

No composer.lock file present. Updating dependencies to latest instead of installing from lock file. See Basic usage - Composer for more information.
Loading composer repositories with package information
Updating dependencies
Lock file operations: 2 installs, 0 updates, 0 removals
- Locking mautic/api-library (3.0.0)
- Locking psr/log (1.1.4)
Writing lock file
Installing dependencies from lock file
Package operations: 2 installs, 0 updates, 0 removals
- Downloading psr/log (1.1.4)
- Downloading mautic/api-library (3.0.0)
- Installing psr/log (1.1.4): Extracting archive
- Installing mautic/api-library (3.0.0): Extracting archive
Generating autoload files

Trying the example code from above, from both @mzagmajster and @joeyk, I don’t get any more errors, but also no results or $contact output.

Is there a way to check if it is actually connecting to the API? I tried echo $contactApi etc., but no results.

I still get this error:

Fatal Error: Uncaught Error: Class ‘Mautic\Auth\ApiAuth’ not found in /home/username/public_html/myothersite/site/assets/cache/FileCompiler/site/templates/process/functions.php: …

This error appears when I put the code in a functions.php file in my CMS; putting it in other files produces a similar error. Putting it in a plain php file outside of the CMS I get no error, but also no results/response and don’t have logs to check why not.

I put echo 'WE LIVE'; in autoload.php and that does show up where it is included, but apparently autoload.php cannot find Class 'Mautic\Auth\ApiAuth'?

@modifiedcontent

You successfully installed mautic api library. If you followed my instructions inside lib folder I told you to create there should now be another folder named vendor.

Modify line with include from snippet I posted so that script loads autoload.php from lib/vendor folder. Please also note that autload.php from lib/vendor folder should only be loaded once.

And delete any cache you are using with your CMS while testing this.

If you put something like:

var_dump($contactApi);

after variable initialization you should be able to detrmine if connections succeeds. Until you get an error complaining about ApiAuth class and using mautic api library, you are for sure not connecting to any api.

1 Like

Thanks. This is really messy with all these files in the wrong, non-standard places.

Could I install the API library in the normal /vendor/ folder by temporarily replacing composer.json at the Mautic root level with your stripped down version for mautic/api-library and then run composer install --no-dev from there?

Or edit the composer.json to include mautic/api-library and edit something out to skip the willdurand/oauth-server-bundle bug?

Or could I manually move files to the right places? Edit a few paths? I could do search/replace with grepWin.

I do get some kind of result now:

array(1) { [“errors”]=> array(1) { [0]=> array(2) { [“code”]=> int(401) [“message”]=> string(161) “The response has unexpected status code (401). Response: {“errors”:[{“message”:“Authorization denied, invalid credentials.”,“code”:401,“type”:“access_denied”}]}” } } }

NULL

When I change userName to username, the error disappears, but I don’t get any other response or result.

1 Like

Do not replace composer.json of mautic. You can add mautic api library to existing composer file (in fact thats what I suggested first).

The fact that you have some issues with dependencies does not sound good. It makes me belive that your composer.json and composer.lock files are corrupted. Can you make a backup of entire source code with vendor folder and everything and then:

Core dependencies should be installed without any issue. Once this is installed modify composer.json file in a way that you add mautic api library

I only get this same bug reported here.

Thanks for the instructions to reinstall the Composer dependencies. After doing that, can I simply delete the /lib/ folder? Or would Composer also have installed files in other places?

Since you do not want to install mautic api library as part of existing “bundle” of dependencies …

It’s not that I “do not want” to install the API library that way; I can’t because of that bug, incomplete documentation with dead links and no response from the Mautic community to these issues - thank you for helping me, but I have now “solved” it with workarounds and the reported issues still have not been addressed, including the original question of this thread, how to add the API without having to use Composer.

you can delete lib folder once you installed mautic core dependencies again.

1 Like

Continuing with API example on the appropriate thread…

Hi, API user is a user in the system - it is the simple authentication way.

1 Like

Following your instructions, I removed the original /vendor/ folder, copied composer.json and .lock from the links you provided and ran composer install --no-dev which recreated the /vendor/ folder, but apparently still minus the API library - why is that thing not core Mautic anyway?!

To include the API I had to wurm a bit of code you provided earlier into the composer.json file. This was my first guess how to do that:

{
“name”: “mautic/mautic”,
“license”: “GPL-3.0”,
“type”: “project”,
“description”: “Mautic Open Source Distribution”,
“require”: {
“composer/installers”: “^1.11”,
“mautic/core-lib”: “^3.0”
},
“require”: {
“mautic/api-library”: “^3.0”
},
“require-dev”: {
“friendsofphp/php-cs-fixer”: “~2.16.1”,

That produced this error in SSH:

Key require is a duplicate in ./composer.json at line 12

Makes sense, so I guess I should include it like this:

“require”: {
“composer/installers”: “^1.11”,
“mautic/core-lib”: “^3.0”,
“mautic/api-library”: “^3.0”
},

Either way, Composer does not seem to actually install the API library with composer install --no-dev when I add that.

So I tried to install the API library again with composer require mautic/api-library --with-all-dependencies, but it produces the exact same error as before:

Your requirements could not be resolved to an installable set of packages.

Problem 1
- Root composer.json requires mautic/core-lib ^3.0 → satisfiable by mautic/core-lib[3.x-dev].
- mautic/core-lib 3.x-dev requires willdurand/oauth-server-bundle dev-release-0.0.3 → found willdurand/oauth-server-bundle[dev-master, 0.0.1] but it does not match the constraint.

Installation failed, reverting ./composer.json and ./composer.lock to their original content.

The same stupid bug* reported last year that apparently nobody at Mautic cares about, like nobody cares about incomplete and outdated API documentation with dead links.

Anyway, there is still no answer to the original question of this thread, how to use the API without Composer. It really wasn’t my intention to hijack the thread. I was looking for a non-Composer solution, because Composer is a dead end for me - still is. Couldn’t I just move the files from /lib/ to /vendor/ and fix a few links? Or does Composer really perform some unseen magic elsewhere on the server?

I love Mautic, am rooting for it to become the open source marketing automation standard, appreciate all your hard work and support, but please, get your &*$%! together.

I’ll stumble on with the /lib/ folder workaround, trying to get my month-long delayed project back on track.

* there should be an easy fix simply changing ‘0.0.3’ to ‘0.0.1’ somewhere, but where?

Hi @modifiedcontent ,

TLDR:

  1. I prepared vendor package for M3.3.4 & 4.0.0 (prod. dep. only) I will post link to download it, you can place them on your server and see if works (both have api library already installed). Please note that I installed this from scratch on isolated system. Also know that post might be blocked by spam filter so it might take an admin person to review to be visible to you.

  2. [If 1., does not work]Can you install dependencies without Mautic api library, then install the library as its suggested here: GitHub - mautic/api-library: Mautic API Library (the reason I am asking this is is because I took the same approach when setting up my instances and it worked, its tested and it works on three different instances). No manual mingling with the contents of composer files.

Now let me address some other points as well:

I think it has to do with the fact that Mautic api library is not essential in using the api, it just enables you to work with the API on a higher level. Also primarily I think lib is meant to be used by other projects to communicate with Mautic. I think if this would be included in the core there would be a lot of cases where api library would just sit in the vendor folder not doing anything but hit the preformance of the whole app.

You can try, but make the backup first. You see the tools like composer are created for a reason by using it half-way you are just risking to stumble on another layer of problems that you would eliminate just by using the tool properly.

You can call api via library like curl (which api library depends on anyway). You can see actual endpoints and authorization techniques in Mautic Developer documentation: < Mautic Developer Documentation >, so far all the endpoints I tried seem to work, if anything does not try investigating source code of api library or mautic core itself (I know not ideal).

  1. In regards to documentation I agree with you - its not the area where Mautic shines, but if I am not mistaken community is trying to fix that and any contributions in that regard are highly appreciated.
1 Like

Make sure to set proper permissions on the folder once you copy it to your server: < mautic-public - Google Drive >.

Hope it helps.

1 Like

Hello Guys ,
I am trying to configure the mautic library to share the contacts from my website to mautic . but I getting error 500 fetal error every time when I am configuring it through php and mautic library.

I tried with curl also it is responding …
{“errors”:[{“message”:“API authorization denied.”,“code”:401,“type”:“access_denied”}]}{“errors”:[{“message”:“API authorization denied.”,“code”:401,“type”:“access_denied”}]}

This is the code which I used to get the auth token .

<?php require_once __DIR__ . '/lib/MauticApi.php'; require_once __DIR__ . '/lib/Auth/OAuth.php'; use lib\Auth\ApiAuth; // $initAuth->newAuth() will accept an array of OAuth settings $settings = array( 'baseUrl' => 'https://mautic.virtualcoaching365.com/mautic', 'version' => 'OAuth2', 'clientKey' => '****', 'clientSecret' => '****', 'callback' => 'https://createaivoiceovers.com/mautic/' ); // Initiate the auth object $initAuth = new ApiAuth(); $auth = $initAuth->newAuth($settings); // Initiate process for obtaining an access token; this will redirect the user to the authorize endpoint and/or set the tokens when the user is redirected back after granting authorization if ($auth->validateAccessToken()) { if ($auth->accessTokenUpdated()) { $accessTokenData = $auth->getAccessTokenData(); print_r($accessTokenData); //store access token data however you want } }

I think you can try manually including the necessary files for the Mautic API in your script. You can find the latest version of the API on the Mautic website and then use the require or include a statement to include it in your script. Another option to consider is using a tool like Zaptest to automate your API testing process. This way you won’t have to worry about including the necessary files manually, and you’ll be able to run your tests with ease. Plus, this service also provides a free version, so you can test it out and see if it works for you.