I can not capture data from JSON!

1

Well, I have json here, and I used CURL to get it!

Then I gave json_decode , but, I tried everything to get the data, but nothing works. What could be happening?

I tried to pick up only the items part of JSON , and it works, get all the data etc! But when I try to get only 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 bit 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
    
asked by anonymous 29.10.2016 / 05:01

1 answer

2

Do the following:

  • By default cURL will verify that the server certificate is valid, if the verification fails, the connection will fail, to disable CURLOPT_SSL_VERIFYPEER for false :

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    
  • $info = json_decode($json, true) : When the second parameter is indicated as true , the returned object will be converted to% associative%. To get array , make name and not $info['items'][0]['name'] .

    See DEMO

29.10.2016 / 14:03