1

I try to put a JSON Object into a PHP Variable, it is not working. When I var_dump it, it says NULL.

PHP:

<?php
$json = utf8_encode(file_get_contents('http://socialclub.rockstargames.com/ajax/stat/1/profile_body/sta1/drantifat'));
$data = json_decode($json, false);
$money = $data->tplc->stats; // -> What do i have to do to reach the id:GTA Online Cash and use the val: 33ドル.9K ?
?>

JSON file example: (it changes over time)

{"cid":0,"ttl":"Grand Theft Auto V",
"ishs":false,"issa":false,"istc":false,"htl":true,"tlc":"text",
"tlt":"View Stats","tlh":"/member/drantifat/games/gtav/career/overview",
"iso":false,"cmng":false,"order":0,"uid":0,"ttc":0,
"tplc":"{\"game\":\"GTAV\",\"stats\":[{\"id\":\"Game Progress\",\"val\":\"58.77%\"},
{\"id\":\"Missions Passed\",\"val\":\"55\"},{\"id\":\"Playing Time\",\"val\":\"29:00:33\"},
{\"id\":\"GTA Online RP\",\"val\":\"2.4M\"},{\"id\":\"GTA Online Rank\",\"val\":\"128\"},
{\"id\":\"GTA Online Cash\",\"val\":\"33ドル.9K\"},
{\"id\":\"GTA Online Playing Time\",\"val\":\"529:44:12\"}],
\"url\":\"/member/drantifat/games/gtav/career/overview\"}","isa":false,"cnt":""}

Could anyone also help me with what I've mentioned in the PHP file?

halfer
20.2k20 gold badges111 silver badges208 bronze badges
asked Sep 12, 2014 at 22:40
3
  • 3
    As you can see in the output, $data->tplc is another string containing JSON. Commented Sep 12, 2014 at 22:46
  • 1
    Wish I could test this, that URL returns no data for me Commented Sep 12, 2014 at 22:50
  • @wavemode its true, i see that now. Im glad you've said it, as i see you have to be logged into Rockstar. I think my 'webhost' isnt logged into Rockstar lets say that. Otherwise it would work? I'll test it on localhost and will response once again. Commented Sep 12, 2014 at 22:57

1 Answer 1

1

tplc isn't a JSON object, it's a string that is itself JSON.

<?php
$json = utf8_encode(file_get_contents('http://socialclub.rockstargames.com/ajax/stat/1/profile_body/sta1/drantifat'));
$data = json_decode($json, false);
$tplc = json_decode($data->tplc);
$money = $tplc->stats;
?>

To get the money value specifically:

<?php
$json = utf8_encode(file_get_contents('http://socialclub.rockstargames.com/ajax/stat/1/profile_body/sta1/drantifat'));
$data = json_decode($json, false);
$tplc = json_decode($data->tplc);
$stats = $tplc->stats;
foreach ($tplc->stats as $stat) {
 if ($stat->id == 'GTA Online Cash') {
 $money = $stat->val;
 break;
 }
}
echo $money;
?>
answered Sep 12, 2014 at 22:46
Sign up to request clarification or add additional context in comments.

2 Comments

Still NULL ? Maybe i have to access an object with a value first. How do i access the money part, because as i see it is an array. It has multiple 'items' lets say.
@Αράπης Make sure the URL is actually returning data (e.g. echo $json;). For me it just returns an empty string. I added how to access the money value specifically.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.