|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
// Inicializar uma requisi<EFBFBD><EFBFBD>o http //
|
|
|
|
|
$key = 'IvDU6Sl6AosNFQ28APSSotJkUOnn53XahGeC3z93j7XCC5VwlcFLaopX';
|
|
|
|
|
$endPoint = 'https://api.pexels.com/v1/';
|
|
|
|
|
$type = 'search';
|
|
|
|
|
|
|
|
|
|
$url = montarUrl($endPoint, $type);
|
|
|
|
|
// Monta op<EFBFBD><EFBFBD>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<EFBFBD><EFBFBD>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<EFBFBD><EFBFBD>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<EFBFBD>o cURL
|
|
|
|
|
curl_close($ch);
|
|
|
|
|
|
|
|
|
|
// Fun<EFBFBD><EFBFBD>es utilizadas //
|
|
|
|
|
function getBanner($data)
|
|
|
|
|
{
|
|
|
|
|
$imagem_aleatoria = random_int(0, 15);
|
|
|
|
|
$urlImagem = $data['photos'][$imagem_aleatoria]['src']['original'];
|
|
|
|
|
if (!$urlImagem) {
|
|
|
|
|
throw new Exception('Erro ao adquirir imagem');
|
|
|
|
|
}
|
|
|
|
|
return $urlImagem;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function montarUrl($endPoint, $type)
|
|
|
|
|
{
|
|
|
|
|
$endPoint .= $type;
|
|
|
|
|
$params = array(
|
|
|
|
|
'query' => 'phone call',
|
|
|
|
|
'orientation' => 'landscape',
|
|
|
|
|
'size' => 'small',
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$url = $endPoint . '?' . http_build_query($params);
|
|
|
|
|
return $url;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
?>
|