0

example.php

{"status": "ok"} {"status": "error"}

result:

ok , error

My website is showing a blank page(I'm using this code),can you help me to fix it?

<?php
$userinfo = 'example.php';
$fgc = file_get_contents($userinfo);
$json2 = json_decode($fgc, true);
$media = $json2['status'];

$mediaId = $media;

echo $mediaId;
?>
mickmackusa
  • 43,625
  • 12
  • 83
  • 136

2 Answers2

1

example.php does not contain valid JSON

Here's probably what you want:

 [ {"status": "ok"} , {"status": "error"} ]

You can validate JOSN here: http://jsonlint.com/

You can find more on how to debug PHP "white screen of death" here: PHP's white screen of death (read the top two answers)

Community
  • 1
  • 1
Robbie
  • 17,605
  • 4
  • 35
  • 72
  • I took the results of external websites, and as a result there is no comma separating between it. – ALFIAN ANANDA PUTRA Dec 12 '16 at 03:30
  • Just because you got it from an external website does not mean it's right. It's NOT valid JSON, therefore `json_decode` WILL fail and throw an error and give you a white screen. – Robbie Dec 12 '16 at 03:56
  • 1
    @ALFIANANANDAPUTRA then contact whoever made this external website and tell him to learn the basics of what he's supposed to do, because that was **not** valid JSON. – Franz Gleichmann Dec 12 '16 at 06:59
0

This will resolve your issue in the non-recommended way:

$broken_json = '{"success": "ok"} {"success": "error"}';
$fixed_json = "[" . str_replace("} {", "},{", $broken_json) . "]";
echo $fixed_json;

$array = json_decode($fixed_json, true);

echo "<pre>";
var_dump($array);
echo "</pre>";

Result:

[{"success": "ok"},{"success": "error"}]

array(2) {
  [0]=>
  array(1) {
    ["success"]=>
    string(2) "ok"
  }
  [1]=>
  array(1) {
    ["success"]=>
    string(5) "error"
  }
}

Recommended Way:

Actually get VALID JSON from the source

zanderwar
  • 3,440
  • 3
  • 28
  • 46