file_get_contents php and json_decode error

孤者浪人 提交于 2019-12-02 02:22:23

问题


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


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/17135996/file-get-contents-php-and-json-decode-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!