我在使用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;
?>
categories_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 配置:http: //mediarupt.com/phpinfo.php
我希望找到解决方案...
先感谢您