Uname: Linux p3plzcpnl499967.prod.phx3.secureserver.net 4.18.0-553.54.1.lve.el8.x86_64 #1 SMP Wed Jun 4 13:01:13 UTC 2025 x86_64
Software: Apache
PHP version: 8.2.30 [ PHP INFO ] PHP os: Linux
Server Ip: 208.109.40.231
Your Ip: 216.73.216.26
User: nayff91c5tsx (10005085) | Group: nayff91c5tsx (10005085)
Safe Mode: OFF
Disable Function:
NONE

name : API.php
<?php

namespace FluentFormPro\Integrations\Insightly;

use FluentForm\Framework\Helpers\ArrayHelper;

if (!defined('ABSPATH')) {
    exit; // Exit if accessed directly.
}

class API
{
    public $apiKey = null;

    public $url = null;

    public function __construct($settings)
    {
        $this->url = trim($settings['url']);
        $this->apiKey = 'Basic ' . base64_encode($settings['api_key'] . ':' . ' ');
    }

    public function checkAuth()
    {
        return $this->makeRequest($this->url . '/v3.1/instance');
    }

    public function makeRequest($url, $bodyArgs = [], $type = 'GET')
    {
        $request = [];
        if ($type == 'GET') {
            $request = wp_remote_get($url, [
                'headers' => [
                    'Content-Type'  => 'application/json',
                    'Accept'        => 'application/json',
                    'Authorization' => $this->apiKey,
                ]
            ]);
        }

        if ($type == 'POST') {
            $request = wp_remote_post($url, [
                'headers' => [
                    'Content-Type'  => 'application/json',
                    'Accept'        => 'application/json',
                    'Authorization' => $this->apiKey,
                ],
                'body' => $bodyArgs
            ]);
        }

        if (is_wp_error($request)) {
            $code = $request->get_error_code();
            $message = $request->get_error_message();
            return new \WP_Error($code, $message);
        }

        $body = wp_remote_retrieve_body($request);
        $body = \json_decode($body, true);
        $code = wp_remote_retrieve_response_code($request);

        if ($code == 200 || $code == 201) {
            return $body;
        } else {
            $message = ArrayHelper::get($body, 'Message');
            if(empty($message)){
                $message = ArrayHelper::get($body, '0.Message','Something went wrong please check again!');
            }
            return new \WP_Error($code, $message);
        }
    }

    public function subscribe($subscriber)
    {
        $url = $this->url . '/v3.1/' . $subscriber['list_id'];

        $post = \json_encode($subscriber['attributes'], true);

        $response = $this->makeRequest($url, $post, 'POST');

        return $response;
    }
}
© 2026 GrazzMean