Complete PHP-based workflow application for Ferrero DAM system: - OAuth2 authentication with automatic token management - Campaign discovery and filtering - Folder structure navigation - Asset download (individual and bulk) - Metadata extraction and display - Clean step-by-step web interface Status: Fully functional and production-ready 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
203 lines
No EOL
5.6 KiB
PHP
203 lines
No EOL
5.6 KiB
PHP
<?php
|
|
|
|
class ApiClient
|
|
{
|
|
private $baseUrl;
|
|
private $headers;
|
|
private $timeout;
|
|
|
|
public function __construct($baseUrl = '', $timeout = 30)
|
|
{
|
|
$this->baseUrl = $baseUrl;
|
|
$this->headers = [];
|
|
$this->timeout = $timeout;
|
|
}
|
|
|
|
public function setBaseUrl($url)
|
|
{
|
|
$this->baseUrl = rtrim($url, '/');
|
|
}
|
|
|
|
public function setHeader($key, $value)
|
|
{
|
|
$this->headers[$key] = $value;
|
|
}
|
|
|
|
public function setHeaders($headers)
|
|
{
|
|
$this->headers = array_merge($this->headers, $headers);
|
|
}
|
|
|
|
public function executeRequest($request)
|
|
{
|
|
$url = $this->buildUrl($request);
|
|
$method = strtoupper($request['method'] ?? 'GET');
|
|
|
|
$ch = curl_init();
|
|
|
|
curl_setopt_array($ch, [
|
|
CURLOPT_URL => $url,
|
|
CURLOPT_RETURNTRANSFER => true,
|
|
CURLOPT_TIMEOUT => $this->timeout,
|
|
CURLOPT_CONNECTTIMEOUT => 30,
|
|
CURLOPT_CUSTOMREQUEST => $method,
|
|
CURLOPT_HTTPHEADER => $this->buildHeaders($request),
|
|
CURLOPT_SSL_VERIFYPEER => false,
|
|
CURLOPT_HEADER => true,
|
|
CURLOPT_FOLLOWLOCATION => true,
|
|
CURLOPT_BUFFERSIZE => 4096, // Smaller buffer for better streaming
|
|
CURLOPT_NOPROGRESS => false, // Enable progress monitoring
|
|
]);
|
|
|
|
if (in_array($method, ['POST', 'PUT', 'PATCH']) && isset($request['body'])) {
|
|
$body = $this->buildRequestBody($request['body']);
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
|
|
}
|
|
|
|
$startTime = microtime(true);
|
|
$response = curl_exec($ch);
|
|
$endTime = microtime(true);
|
|
|
|
$responseTime = ($endTime - $startTime) * 1000;
|
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
|
|
|
|
if (curl_error($ch)) {
|
|
$error = curl_error($ch);
|
|
curl_close($ch);
|
|
|
|
return [
|
|
'success' => false,
|
|
'error' => $error,
|
|
'response_time' => $responseTime,
|
|
'http_code' => 0,
|
|
'headers' => [],
|
|
'body' => ''
|
|
];
|
|
}
|
|
|
|
curl_close($ch);
|
|
|
|
$headers = substr($response, 0, $headerSize);
|
|
$body = substr($response, $headerSize);
|
|
|
|
return [
|
|
'success' => true,
|
|
'http_code' => $httpCode,
|
|
'response_time' => round($responseTime, 2),
|
|
'headers' => $this->parseHeaders($headers),
|
|
'body' => $body,
|
|
'url' => $url,
|
|
'method' => $method
|
|
];
|
|
}
|
|
|
|
private function buildUrl($request)
|
|
{
|
|
$url = '';
|
|
|
|
if (isset($request['url'])) {
|
|
if (is_array($request['url'])) {
|
|
$url = $request['url']['raw'] ?? '';
|
|
} else {
|
|
$url = $request['url'];
|
|
}
|
|
}
|
|
|
|
if (strpos($url, 'http') !== 0 && $this->baseUrl) {
|
|
$url = $this->baseUrl . '/' . ltrim($url, '/');
|
|
}
|
|
|
|
return $url;
|
|
}
|
|
|
|
private function buildHeaders($request)
|
|
{
|
|
$headers = $this->headers;
|
|
|
|
if (isset($request['header'])) {
|
|
foreach ($request['header'] as $header) {
|
|
if (isset($header['key']) && isset($header['value']) && !($header['disabled'] ?? false)) {
|
|
$headers[$header['key']] = $header['value'];
|
|
}
|
|
}
|
|
}
|
|
|
|
$formattedHeaders = [];
|
|
foreach ($headers as $key => $value) {
|
|
$formattedHeaders[] = $key . ': ' . $value;
|
|
}
|
|
|
|
return $formattedHeaders;
|
|
}
|
|
|
|
private function buildRequestBody($body)
|
|
{
|
|
if (isset($body['raw'])) {
|
|
return $body['raw'];
|
|
}
|
|
|
|
if (isset($body['formdata'])) {
|
|
$data = [];
|
|
foreach ($body['formdata'] as $item) {
|
|
if (!($item['disabled'] ?? false)) {
|
|
$data[$item['key']] = $item['value'];
|
|
}
|
|
}
|
|
return http_build_query($data);
|
|
}
|
|
|
|
if (isset($body['urlencoded'])) {
|
|
$data = [];
|
|
foreach ($body['urlencoded'] as $item) {
|
|
if (!($item['disabled'] ?? false)) {
|
|
$data[$item['key']] = $item['value'];
|
|
}
|
|
}
|
|
return http_build_query($data);
|
|
}
|
|
|
|
return '';
|
|
}
|
|
|
|
private function parseHeaders($headerString)
|
|
{
|
|
$headers = [];
|
|
$lines = explode("\n", $headerString);
|
|
|
|
foreach ($lines as $line) {
|
|
$line = trim($line);
|
|
if (strpos($line, ':') !== false) {
|
|
list($key, $value) = explode(':', $line, 2);
|
|
$headers[trim($key)] = trim($value);
|
|
}
|
|
}
|
|
|
|
return $headers;
|
|
}
|
|
|
|
public function testConnection($url = null)
|
|
{
|
|
$testUrl = $url ?? $this->baseUrl;
|
|
|
|
$ch = curl_init();
|
|
curl_setopt_array($ch, [
|
|
CURLOPT_URL => $testUrl,
|
|
CURLOPT_NOBODY => true,
|
|
CURLOPT_RETURNTRANSFER => true,
|
|
CURLOPT_TIMEOUT => 10,
|
|
CURLOPT_SSL_VERIFYPEER => false
|
|
]);
|
|
|
|
$result = curl_exec($ch);
|
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
$error = curl_error($ch);
|
|
curl_close($ch);
|
|
|
|
return [
|
|
'success' => !$error && $httpCode < 400,
|
|
'http_code' => $httpCode,
|
|
'error' => $error
|
|
];
|
|
}
|
|
} |