gpt4 book ai didi

php 无法读取 uft8 json

转载 作者:行者123 更新时间:2023-11-29 19:12:25 24 4
gpt4 key购买 nike

我现在正在编写 Android 应用程序,这些应用程序将使用 json(utf8):

http://.....xxx.php?json=%7B%22name%22%3A%22peterchan%22%2C%22phone%22%3A%2212345678%22%2C%22password% 22%3A%226kxhSJM6iLB0kZ9LZGCEUQ%3S%3D%0A%22%7D

xxx.php 是这样的:

<?php
header("Content-Type:text/html; charset=utf-8");
ini_set('default_charset', 'utf-8');
$json = $_GET["json"];
$obj = json_decode($json);
$name = $obj -> {"name"};
$phone = $obj -> {"phone"};
$password = $obj -> {"password"};
printf($json);
?>

但它返回:警告:printf():xxx.php 中的参数太少

谁能帮帮我?请

最佳答案

你应该验证 json_decode 不返回 null 并检查错误如果是这样使用 json_last_error_msg

您的密码以换行符结尾(也许它不应该在那里),它是 percent-encoded as %0A 这是无效的,因为 in JSON newlines must be escaped as \n 将被百分比编码为 %5Cn

演示:https://3v4l.org/vTM2r

$broke = '%7B%22name%22%3A%22peterchan%22%2C%22phone%22%3A%2212345678%22%2C%22password%22%3A%226kxhSJM6iLB0kZ9LZGCEUQ%3S%3D%0A%22%7D';
$fixed = '%7B%22name%22%3A%22peterchan%22%2C%22phone%22%3A%2212345678%22%2C%22password%22%3A%226kxhSJM6iLB0kZ9LZGCEUQ%3S%3D%5Cn%22%7D';

var_dump(urldecode($broke));
// string(80) "{"name":"peterchan","phone":"12345678","password":"6kxhSJM6iLB0kZ9LZGCEUQ%3S=
// "}"

var_dump(json_decode(urldecode($broke)));
// NULL

var_dump(urldecode($fixed));
// string(81) "{"name":"peterchan","phone":"12345678","password":"6kxhSJM6iLB0kZ9LZGCEUQ%3S=\n"}"

var_dump(json_decode(urldecode($fixed)));
// object(stdClass)#1 (3) {
// ["name"]=>
// string(9) "peterchan"
// ["phone"]=>
// string(8) "12345678"
// ["password"]=>
// string(27) "6kxhSJM6iLB0kZ9LZGCEUQ%3S=
// "
// }

关于php 无法读取 uft8 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44862741/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com