You must use the index of AWP | Dragon Lore (Field-Tested)
, containing a -
, if you use
(space) will not find, basically will have to do:
$json['AWP | Dragon Lore (Field-Tested)']['2017-03-25']['price'];
If you want less code do it, cacheless:
if($resposta = file_get_contents('https://opskins.com/pricelist/730.json')){
$json = json_decode($resposta, true);
echo $json['AWP | Dragon Lore (Field-Tested)']['2017-03-25']['price'];
}
If you want with CURL, cacheless:
$curl = curl_init('https://opskins.com/pricelist/730.json');
curl_setopt_array($curl, [
CURLOPT_SSL_VERIFYHOST => 2,
CURLOPT_SSL_VERIFYPEER => 1,
CURLOPT_PROTOCOLS => CURLPROTO_HTTPS,
CURLOPT_RETURNTRANSFER =>1,
CURLOPT_FAILONERROR => 1
]);
if($resposta = json_decode(curl_exec($curl), true)){
echo $resposta['AWP | Dragon Lore (Field-Tested)']['2017-03-25']['price'];
}
You can use a cache system, so preventing having to download all this content at all times, this can save server resources, for example:
echo getValorDataPorNome('AWP | Dragon Lore (Field-Tested)', '2017-03-25');
Return:
86500
Taking all the data:
var_dump(getValorDataTodos('2017-03-25'));
Return:
array(8966) {
["AK-47 | Aquamarine Revenge (Battle-Scarred)"]=>
int(1004)
["AK-47 | Aquamarine Revenge (Factory New)"]=>
int(2829)
["AK-47 | Aquamarine Revenge (Field-Tested)"]=>
int(1452)
["AK-47 | Aquamarine Revenge (Minimal Wear)"]=>
int(1989)
["AK-47 | Aquamarine Revenge (Well-Worn)"]=>
int(1228)
["AK-47 | Black Laminate (Battle-Scarred)"]=>
int(828)
["AK-47 | Black Laminate (Factory New)"]=>
int(8476)
["AK-47 | Black Laminate (Field-Tested)"]=>
int(758)
["AK-47 | Black Laminate (Minimal Wear)"]=>
int(902)
["AK-47 | Black Laminate (Well-Worn)"]=>
int(827)
//...
Using:
const CACHE_ARQUIVO = 'json.json';
const CACHE_TEMPO = 120;
function getValorDataPorNome($nome, $data, $json = false){
$json = $json === false ? getJSON() : $json;
if(isset($json[$nome][$data]['price'])){
return $json[$nome][$data]['price'];
}
return getValorRecentePorNome($nome, $json);
}
function getValorRecentePorNome($nome, $json = false){
$json = $json === false ? getJSON() : $json;
if(isset($json[$nome])) {
$ultimaData = array_reverse(array_keys($json[$nome]))[0];
return $json[$nome][$ultimaData]['price'];
}
return false;
}
function getValorDataTodos($data, $json = false){
$json = $json === false ? getJSON() : $json;
foreach($json as $nome => $item){
$json[$nome] = getValorDataPorNome($nome, $data, $json);
}
return $json;
}
function getJSON(){
if(existeArquivoRecente() === false && executaCurl() === false){
die;
}
return json_decode(file_get_contents(CACHE_ARQUIVO), true);
}
function existeArquivoRecente(){
clearstatcache();
return file_exists(CACHE_ARQUIVO) && filemtime(CACHE_ARQUIVO) > time() - CACHE_TEMPO;
}
function executaCurl(){
$escreverArquivo = fopen(CACHE_ARQUIVO, "w");
$curl = curl_init('https://opskins.com/pricelist/730.json');
curl_setopt_array($curl, [
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_PROTOCOLS => CURLPROTO_HTTPS,
CURLOPT_FILE => $escreverArquivo,
CURLOPT_FAILONERROR => 1
]);
if(!curl_exec($curl)){
return false;
}else{
curl_close($curl);
fclose($escreverArquivo);
return true;
}
}
It does not return any Exception or store details in logs, this was not added to try to be more simplistic. He will silently get the last price, if there is no record of that date.
This will keep a cached file for 120 seconds, set by CACHE_TEMPO
, it will get the price (or return false
, if there is any error).
Function:
getValorDataPorNome(string $nome, string $data, [array $json]) : string
:
Gets the price of the item at the informed date, if any. If there is no available price at the informed date it will fall to getValorRecentePorNome
.
getValorRecentePorNome(string $nome, [array $json]) : string
:
Gets the price latest item reported.
getValorDataTodos(string $data, [array $json]) : array
:
Get all the prices of all available items, based on the informed date, if any, if there is no fixed price on the specified date you will get the most recent price.
This has been tested in PHP 7 and PHP 7.1, older versions may have incompatibility, although I don’t see any overt.
Could you improve the question? What do you effectively need? In the link you have passed, the result already comes in Json.
– eduardosouza