48 lines
1.3 KiB
PHP
48 lines
1.3 KiB
PHP
<?php
|
|
// Helper functions to interact with Pexels API
|
|
function pexels_key() {
|
|
$k = getenv('PEXELS_KEY');
|
|
// Use a default public key if not set in environment
|
|
return $k && strlen($k) > 0 ? $k : 'Vc99rnmOhHhJAbgGQoKLZtsaIVfkeownoQNbTj78VemUjKh08ZYRbf18';
|
|
}
|
|
|
|
function pexels_get($url) {
|
|
$ch = curl_init();
|
|
curl_setopt_array($ch, [
|
|
CURLOPT_URL => $url,
|
|
CURLOPT_RETURNTRANSFER => true,
|
|
CURLOPT_HTTPHEADER => [ 'Authorization: '. pexels_key() ],
|
|
CURLOPT_TIMEOUT => 20,
|
|
CURLOPT_USERAGENT => 'Flatlogic-Gemini-Agent/1.0'
|
|
]);
|
|
$resp = curl_exec($ch);
|
|
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
curl_close($ch);
|
|
if ($code >= 200 && $code < 300 && $resp) {
|
|
return json_decode($resp, true);
|
|
}
|
|
error_log("Pexels API request failed with code $code for URL: $url");
|
|
return null;
|
|
}
|
|
|
|
function download_to($srcUrl, $destPath) {
|
|
$dir = dirname($destPath);
|
|
if (!is_dir($dir)) {
|
|
if (!mkdir($dir, 0775, true)) {
|
|
error_log("Failed to create directory: $dir");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
$data = @file_get_contents($srcUrl);
|
|
if ($data === false) {
|
|
error_log("Failed to download image from: $srcUrl");
|
|
return false;
|
|
}
|
|
|
|
if (file_put_contents($destPath, $data) === false) {
|
|
error_log("Failed to save image to: $destPath");
|
|
return false;
|
|
}
|
|
return true;
|
|
} |