PHP code example of livepixel / laravel-mautic-api

1. Go to this page and download the library: Download livepixel/laravel-mautic-api library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

livepixel / laravel-mautic-api example snippets




// Bootup the Composer autoloader
''; 
$secretKey = ''; 
$callback  = ''; 

// ApiAuth::initiate will accept an array of OAuth settings
$settings = array(
    'baseUrl'          => '',       // Base URL of the Mautic instance
    'version'          => 'OAuth2'  // Version of the OAuth can be OAuth2 or OAuth1a. OAuth2 is the default value.
    'clientKey'        => '',       // Client/Consumer key from Mautic
    'clientSecret'     => '',       // Client/Consumer secret key from Mautic
    'callback'         => ''        // 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
$auth = ApiAuth::initiate($settings);

// 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

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->accessTokenUpdated()) {
        $accessTokenData = $auth->getAccessTokenData();
        
        //store access token data however you want
    }
}



use Mautic\MauticApi;

// Create an api context by passing in the desired context (Leads, Forms, Pages, etc), the $auth object from above
// and the base URL to the Mautic server (i.e. http://my-mautic-server.com/api/)

$leadApi = MauticApi::getContext("leads", $auth, $apiUrl);



$lead = $leadApi->get($id);

// getList accepts optional parameters for filtering, limiting, and ordering
$leads = $leadApi->getList($filter, $start, $limit, $orderBy, $orderByDir);



$fields = $leadApi->getFieldList();

$data = array();

foreach ($fields as $f) {
    $data[$f['alias']] = $_POST[$f['alias']];
}

// Set the IP address the lead originated from if it is different than that of the server making the request
$data['ipAddress'] = $ipAddress;
 
// Create the lead 
$lead = $leadApi->create($data);



$updatedData = array(
    'firstname' => 'Updated Name'
);

$result = $leadApi->edit($leadId, $updatedData);

// If you want to create a new lead in the case that $leadId no longer exists
// $result will be populated with the new lead item
$result = $leadApi->edit($leadId, $updatedData, true);



$result = $leadApi->delete($leadId);



// $result returned by an API call should be checked for errors
$result = $leadApi->delete($leadId);

if (isset($result['error'])) {
    echo $result['error']['code'] . ": " . $result['error']['message'];
} else {
    // do whatever with the info
}