Mautic Community Forums

Not able to connect Mautic API from PHP library

Hi,



I am trying to connect Mautic API via PHP useing PHP library.



Tried with basci authntication and Oauth1 but didn;t get successfull. It always throw an error like “Requested URL not found: /mautic/index.php/api/contacts?orderByDir=ASC. ‘message’ => string ‘Requested URL not found: /mautic/index.php/api/contacts?orderByDir=ASC (error is deprecated as of 2.6.0 and will be removed in 3.0. Use the errors array instead.)’ (length=166)

‘code’ => int 404”



This is my code.

Code:
include "../vendor/autoload.php"; require_once '../lib/MauticApi.php'; use MauticAuthApiAuth; use MauticMauticApi;
$apiUrl = "http://localhost/mautic/index.php";

 $settings = array(
     'AuthMethod'       => 'BasicAuth', // Must be one of 'OAuth' or 'BasicAuth'
     'userName'         => 'admin', // The username for authentication; Best practise would be to set up a new user for each external site
     'password'         => 'Admin123!',         // Make this a Long passPhrase e.g. (Try.!wE4.And.*@ws4.Guess.!a4911.This.*-+1.Sucker.!)
     'apiUrl'           => 'http://localhost/mautic/index.php',         // NOTE: Required for Unit tests; *must* contain a valid url
 );

try {
	// Initiate the auth object specifying to use BasicAuth
	$initAuth = new ApiAuth();
	$auth = $initAuth->newAuth($settings, 'BasicAuth');

	if ($auth) {
		
		$api = new MauticApi();
		$contactApi = $api->newApi('contacts', $auth, $apiUrl);
		
		// Get Contact list
		$results = $contactApi->getList();
		var_dump($results);
	} else {
		echo "Something went wrong!";
	}
} catch (Exception $e) {
	echo "Mautic not connected!";	
}</div>

Hi @aravin try removing the index.php from your URL.

Hi,

I am trying to connect Mautic API via PHP useing PHP library.

Tried with basci authntication and Oauth1 but didn;t get successfull. It always throw an error like “Requested URL not found: /mautic/index.php/api/contacts?orderByDir=ASC. ‘message’ => string ‘Requested URL not found: /mautic/index.php/api/contacts?orderByDir=ASC (error is deprecated as of 2.6.0 and will be removed in 3.0. Use the errors array instead.)’ (length=166)
‘code’ => int 404”

This is my code.

[code]include “…/vendor/autoload.php”;
require_once ‘…/lib/MauticApi.php’;
use MauticAuthApiAuth;
use MauticMauticApi;

$apiUrl = "http://localhost/mautic/index.php";

 $settings = array(
     'AuthMethod'       => 'BasicAuth', // Must be one of 'OAuth' or 'BasicAuth'
     'userName'         => 'admin', // The username for authentication; Best practise would be to set up a new user for each external site
     'password'         => 'Admin123!',         // Make this a Long passPhrase e.g. (Try.!wE4.And.*@ws4.Guess.!a4911.This.*-+1.Sucker.!)
     'apiUrl'           => 'http://localhost/mautic/index.php',         // NOTE: Required for Unit tests; *must* contain a valid url
 );

try {
	// Initiate the auth object specifying to use BasicAuth
	$initAuth = new ApiAuth();
	$auth = $initAuth->newAuth($settings, 'BasicAuth');

	if ($auth) {
		
		$api = new MauticApi();
		$contactApi = $api->newApi('contacts', $auth, $apiUrl);
		
		// Get Contact list
		$results = $contactApi->getList();
		var_dump($results);
	} else {
		echo "Something went wrong!";
	}
} catch (Exception $e) {
	echo "Mautic not connected!";	
}[/code]

hi @aravin did you just activate the API and HTTP basic auth at http://localhost/mautic/s/config/edit ?
I had a similar problem weeks ago, and that was my mistake…

Hi aabilio,

Thanks for your reply.

Enabled the API and HTTP basic auth.
Please refer the below image.
Image Here

@aravin try to clear the cache. More info here -> https://mautic.org/docs/en/tips/troubleshooting.html