Problem Doing JSON to a Site

Asked

Viewed 202 times

6

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

Link: https://opskins.com/pricelist/730.json

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

I intend to do with PHP.

  • Could you improve the question? What do you effectively need? In the link you have passed, the result already comes in Json.

1 answer

8


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.

  • It gave me the following error: Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 42 bytes) in C: Program Files (x86) Vertrigoserv www sick.php on line 4

  • I used the following code: ini_set('memory_limit', '-1');

  • By the way, can you give an example in case I want to get the prices of the items per day, without having to name the item? In other words, I put only the day, and I get the name of the items and their price.

  • Function, is error in Function: Parse error: syntax error, Unexpected ':', expecting '{' in C: Program Files (x86) Vertrigoserv www more.php on line 20

  • What is your version of PHP? If it is smaller than PHP 7, remove : bool, I’ll edit.

  • Now you are giving the following error: Catchable fatal error: Argument 1 passed to getValorItem() must be an instance of string, string Given

  • Test this now.

  • It still doesn’t work.

  • I added that I talked about taking all the data, try turning off the CURLOPT_SSL_VERIFYPEER, as it is now, this leave unsafe.

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.