file_get_contents php和json_decode错误

时间:2013-06-16 17:39:55

标签: php utf-8 file-get-contents json

我面临一个奇怪的问题 file_get_contents 并解码为json结果。我必须告诉你,这段代码在其他服务器上完全正常运行。

我正在尝试解码这个json: http://mediarupt.com/GET/categorized_services_for_supplier.php?suppliersID=10

进入此页面: http://mediarupt.com/kostas.php

kostas.php的代码是:

<?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;

?>

classifiedized_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);
?>

我收到错误 4 作为最新的json错误,这意味着我有 JSON_ERROR_SYNTAX 。 但我用http://jsonlint.com/验证了json结果,似乎都没问题。

此外,两个页面都是UTF-8,禁用了BOM。 您可以在此处访问php configurarion:http://mediarupt.com/phpinfo.php

我希望找到解决方案......

提前谢谢

2 个答案:

答案 0 :(得分:0)

尝试在http://jsonlint.com/中提供JSON URL,然后复制回复并将其粘贴到已禁用BOM的文件中,您会注意到之前有一个奇怪的小行导致问题的开头{括号

答案 1 :(得分:0)

json无效。

在第一个开口花括号前面有以下内容(十六进制编码)ef bb bf ef bb bf

这是utf8 bom。

有一个问题与处理相关,所以我不会在这里介绍它。在这里阅读:

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

另一个专门与json打交道:

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