file_get_contents php和json_decode错误 [英] file_get_contents php and json_decode error

查看:190
本文介绍了file_get_contents php和json_decode错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到 file_get_contents 的怪异问题,并解码为json结果.我必须告诉您,此代码在其他服务器上已完全可用.

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.

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

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

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

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;

?>

categorized_services_for_supplier.php的代码:

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

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

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.

此外,两个页面均为禁用了BOM的UTF-8. 您可以在此处访问php配置: http://mediarupt.com/phpinfo.php

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...

提前谢谢

推荐答案

json完全无效.

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

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

这是utf8 bom.

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:

如何在PHP字符串中删除%EF%BB%BF a>

和另一个专门处理json的地方:

and another one dealing specifically with json here:

转换UTF- 8,物料清单转换为UTF-8,Python中没有物料清单

这篇关于file_get_contents php和json_decode错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆