Issues (11)

Examples/example05.php (1 issue)

Labels
Severity
1
<?php
2
3
require_once __DIR__ . "/../vendor/autoload.php";
4
5
use Silviooosilva\CacheerPhp\Cacheer;
6
7
$options = [
8
    "cacheDir" =>  __DIR__ . "/cache",
9
];
10
11
$Cacheer = new Cacheer($options);
12
13
// URL da API e chave de cache
14
$apiUrl = 'https://jsonplaceholder.typicode.com/posts';
15
$cacheKey = 'api_response_' . md5($apiUrl);
16
17
// Verificando se a resposta da API já está no cache
18
$cachedResponse = $Cacheer->getCache($cacheKey);
19
20
if ($Cacheer->isSuccess()) {
21
    // Use a resposta do cache
22
    $response = $cachedResponse;
23
} else {
24
    // Faça a chamada à API e armazene a resposta no cache
25
    $response = file_get_contents($apiUrl);
26
    $Cacheer->putCache($cacheKey, $response);
27
}
28
29
// Usando a resposta da API (do cache ou da chamada)
30
$data = json_decode($response, true);
0 ignored issues
show
It seems like $response can also be of type Silviooosilva\CacheerPhp\Utils\CacheDataFormatter; however, parameter $json of json_decode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

30
$data = json_decode(/** @scrutinizer ignore-type */ $response, true);
Loading history...
31
print_r($data);
32