-2

Total beginner with PHP, here.

I want to make a small website that is going to parse data from a JSON web page and put it on my website but for some reason, no data (the value of "length") is being returned PHP code:

    <?
    $jsonData = file_get_contents('web site link');
    $data = json_decode($jsonData, TRUE);

    foreach($data as $stats )
    {
        echo $stats->length;
    }

//  var_dump($jsonData);
?>

Json data (full data: http://logs.tf/json/2652721#76561198044689160):

    "length": 1674,
"players": {
    "[U:1:131277951]": {
        "team": "Blue",
        "class_stats": [{
            "type": "soldier",
            "kills": 16,
            "assists": 2,
            "deaths": 21,
            "dmg": 7505,
            "weapon": {
                "tf_projectile_rocket": {
                    "kills": 16,
                    "dmg": 7505,
                    "avg_dmg": 58.6328125,
                    "shots": 0,
                    "hits": 0
                }
            },
            "total_time": 1674
        }],

What am I doing wrong? :(

GeoV
  • 1
  • 4

2 Answers2

0

At first look, your json looks invalid. check here, https://jsonlint.com/ it should be like this.

{
    "length": 1674,
    "players": {
        "[U:1:131277951]": {
            "team": "Blue",
            "class_stats": [{
                "type": "soldier",
                "kills": 16,
                "assists": 2,
                "deaths": 21,
                "dmg": 7505,
                "weapon": {
                    "tf_projectile_rocket": {
                        "kills": 16,
                        "dmg": 7505,
                        "avg_dmg": 58.6328125,
                        "shots": 0,
                        "hits": 0
                    }
                },
                "total_time": 1674
            }]
        }
    }
}

Second i tried your code with sample json from https://jsonplaceholder.typicode.com/posts and it works.

<?php
  
   $jsonData = file_get_contents(
        "https://jsonplaceholder.typicode.com/posts");
    $data = json_decode($jsonData, TRUE);
        
    foreach($data as $stats )
    {
        print_r( $stats );
    }
Jitesh Dhamaniya
  • 1,326
  • 2
  • 8
  • 17
  • Here is the original json page http://logs.tf/json/2652721#76561198044689160 and I believe it 'looks' alright – GeoV Aug 15 '20 at 06:39
  • and it works. ` $jsonData = file_get_contents( "http://logs.tf/json/2652721#76561198044689160"); $data = json_decode($jsonData, TRUE); print_r($data); ` – Jitesh Dhamaniya Aug 15 '20 at 06:43
  • I even copy-pasted your code and there is absolutely no data output. It works for everyone else except for me and my website. – GeoV Aug 15 '20 at 06:47
  • Are you using ob_start somewhere? it stop echoing. – Jitesh Dhamaniya Aug 15 '20 at 06:53
0

Refer to the documentation json_decode($jsonData, TRUE); return array, if you want to retrive these values as object, delet the seconde parameter. try json_decode($jsonData);

<?
$jsonData = file_get_contents('web site link');
$data = json_decode($jsonData);

foreach($data as $stats ){
    echo $stats->length;
}

//  var_dump($jsonData);
?>
GNassro
  • 891
  • 1
  • 10
  • 23