# Not able to connect Mautic API from PHP library

**URL:** <https://forum.mautic.org/t/not-able-to-connect-mautic-api-from-php-library/7980>\
**Category:** Development\
**Created:** [March 21, 2017, 4:50am UTC](https://forum.mautic.org/t/not-able-to-connect-mautic-api-from-php-library/7980 "2017-03-21T04:50:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![aravin](https://avatars.discourse-cdn.com/v4/letter/a/c4cdca/32.png) [@aravin](https://forum.mautic.org/u/aravin)\
**Post date:** [March 21, 2017, 4:50am UTC](https://forum.mautic.org/t/not-able-to-connect-mautic-api-from-php-library/7980/1 "2017-03-21T04:50:32Z")

</div>

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>

```

---

<div class="post-metadata">

**Author:** ![MarkLL](https://avatars.discourse-cdn.com/v4/letter/m/d9b06d/32.png) [@MarkLL](https://forum.mautic.org/u/MarkLL)\
**Post date:** [March 22, 2017, 10:39am UTC](https://forum.mautic.org/t/not-able-to-connect-mautic-api-from-php-library/7980/2 "2017-03-22T10:39:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![aravin](https://avatars.discourse-cdn.com/v4/letter/a/c4cdca/32.png) [@aravin](https://forum.mautic.org/u/aravin)\
**Post date:** [March 21, 2017, 4:50am UTC](https://forum.mautic.org/t/not-able-to-connect-mautic-api-from-php-library/7980/3 "2017-03-21T04:50:32Z")

</div>

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]
```

---

<div class="post-metadata">

**Author:** ![aabilio](https://avatars.discourse-cdn.com/v4/letter/a/3bc359/32.png) [@aabilio](https://forum.mautic.org/u/aabilio)\
**Post date:** [March 21, 2017, 11:03am UTC](https://forum.mautic.org/t/not-able-to-connect-mautic-api-from-php-library/7980/4 "2017-03-21T11:03:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![aravin](https://avatars.discourse-cdn.com/v4/letter/a/c4cdca/32.png) [@aravin](https://forum.mautic.org/u/aravin)\
**Post date:** [March 22, 2017, 6:14am UTC](https://forum.mautic.org/t/not-able-to-connect-mautic-api-from-php-library/7980/5 "2017-03-22T06:14:57Z")

</div>

Hi aabilio,

Thanks for your reply.

Enabled the API and HTTP basic auth.  
Please refer the below image.  
[Image Here](https://ibb.co/mypZBF)

---

<div class="post-metadata">

**Author:** ![aabilio](https://avatars.discourse-cdn.com/v4/letter/a/3bc359/32.png) [@aabilio](https://forum.mautic.org/u/aabilio)\
**Post date:** [March 22, 2017, 3:14pm UTC](https://forum.mautic.org/t/not-able-to-connect-mautic-api-from-php-library/7980/6 "2017-03-22T15:14:44Z")

</div>

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