Problem making JSON to a Site

6

I need to make JSON from the following link, in order to get the data for a record AWP | Dragon Lore (Field Tested) of the day: 2017-03-25 .

Link: link

How can I make a json, in order to get price , of the following item from the following day?

I want to do with PHP.

    
asked by Gonçalo 26.03.2017 в 01:56

1 answer

8

You should use the AWP | Dragon Lore (Field-Tested) index, containing - , if you use (space) you will not find, basically you have to do:

$json['AWP | Dragon Lore (Field-Tested)']['2017-03-25']['price'];

If you want less code do no cache :

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, no cache :

$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 caching system, thus preventing you from having to download all this content at all times, this can save resources on the server, for example :

echo getValorDataPorNome('AWP | Dragon Lore (Field-Tested)', '2017-03-25');

Return:

86500

Getting 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 exceptions or store details in the logs, this has not been added to try to be more simplistic. It will silently get the last price if there is no registration from 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 are any errors).

Functions:

  • getValorDataPorNome(string $nome, string $data, [array $json]) : string :

    Gets the price of the item on the date entered, if any. If there is no price available on the informed date it will drop to getValorRecentePorNome .

  • getValorRecentePorNome(string $nome, [array $json]) : string :

    Gets the most recent price of the item entered.

  • getValorDataTodos(string $data, [array $json]) : array :

    You get all prices for all available items, based on the date entered, if any, if there is no available 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 it does not see any obvious .

    
28.03.2017 / 19:04