我正在尝试这段代码
$json = file_get_contents("http://www.google.com/alerts/preview?q=test&t=7&f=1&l=0&e");
print_r(json_decode(utf8_encode($json), true));
//////////////
// Define the errors.
$constants = get_defined_constants(true);
$json_errors = array();
foreach ($constants["json"] as $name => $value) {
if (!strncmp($name, "JSON_ERROR_", 11)) {
$json_errors[$value] = $name;
}
}
// Show the errors for different depths.
foreach (range(4, 3, -1) as $depth) {
var_dump(json_decode($json, true, $depth));
echo 'Last error: ', $json_errors[json_last_error()], PHP_EOL, PHP_EOL;
}
Run Code Online (Sandbox Code Playgroud)
我已经尝试了很多函数,html_entities_decode,utf8_encode和解码,解码十六进制代码,但我总是得到错误"JSON_ERROR_UTF8".
我该怎么解决这个问题?
我正在使用Laravel(一个PHP框架)为移动设备编写服务并以JSON格式返回数据.在数据结果中有一些字段编码UTF-8.
以下声明
return JsonResponse::create($data);
Run Code Online (Sandbox Code Playgroud)
返回以下错误
InvalidArgumentException
HELP
Malformed UTF-8 characters, possibly incorrectly encoded
Open: /var/www/html/vendor/symfony/http-foundation/Symfony/Component/HttpFoundation/JsonResponse.php
} catch (\Exception $exception) {
restore_error_handler();
throw $exception;
}
if (JSON_ERROR_NONE !== json_last_error()) {
throw new \InvalidArgumentException($this->transformJsonError());
}
Run Code Online (Sandbox Code Playgroud)
我改变了:
return JsonResponse::create($data);
Run Code Online (Sandbox Code Playgroud)
至
return JsonResponse::create($data, 200, array('Content-Type'=>'application/json; charset=utf-8' ));
Run Code Online (Sandbox Code Playgroud)
但它仍然无法正常工作.
我该如何解决?