Situation: I want to call the Mautic API and get a response, which isn’t an error.
I use the API library (https://github.com/mautic/api-library).
The API is enabled in the configuration settings and I have authorized my requests with oAuth1a.
Everything’s working fine with the authorization.
Mautic is installed in the subdirectory ‘mautic’.
Whenever I make a call to the API I get the response, that the requested URL has not been found:
The $json_response of the following code is:
{“contact”:{“error”:{“message”:“Requested URL not found: /mautic/api/contacts/new”,“code”:404}}}
So /mautic/api/contacts/new has not been found. This happens with every endpoint!
Even when I use the API Tester exactly the same happens.
<?php
include __DIR__ . '/vendor/autoload.php';
use MauticAuthApiAuth;
use MauticMauticApi;
$settings = array(
'baseUrl' => 'http://mysite.com/mautic',
'version' => 'OAuth1a',
'clientKey' => 'xxxxxxxxxxxxxxx',
'clientSecret' => 'xxxxxxxxxxxxxxx',
'callback' => 'http://mysite.com/go.php', // used before to get access tokens (see below)
'accessToken' => 'xxxxxxxxxxxxxxx', // <-- hard coded access token data
'accessTokenSecret' => 'xxxxxxxxxxxxxxx' // <-- hard coded access token data
);
$initAuth = new ApiAuth();
$auth = $initAuth->newAuth($settings);
$apiUrl = "http://mysite.com/mautic";
$api = new MauticApi();
$contactApi = $api->newApi("contacts", $auth, $apiUrl);
$data = array(
'firstname' => 'Jim',
'lastname' => 'Contact',
'email' => 'jim@his-site.com',
'ipAddress' => $_SERVER['REMOTE_ADDR']
);
$contact = $contactApi->create($data);
$json_response['contact'] = $contact;
header('Content-Type: application/json');
echo json_encode($json_response);
Running the command mentioned above didn’t fix the problem for me, but I fixed it by running the clear:cache command from Mautic - the command will look something like: php /var/www/html/mautic/bin/console cache:clear
( change the path to your Mautic installation - and if you have a pre-3 version , it will be “ app/console ” for you instead of “ bin ” )
(PD: If you get some “can’t locate php” error, change the “ php ” part of the code by the path you have in your crons pointing to php (to find it, write “ crontab -e ” or “ sudo crontab -e ” on the command line and copy the string/path between “-c” and “-q” (which in my case is “/opt/bitnami/php/bin/php” since I installed it through Bitnami)
If / when this blows up someone’s setup… try the advice here to check that your site setting in the local.php file is prefixed correctly (http or https) to match whatever load-balancer / redirection scheme you have.
the base url is well set without / slash at the end…
Otherwise I decided to delete connection and do a fresh one. Unfortunately I have digressed! as now connection wont work after clearing cache and also Enabling API and HTTP basic auth
Is it installed in a subdirectory … es with the initial post?
Don’t forget to adjust the provided .htaccess - as this only works with Mautic in root folders of the webserver document directory. If Mautic runs on TLD/mautic/ or similar, .htaccess needs an edit after each update.