loreal-global-kickoff/OMGService.php
DJP 5a65aca75c Use X-API-Key header for OMG API authentication
Changed from 'Authorization: Bearer' to 'X-API-Key' header format.
Removed duplicate Authorization header.

This is the correct format for OMG API authentication.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-18 09:12:06 -05:00

208 lines
6.7 KiB
PHP

<?php
/**
* OMG API Service
* Handles communication with OMG system for campaign data
*/
class OMGService {
private $config;
private $baseUrl;
private $apiKey;
private $timeout;
public function __construct() {
$appConfig = require __DIR__ . '/config.php';
$this->config = $appConfig['omg_api'];
$this->baseUrl = $this->config['base_url'];
$this->apiKey = $this->config['api_key'];
$this->timeout = $this->config['timeout'] ?? 30;
}
/**
* Get project/campaign information from OMG
*/
public function getProject($projectNumber) {
try {
$url = $this->baseUrl . '/getProject';
$url .= '?project_number=' . urlencode($projectNumber);
error_log('===== OMG API CALL START =====');
error_log('OMG API: Base URL: ' . $this->baseUrl);
error_log('OMG API: Full URL: ' . $url);
error_log('OMG API: API Key (first 20 chars): ' . substr($this->apiKey, 0, 20) . '...');
error_log('OMG API: Requesting project ' . $projectNumber);
$ch = curl_init($url);
$headers = [
'Content-Type: application/json',
'X-API-Key: ' . $this->apiKey
];
error_log('OMG API: Sending X-API-Key header');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => $this->timeout,
CURLOPT_HTTPHEADER => $headers
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$curlError = curl_error($ch);
curl_close($ch);
error_log('OMG API: Response code ' . $httpCode);
error_log('OMG API: Response body (first 500 chars): ' . substr($response, 0, 500));
error_log('===== OMG API CALL END =====');
if ($curlError) {
return [
'success' => false,
'error' => 'API request failed',
'details' => $curlError,
'httpCode' => 0
];
}
error_log('OMG API: Response code ' . $httpCode);
if ($httpCode === 404) {
return [
'success' => false,
'error' => 'Campaign not found in OMG',
'details' => "Campaign number '$projectNumber' does not exist in OMG system",
'httpCode' => 404,
'action' => 'Verify the campaign number is correct and exists in OMG'
];
}
if ($httpCode !== 200) {
return [
'success' => false,
'error' => 'OMG API returned error',
'details' => 'HTTP ' . $httpCode . ': ' . $response,
'httpCode' => $httpCode,
'action' => 'Check API credentials and OMG system status'
];
}
$data = json_decode($response, true);
if (!$data) {
return [
'success' => false,
'error' => 'Invalid JSON response from OMG',
'details' => 'Unable to parse API response',
'httpCode' => $httpCode,
'rawResponse' => substr($response, 0, 500)
];
}
error_log('OMG API: Success - ' . json_encode($data));
return [
'success' => true,
'data' => $data,
'httpCode' => $httpCode
];
} catch (Exception $e) {
error_log('OMG API Exception: ' . $e->getMessage());
return [
'success' => false,
'error' => 'Exception during OMG API call',
'details' => $e->getMessage(),
'httpCode' => 0
];
}
}
/**
* Extract business area from project data
*/
public function getBusinessArea($projectData) {
// Check for _planning_data structure (from Make.com blueprint)
if (isset($projectData['_planning_data']['business_area'])) {
return $projectData['_planning_data']['business_area'];
}
// Check for direct business_area field
if (isset($projectData['business_area'])) {
return $projectData['business_area'];
}
// Check alternative field names
if (isset($projectData['businessArea'])) {
return $projectData['businessArea'];
}
if (isset($projectData['division'])) {
return $projectData['division'];
}
return null;
}
/**
* Map business area to business unit code
*/
public function mapBusinessUnit($businessArea) {
$appConfig = require __DIR__ . '/config.php';
$businessUnitMap = $appConfig['global_to_local']['business_unit_map'];
if (empty($businessArea)) {
return [
'success' => false,
'error' => 'Business area is empty',
'mapped' => 'ERROR'
];
}
$businessAreaUpper = strtoupper(trim($businessArea));
// Try exact match first
if (isset($businessUnitMap[$businessArea])) {
return [
'success' => true,
'businessUnit' => $businessUnitMap[$businessArea],
'originalValue' => $businessArea
];
}
// Try case-insensitive match
foreach ($businessUnitMap as $pattern => $output) {
if (strtoupper($pattern) === $businessAreaUpper) {
return [
'success' => true,
'businessUnit' => $output,
'originalValue' => $businessArea
];
}
}
// Try regex matching (for patterns like "Vichy Test")
foreach ($businessUnitMap as $pattern => $output) {
if (preg_match('/' . preg_quote($pattern, '/') . '/i', $businessArea)) {
return [
'success' => true,
'businessUnit' => $output,
'originalValue' => $businessArea,
'matchType' => 'regex'
];
}
}
// No match found
return [
'success' => false,
'error' => 'Business unit not recognized',
'details' => "Business area '$businessArea' does not match any known business units",
'businessUnit' => 'ERROR',
'originalValue' => $businessArea,
'action' => 'Contact administrator to add mapping for this business unit'
];
}
}