forked from SimplesIP/pabx-app
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
66 lines
1.6 KiB
66 lines
1.6 KiB
<?php |
|
|
|
// Inicializar uma requisição http // |
|
$key = 'IvDU6Sl6AosNFQ28APSSotJkUOnn53XahGeC3z93j7XCC5VwlcFLaopX'; |
|
$endPoint = 'https://api.pexels.com/v1/'; |
|
$type = 'search'; |
|
|
|
$url = montarUrl($endPoint, $type); |
|
// Monta opções do cURL |
|
$options = array( |
|
CURLOPT_URL => $url, |
|
CURLOPT_HEADER => false, |
|
CURLOPT_RETURNTRANSFER => true, |
|
CURLOPT_HTTPHEADER => array( |
|
'Authorization: ' . $key, |
|
), |
|
); |
|
// Iniciar cURL |
|
$ch = curl_init(); |
|
curl_setopt_array($ch, $options); |
|
$resposta = curl_exec($ch); |
|
|
|
// Verificar se houve erros na execução do cURL |
|
if (curl_errno($ch)) { |
|
echo 'Erro cURL: ' . curl_error($ch); |
|
} else { |
|
// Decodificar a resposta JSON |
|
$resp = json_decode($resposta, true); |
|
// Verificar se houve erros na decodificação JSON |
|
if (json_last_error() !== JSON_ERROR_NONE) { |
|
$erroJson = json_last_error_msg(); |
|
echo "<script>console.log('Erro ao receber imagem da API: $erroJson');</script>"; |
|
} else { |
|
$imagem = getBanner($resp); |
|
} |
|
} |
|
|
|
// Fechar a sessão cURL |
|
curl_close($ch); |
|
|
|
// Funções utilizadas // |
|
function getBanner($data) |
|
{ |
|
$imagem_aleatoria = random_int(0, 15); |
|
$urlImagem = $data['photos'][$imagem_aleatoria]['src']['original']; |
|
if (!$urlImagem) { |
|
echo "<script>console.error('Erro ao receber imagem!');</script>"; |
|
$urlImagem = "img/imageNotFound.jpg"; |
|
} |
|
return $urlImagem; |
|
} |
|
|
|
function montarUrl($endPoint, $type) |
|
{ |
|
$endPoint .= $type; |
|
$params = array( |
|
'query' => 'call center', |
|
'orientation' => 'landscape', |
|
'size' => 'small', |
|
); |
|
|
|
$url = $endPoint . '?' . http_build_query($params); |
|
return $url; |
|
} |
|
|
|
?>
|