When I am trying to echo the JSON objects from the array it is not working. In the error logs it gives me this:
PHP Notice: Trying to get property of non-object in /home/zadmin/test.britishweb.co.uk/patchwork/featuredseries.php on line 48
For every one of the objects I'm trying to echo. Here is my code:
<?php
$source = "http://prod.cloud.rockstargames.com/global/SC/events/eventschedule-game-en.json"; // Source URL will be unchanged most likely but placed in a variable just in case.
$ch = curl_init(); // Connect
curl_setopt($ch, CURLOPT_URL, $source);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec ($ch);
curl_close ($ch);
$destination = "eventschedule-game-en.json";
$file = fopen($destination, "w+");
file_put_contents($destination, $data);
fclose($file);
$json = file_get_contents($destination);
$obj = json_decode($json, true);
var_dump($obj); // Debug option
?>
Var dump of $obj:
{
"multiplayerEvents": [
{
"posixStartTime": 1498687200,
"posixEndTime": 1499720340,
"eventRosGameTypes": [
"gta5"
],
"eventPlatformTypes": [
"pcros",
"xboxone",
"ps4"
],
"displayName": "2x$ and RP Dawn Raid Adversary Mode",
"eventId": 20417,
"extraData": {
"eventType": "FeaturedJob"
}
}
]
}
and then I am doing the HTML as such:
<div class="main_event">
<p id="name"><?php echo $obj["displayName"]; ?> Now playing on GTA V.</p>
<h2>Selected Platforms</h2>
<p id="platforms">The series is currently running on the following platforms:</p>
<ul>
<li><?php echo $obj->eventPlatformTypes[0]; ?></li>
<li><?php echo $obj->eventPlatformTypes[1]; ?></li>
<li><?php echo $obj->eventPlatformTypes[2]; ?></li>
</ul>
</div>
var_dump($obj);?file_put_contents()does not require you opening the file seperately$obj->eventPlatformTypeswhenobjis an array