34225-vm/includes/pexels.php
Flatlogic Bot b4861d9d63 cats
2025-09-19 10:44:45 +00:00

56 lines
1.4 KiB
PHP

<?php
function pexels_key() {
$k = getenv('PEXELS_KEY');
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 => 15,
]);
$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);
}
return null;
}
function download_to($srcUrl, $destPath) {
if (!is_dir(dirname($destPath))) {
if (!mkdir(dirname($destPath), 0775, true)) {
return false;
}
}
$ch = curl_init($srcUrl);
$fp = fopen($destPath, 'wb');
if (!$fp) {
curl_close($ch);
return false;
}
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
$result = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
fclose($fp);
if ($result && $http_code >= 200 && $http_code < 300) {
return $destPath;
}
if (file_exists($destPath)) {
unlink($destPath);
}
return false;
}