How to take a JSON data in this case

Asked

Viewed 62 times

1

I’m not getting a given JSON in this case:

Use this site only for own testing

https://devjp.xyz/data.json

I tried some combinations:

            $url = "https://devjp.xyz/data.json";
            $json = file_get_contents($url);
            $json_data = json_decode($json, true);


            echo $json_data['results'][0]['bitcoin'][0]['mercadobitcoin']['buy'];   

1 answer

2


With json_decode and file_get_contents you won’t be able to because it’s blocked.

My solution:

You can use a stream that will read the file and turn it into a string for use. See:

// abro uma stream com fopen
$stream = fopen("https://devjp.xyz/data.json", 'r');
$conteudo = stream_get_contents ($stream, -1); // insiro o conteúdo em uma variável
fclose($stream); // fecho o stream

// após pegar o conteúdo eu procuro a palavra 'mercadobitcoin'
// a partir de então o restante do conteúdo será resgatado a partir daquela palavra
$mercadobitcoin = strstr($conteudo, 'mercadobitcoin');

// a mesma coisa acontece aqui
$buy = strstr($mercadobitcoin, 'buy');

// separo para deixar apenas parte que interessa '$val[0]'
$val = explode(",", $buy);

// a mesma coisa, mas agora eu separo a string buy da string 32399.99
$valor = explode(":", $val[0]);

// mostro o valor
echo $valor[1]; // 32399.99

Unfortunately you will have a job to create a function that will standardize this.

I hope it helps you.

  • Helped yes ahhaha Thank you so much!! But this is a gambiarra or has no other method?

  • @Joãopedromorais is kind of a scam. Because the site blocked the use of json_decode.

  • So the only way to use it is to treat the content as a string.

  • Parameters: format: (response format) -json -json-Cors (released for AJAX, requires key with correct domain) -php-serialize -debug (response in indented JSON, recommended for reading only)

  • In api has it, it means something?

  • @Joãopedromorais Probably yes. But I don’t know what it is. You need to see right how it configures, then it will be easier to pick up the values.

  • https://hgbrasil.com/status/finance/ am picking up from this site

  • @Joãopedromorais So I saw it needs a key to register there and put it on your site (application). There you will probably have easier access.

  • What is this json_decode lock? is something in the form of response or blocking by the server?

  • @Joãopedromorais both. The server blocks and the content you pick up comes with this lock.

  • @Joãopedromorais take a look. https://answall.com/questions/199199/como-prote-minha-aplica%C3%A7%C3%A3o-json

  • I created a key there for me, now as I know it’s releasing json_decode

  • I did!!! But I learned a lot about json from vc vlw msm! $json_data['Results']['bitcoin']['mercadobitcoin']['buy'];

  • it was just release the key there, I didn’t know there was this json lock Decode

  • @Joãopedromorais exact. I imagined that was it.

  • @Joãopedromorais quiet friend! Good luck! Hug!

Show 11 more comments

Browser other questions tagged

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