CodeIgniter 3.1.x
application/libraries
The cURL library
<?php defined('BASEPATH') OR exit('No direct script access allowed');
// application/libraries/Ci_bridge.php
class Ci_bridge {
private $base;
private $key;
public function __construct()
{
$CI =& get_instance();
$CI->config->load('ci_bridge', TRUE);
$this->base = $CI->config->item('base_url', 'ci_bridge');
$this->key = $CI->config->item('api_key', 'ci_bridge');
}
public function call($path, $data = NULL, $method = 'GET')
{
$ch = curl_init($this->base . $path);
curl_setopt_array($ch, array(
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_CUSTOMREQUEST => $method,
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json',
'Accept: application/json',
'X-API-Key: ' . $this->key,
),
CURLOPT_POSTFIELDS => is_null($data) ? NULL : json_encode($data),
CURLOPT_TIMEOUT => 15,
));
$body = curl_exec($ch);
$status = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
$error = curl_error($ch);
curl_close($ch);
return array(
'status' => $status,
'data' => json_decode($body, TRUE),
'error' => $error,
);
}
}
Config + controller
<?php
// application/config/ci_bridge.php
$config['base_url'] = 'https://your-app-url/api/v1';
$config['api_key'] = 'ci_live_7f3b2a9c41d84e6fb05c7d31';
// application/controllers/Leads.php
class Leads extends CI_Controller {
public function store()
{
$this->load->library('ci_bridge');
$res = $this->ci_bridge->call('/records', array(
'name' => $this->input->post('name', TRUE),
'email' => $this->input->post('email', TRUE),
'source' => 'ci3',
'reference' => 'LEAD-' . time(),
'payload' => array('plan' => $this->input->post('plan', TRUE)),
), 'POST');
if ($res['status'] === 201) {
$this->session->set_flashdata('ok', 'Synced as #' . $res['data']['id']);
redirect('leads');
}
log_message('error', 'Bridge failed: ' . $res['status']);
show_error('Could not reach the bridge', 502);
}
}
CodeIgniter 4.5.x
app/Config, app/Controllers
Config class + .env
<?php
// app/Config/Bridge.php
namespace Config;
use CodeIgniter\Config\BaseConfig;
class Bridge extends BaseConfig
{
public string $baseUrl = 'https://your-app-url/api/v1';
public string $apiKey = 'ci_live_7f3b2a9c41d84e6fb05c7d31';
}
# .env (overrides the class above, nothing committed)
bridge.baseUrl = 'https://your-app-url/api/v1'
bridge.apiKey = 'ci_live_7f3b2a9c41d84e6fb05c7d31'
CURLRequest controller
<?php
// app/Controllers/Leads.php
namespace App\Controllers;
use Config\Services;
class Leads extends BaseController
{
public function store()
{
$cfg = config('Bridge');
$client = Services::curlrequest([
'baseURI' => rtrim($cfg->baseUrl, '/') . '/',
'timeout' => 15,
'http_errors' => false,
]);
$res = $client->post('records', [
'headers' => [
'X-API-Key' => $cfg->apiKey,
'Accept' => 'application/json',
],
'json' => [
'name' => $this->request->getPost('name'),
'email' => $this->request->getPost('email'),
'source' => 'ci4',
'reference' => 'LEAD-' . time(),
'payload' => ['plan' => $this->request->getPost('plan')],
],
]);
$body = json_decode($res->getBody(), true);
if ($res->getStatusCode() === 201) {
return redirect()->to('/leads')->with('ok', 'Synced as #' . $body['id']);
}
log_message('error', 'Bridge said {0}', [$res->getStatusCode()]);
return redirect()->back()->with('error', $body['error'] ?? 'Bridge unreachable');
}
public function pull()
{
$cfg = config('Bridge');
$client = Services::curlrequest(['baseURI' => rtrim($cfg->baseUrl, '/') . '/']);
$res = $client->get('records?limit=25', [
'headers' => ['X-API-Key' => $cfg->apiKey],
]);
return $this->response->setJSON(json_decode($res->getBody(), true));
}
}