1

I am facing a weird problem with file_get_contents and decoding to json the result. I have to tell you that this code was fully functional in other server.

I am trying to decode this json: http://mediarupt.com/GET/categorized_services_for_supplier.php?suppliersID=10

into this page: http://mediarupt.com/kostas.php

The code for kostas.php is this:

<?php 
$servicesJSON = file_get_contents("http://mediarupt.com/GET/categorized_services_for_supplier.php?suppliersID=10");
$ForeignKeys = (object)array();
$ForeignKeys->ServicesList = json_decode($servicesJSON, true);

echo "error: " . json_last_error();

?>

<select name="servicesID" id="servicesID">
    <option id="serviceID_0" value="0" rel="0" style="font-style:italic;">Select a category...</option>
<?php foreach($ForeignKeys->ServicesList['categorized_services'] as $value){ ?>
    <option id="serviceID_<?=$value['servicesID']?>" value="<?=$value['servicesID']?>" rel="<?=$value['hasStoresList']?>"><?=$value['serviceName']?></option>   

<?php } ?>

</select>
<?php

echo '<br><br>The result of file_get_contents ($servicesJSON): '.$servicesJSON;

?>

Code for categorized_services_for_supplier.php:

<?php header('Content-Type: application/json');
    require('../settings/dbli.php');

    $table = array();
    $suppliersID = "0";

    if(isset($_GET['suppliersID']) && $_GET['suppliersID']!=NULL && $_GET['suppliersID']!='' ){  $suppliersID = $SQLConn->real_escape_string($_GET['suppliersID']); }
    $query = "SELECT DISTINCT Services.servicesID, serviceName, servicePrice, hasStoresList FROM Services, ServicesList WHERE Services.servicesID = ServicesList.servicesID AND suppliersID = '$suppliersID' AND  status = '1' AND deleted = '0'";

    $result = $SQLConn->query($query);  
     while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
        $tempArray = array();
        foreach($row as $key => $value){
            $tempArray[$key] = $value;                      
        }
        array_push($table,$tempArray);
     }
     $arr = array( "categorized_services" => $table);
    echo json_encode($arr);
?>

I receive error 4 as latest json error, which means that I have JSON_ERROR_SYNTAX. But I validate the json result with http://jsonlint.com/ and all seems to be ok.

Also, both pages are UTF-8 with BOM disabled. You can access php configurarion here: http://mediarupt.com/phpinfo.php

I hope to find the solution...

Thank you in advance

kokazani
  • 213
  • 2
  • 13
  • You have done a test output of `echo $servicesJSON;` and it contains the JSON code? – Pekka Jun 16 '13 at 17:42
  • 1
    yes of course, I just changed my code to see the result of echo $servicesJSON – kokazani Jun 16 '13 at 17:43
  • You say you've validated with jsonlint.com, but pasting your url there and validating indicates the json isn't valid. – Crisp Jun 16 '13 at 17:52
  • yes indeed, but just copy and paste the pure string (the result of categorized_services_for_supplier.php)...You will see that there is no error there.. – kokazani Jun 16 '13 at 17:53
  • Copy pasting the string obviously removes whatever character is causing the string to be invalid in the response, the response is still invalid. – Crisp Jun 16 '13 at 17:55
  • The weird part here is that exactly the same pages on other server run with no problem... – kokazani Jun 16 '13 at 17:56
  • Total shot in the dark: try `$servicesJSON = trim($servicesJSON)` before decoding – Pekka Jun 16 '13 at 17:59
  • Still dark :P @Pekka웃 – kokazani Jun 16 '13 at 18:04
  • I copy at the body of question the code of categorized_services_for_supplier.php page...please check it if you can see any problem there...Thank you again – kokazani Jun 16 '13 at 18:09

2 Answers2

0

The json is simply invalid.

In front of the first opening curly braces there is the following content (hex encoded) ef bb bf ef bb bf

It is the utf8 bom.

There is a question related to deal with exactly that, so I will not cover it here. Read up here:

How to remove %EF%BB%BF in a PHP string

and another one dealing specifically with json here:

Convert UTF-8 with BOM to UTF-8 with no BOM in Python

Community
  • 1
  • 1
The Surrican
  • 29,118
  • 24
  • 122
  • 168
-1

Try to give JSON URL in the http://jsonlint.com/ and than copy the response and paste it in a file with BOM disabled you will notice that there is a strange small line in the beginning before the opening { bracket which is causing the issues

Khawer Zeshan
  • 9,470
  • 6
  • 40
  • 63
  • You need to find out why is that small strange line coming and you need to remove it. Hope that helps – Khawer Zeshan Jun 16 '13 at 18:04
  • I have noticed that indeed, I copy the code of the page at the body of my question... I ll try to rewrite the script from scratch in new page – kokazani Jun 16 '13 at 18:11