I can’t capture JSON data!

Asked

Viewed 205 times

1

Guys, I got one json here, and I used the CURL to catch him!

Then I gave a json_decode, only, I’ve tried everything to get the data, but nothing works. What may be happening?

I tried to get only the items part of the JSON, and it works, take all the data etc! But when I try to catch only the name, for example, nothing appears!

My code:

<?php 


if (!isset($_GET['nome'])) {


}else {


    $name = $_GET['nome'];

    $ch = curl_init();
    // informar URL e outras funções ao CURL
    curl_setopt($ch, CURLOPT_URL, "https://api.clashofclans.com/v1/clans?name=$name");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json', "authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiIsImtpZCI6IjI4YTMxOGY3LTAwMDAtYTFlYi03ZmExLTJjNzQzM2M2Y2NhNSJ9.eyJpc3MiOiJzdXBlcmNlbGwiLCJhdWQiOiJzdXBlcmNlbGw6Z2FtZWFwaSIsImp0aSI6IjJiMDU4Zjc1LWUwMmItNDAyNC1hNjAzLTgxODdiNGQ1ODczMiIsImlhdCI6MTQ3NjkzMjA5MSwic3ViIjoiZGV2ZWxvcGVyL2FiYjVkZWU0LWM5ODItMmJiNC01YWY1LWMzOGVhOGEyNjBkMSIsInNjb3BlcyI6WyJjbGFzaCJdLCJsaW1pdHMiOlt7InRpZXIiOiJkZXZlbG9wZXIvc2lsdmVyIiwidHlwZSI6InRocm90dGxpbmcifSx7ImNpZHJzIjpbIjE4NS4yOC4yMC4xNSJdLCJ0eXBlIjoiY2xpZW50In1dfQ.v3HR-l3UE2_oDnDobQuxmwH3HfFkR2wyW3KRRjqo0Pg6QtCqKki1po7GGbLODSMBJHgs1EkYFHyxOZun2t6koA"));
    // Acessar a URL e retornar a saída
    $json = curl_exec($ch);
    // liberar
    curl_close($ch);
    // Imprimir a saída
    $info = json_decode($json, true);

    echo ("Clãs com nome: $name");
    echo ("<br>");
    echo ($info->itens->title);
}
?>

A little piece of JSON:

{
  "items": [
    {
      "tag": "#Q8PC0JP8",
      "name": "UNIÃO LENDÁRIA",
      "type": "open",
      "location": {
        "id": 32000038,
        "name": "Brazil",
        "isCountry": true,
        "countryCode": "BR"
      },
      "badgeUrls": {
        "small": "https://api-assets.clashofclans.com/badges/70/E75PzYKp5iqzLoCBSKfmURN757zafvXZUL29KhLSFs0.png",
        "large": "https://api-assets.clashofclans.com/badges/512/E75PzYKp5iqzLoCBSKfmURN757zafvXZUL29KhLSFs0.png",
        "medium": "https://api-assets.clashofclans.com/badges/200/E75PzYKp5iqzLoCBSKfmURN757zafvXZUL29KhLSFs0.png"
      },
      "clanLevel": 1,
      "clanPoints": 414,
      "requiredTrophies": 200,
      "warFrequency": "always",
      "warWinStreak": 0,
      "warWins": 1,
      "warTies": 0,
      "warLosses": 2,
      "isWarLogPublic": true,
      "members": 1
    }, //e por aí vai

1 answer

2


Do the following:

  • By default Curl will check that the server certificate is valid, if the check fails, the connection will fail, to disable change CURLOPT_SSL_VERIFYPEER for false:

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    
  • $info = json_decode($json, true): When specifying the second parameter as true the returned object will be converted into a array associative. To catch the name, do $info['items'][0]['name'] and not $info->itens->title.

    See DEMO

  • and how I would create a while, and put all the results?

  • @Sampaioleal What do you mean? you say, put all the values (and not the keys) in one array one-dimensional?

  • well n, this json, is the result of a search, IE, comes several results within the 'items', and I want to loop to show at least 10 results, only with the 'name' for example

  • @Sampaioleal Is it something close to that? http://ideone.com/oTamjB

  • thanks worked, but you have how to filter this? let appear only 10 or 20? anything create a box with scroll

  • @Sampaioleal Yes, see this other version with the for: http://ideone.com/4VHeXQ

Show 1 more comment

Browser other questions tagged

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