# create contact code is not working

**URL:** <https://forum.mautic.org/t/create-contact-code-is-not-working/10577>\
**Category:** Product Support\
**Created:** [June 10, 2018, 1:28am UTC](https://forum.mautic.org/t/create-contact-code-is-not-working/10577 "2018-06-10T01:28:30Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![sarthakinfotech](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/sarthakinfotech/32/336_2.png) [@sarthakinfotech](https://forum.mautic.org/u/sarthakinfotech)\
**Post date:** [June 10, 2018, 1:28am UTC](https://forum.mautic.org/t/create-contact-code-is-not-working/10577/1 "2018-06-10T01:28:30Z")

</div>

hi i have below code for creating contact via api , it execute fine but not creates any contact in mautic why ??

code is as below

\<?php

// Bootup the Composer autoloader  
include **DIR**. ‘/vendor/autoload.php’;

use Mautic\Auth\ApiAuth;

session\_start();

$publicKey = ‘’;  
$secretKey = ‘’;  
$callback = ‘’;

// ApiAuth-\>newAuth() will accept an array of Auth settings  
$settings = array(  
‘baseUrl’ =\> ‘[http://localhost/mautic](http://localhost/mautic)’, // Base URL of the Mautic instance  
‘version’ =\> ‘OAuth2’, // Version of the OAuth can be OAuth2 or OAuth1a. OAuth2 is the default value.  
‘clientKey’ =\> ‘1\_1w6nrty8k9og0kow48w8w4kww8wco0wcgswoow80ogkoo0gsks’, // Client/Consumer key from Mautic  
‘clientSecret’ =\> ‘id6dow060fswcswgsgswgo4c88cw0kck4k4cc0wkg4gows08c’, // Client/Consumer secret key from Mautic  
‘callback’ =\> ‘[http://localhost/mtest/process.php](http://localhost/mtest/process.php)’ // Redirect URI/Callback URI for this script  
);

/\*  
// If you already have the access token, et al, pass them in as well to prevent the need for reauthorization  
$settings[‘accessToken’] = $accessToken;  
$settings[‘accessTokenSecret’] = $accessTokenSecret; //for OAuth1.0a  
$settings[‘accessTokenExpires’] = $accessTokenExpires; //UNIX timestamp  
$settings[‘refreshToken’] = $refreshToken;  
\*/

// Initiate the auth object  
$initAuth = new ApiAuth();  
$auth = $initAuth-\>newAuth($settings);  
/\*  
if( $auth-\>getAccessTokenData() != null ) {  
$accessTokenData = $auth-\>getAccessTokenData();  
$settings[‘accessToken’] = $accessTokenData[‘access\_token’];  
$settings[‘accessTokenSecret’] = ‘id6dow060fswcswgsgswgo4c88cw0kck4k4cc0wkg4gows08c’; //for OAuth1.0a  
$settings[‘accessTokenExpires’] = $accessTokenData[‘expires’]; //UNIX timestamp  
$settings[‘refreshToken’] = $accessTokenData[‘refresh\_token’];  
}\*/  
// Initiate process for obtaining an access token; this will redirect the user to the $authorizationUrl and/or  
// set the access\_tokens when the user is redirected back after granting authorization

// If the access token is expired, and a refresh token is set above, then a new access token will be requested

try {  
if ($auth-\>validateAccessToken()) {

```
    // Obtain the access token returned; call accessTokenUpdated() to catch if the token was updated via a
    // refresh token

    // $accessTokenData will have the following keys:
    // For OAuth1.0a: access_token, access_token_secret, expires
    // For OAuth2: access_token, expires, token_type, refresh_token

    if ($auth-&gt;accessTokenUpdated()) {
        $accessTokenData = $auth-&gt;getAccessTokenData();
        echo "<pre>";
        print_r($accessTokenData);
        echo "</pre>";
        //store access token data however you want
    }
}

```

} catch (Exception $e) {  
// Do Error handling  
}

use Mautic\MauticApi;  
//use Mautic\Auth\ApiAuth;

// …  
$initAuth = new ApiAuth();  
$auth = $initAuth-\>newAuth($settings);  
$apiUrl = “[http://localhost/mautic/api](http://localhost/mautic/api)”;  
$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);  
echo “\<br/\>contact created”;

NOTE : I have setup mautic at local pc

---

<div class="post-metadata">

**Author:** ![silavapi](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/silavapi/32/7424_2.png) [@silavapi](https://forum.mautic.org/u/silavapi)\
**Post date:** [October 17, 2019, 4:33pm UTC](https://forum.mautic.org/t/create-contact-code-is-not-working/10577/2 "2019-10-17T16:33:42Z")

</div>


