Foreach with this type of JSON (PHP)

1

I'm trying to display the values of a JSON with PHP most unsuccessfully. Used Code:

JSON:

{
    "event_name": "offline_message",
    "widget_id": "sEcXk3TXEw",
    "visitor": {
        "name": "ARTULITO BARBOSA DE SOUSA",
        "email": "[email protected]",
        "phone": "11-952896992",
        "number": 15070,
        "chats_count": 1
    },
    "offline_message_id": 767,
    "message": "<Message text is not displayed here>",
    "session": {
        "geoip": {
            "region_code": "27",
            "country_code": "BR",
            "country": "Brazil",
            "region": "Sao Paulo",
            "city": "São Paulo",
            "isp": "",
            "latitude": "-23.4733",
            "longitude": "-46.6658",
            "organization": "NET Virtua"
        },
        "utm": null,
        "ip_addr": "201.6.231.89",
        "user_agent": "Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/58.0.3029.110 Safari\/537.36"
    }
}

I need to get only field data:

  • event_name

  • visitor-> name

  • visitor-> email

  • visitor-> phone

  • session-> geoip-> region

  • session-> geoip-> city

  • session-> geoip-> latitude

  • session- > geoip- > longitude

I was trying something in PHP like this

<?php

$url = 'https://admin.jivosite.com/widgets/webhooks-log/721441/1';
$content = file_get_contents($url);
$json = json_decode($content, TRUE);

foreach($json['visitor'] as $item) {
    print $item['name'][0];

}

?>

The link to the entire list I'm trying to read: link

    
asked by anonymous 24.05.2017 / 15:50

1 answer

1

json_decode in this case will only return associative arrays because every json is key { There is no json in the bracket [. In this case you will not find an element with index 0 on any json node.

event_name vai ser $json['event_name']
visitor->name vai ser $json['visitor']['name ']
...
session->geoip->region vai ser $json['session']['geoip']['region']
...
    
24.05.2017 / 16:02