我正在向我的PHP页面发送JSON查询。
因为我在服务器上遇到了JSON部分的问题(我是JSON的新手)。
我决定让服务器把它发回去,这样我才能看到它。
这是我发送给它的JSON文本,因此希望它发回给我(是的,它包含虚拟数据):
{"propertyType":"Detached","price":"edtPrice","bedrooms":"2","bathrooms":"2","squareFeet":"1200","address":"edtAddr","city":"edtCity","postal":"edtPostal","agentPhone":"edtAgent","agentEMail":"edtEMail","agentName":"edtAgent","agentCompany":"edtCompany","Pictures":[]}
我已经确认这是发送到服务器的内容,并且在上传的数据中没有#0。
以下是接收它并将其发送回客户端的代码:
<?
if (file_get_contents('php://input')) {
$json = file_get_contents('php://input');
echo $json;
$parsedJSON = json_decode($json);
// (error here) echo $parsedJSON->city;
}
?>
这是Delphi(XE3)代码,它接收POST到服务器的回复:
bRet:= HttpSendRequest(hRequest,
nil,
0,
PChar(sData),
length(sData));
// choosing Unicode here as the format
ss:=TStringStream.Create('', TEncoding.Unicode);
try
repeat
if not InternetReadFile(hRequest, @caBuffer, SizeOf(caBuffer), iBytesRead) then
RaiseLastOSError;
ss.WriteBuffer(caBuffer, iBytesRead);
until iBytesRead = 0;
Result:=ss.DataString;
finally
ss.Free;
end;
从服务器接收回来后,看起来编码是不明确的。我不会惊讶地看到unicode编码从服务器返回,但我不知道如何处理这个文本......
caBuffer包含:
(10, 123, 0, 34, 0, 112, 0, 114, 0, 105, 0, 99, 0, 101, 0, 34, 0, 58, 0, 34, 0, 101, 0, 100, 0, 116, 0, 80, 0, 114, 0, 105, 0, 99, 0, 101, 0, 34, 0, 44, 0, 34, 0, 98, 0, 101, 0, 100, 0, 114, 0, 111, 0, 111, 0, 109, 0, 115, 0, 34, 0, 58, 0, 34, 0, 50, 0, 34, 0, 44, 0, 34, 0, 98, 0, 97, 0, 116, 0, 104, 0, 114, 0, 111, 0, 111, 0, 109, 0, 115, 0, 34, 0, 58, 0, 34, 0, 50, 0, 34, 0, 44, 0, 34, 0, 115, 0, 113, 0, 117, 0, 97, 0, 114, 0, 101, 0, 70, 0, 101, 0, 101, 0, 116, 0, 34, 0, 58, 0, 34, 0, 49, 0, 50, 0, 48, 0, 48, 0, 34, 0, 44, 0, 34, 0, 97, 0, 100, 0, 100, 0, 114, 0, 101, 0, 115, 0, 115, 0, 34, 0, 58, 0, 34, 0, 101, 0, 100, 0, 116, 0, 65, 0, 100, 0, 100, 0, 114, 0, 34, 0, 44, 0, 34, 0, 99, 0, 105, 0, 116, 0, 121, 0, 34, 0, 58, 0, 34, 0, 101, 0, 100, 0, 116, 0, 67, 0, 105, 0, 116, 0, 121, 0, 34, 0, 44, 0, 34, 0, 112, 0, 111, 0, 115, 0, 116, 0, 97, 0, 108, 0, 34, 0, 58, 0, 34, 0, 101, 0, 100, 0, 116, 0, 60, 98, 62, 69, 114, 114, 111, 114, 58, 60, 47, 98, 62, 32, 91, 56, 93, 32, 84, 114, 121, 105, 110, 103, 32, 116, 111, 32, 103, 101, 116, 32, 112, 114, 111, 112, 101, 114, 116, 121, 32, 111, 102, 32, 110, 111, 110, 45, 111, 98, 106, 101, 99, 116, 60, 98, 114, 62, 51, 60, 98, 114, 62, 51, 13, 10, 11, 0, 0, 0, 84, 241, 24, 0, 1, 0, 0, 0, 208, 153, 241, 113, 0, 0, 0, 0, 100, 241, 24, 0, 76, 18, 7, 0, 90, 253, 160, 93, 4, 241, 24, 0, 250, 98, 65, 117, 76, 18, 7, 0, 13, 0, 0, 0, 11, 0, 0, 0, 44, 144, 47, 1, 208, 153, 241, 113, 205, 171, 186, 220, 0, 0, 0, 0, 0, 0, 0, 0, 208, 153, 241, 113, 20, 241, 24, 0, 48, 241, 24, 0, 48, 2, 72, 117, 130, 13, 184, 93, 124, 241, 24, 0, 145, 109, 65, 117, 104, 241, 24, 0, 34, 34, 83, 0, 139, 1, 0, 0, 200, 241, 24, 0, 156, 243, 24, 0, 36, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 112, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 233, 108, 65, 117, 145, 109, 65, 117, 233, 130, 64, 0, 136, 156, 46, 1, 173, 126, 64, 0, 1, 241, 24, 0, 144, 206, 37, 1, 117, 125, 64, 0, 224, 13, 47, 1, 47, 128, 64, 0, 183, 14, 37, 0, 224, 13, 47, 1, 128, 128, 64, 0, 144, 206, 37, 1, 48, 210, 80, 0, 56, 210, 80, 0, 144, 206, 37, 1, 160, 156, 46, 1, 99, 24, 83, 0, 192, 241, 24, 0, 122, 24, 83, 0, 130, 24, 83, 0, 108, 242, 24, 0, 140, 24, 83, 0, 0, 0, 0, 0, 0, 0, 0, 0, 183, 14, 37, 0, 0, 0, 0, 0, 144, 206, 37, 1, 216, 241, 24, 0, 58, 93, 76, 0, 139, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 242, 24, 0, 250, 98, 65, 117, 172, 17, 79, 0, 139, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 183, 14, 37, 0, 205, 171, 186, 220, 0, 0, 0, 0, 0, 0, 0, 0, 183, 14, 37, 0, 20, 242, 24, 0, 48, 242, 24, 0, 48, 2, 72, 117, 144, 206, 37, 1, 124, 242, 24, 0, 145, 109, 65, 117, 81, 109, 65, 117, 191, 255, 160, 93, 0, 0, 0, 0, 172, 17, 79, 0, 152, 31, 51, 1, 0, 0, 12, 0, 0, 184, 253, 126, 88, 242, 24, 0, 213, 98, 66, 117, 64, 170, 158, 1, 0, 0, 0, 0, 196, 242, 24, 0, 0, 184, 253, 126, 152, 31, 51, 1, 172, 17, 79, 0, 132, 242, 24, 0, 153, 203, 55, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 196, 242, 24, 0, 0, 0, 0, 0, 0, 184, 253, 126, 152, 31, 51, 1, 196, 242, 24, 0, 232, 109, 65, 117, 242, 0, 0, 0, 1, 0, 0, 0, 87, 0, 0, 0, 1, 0, 0, 0, 81, 0, 0, 0, 1, 0, 0, 0, 164, 174, 47, 1, 213, 62, 66, 0, 164, 174, 47, 1, 1, 0, 0, 0, 8, 0, 0, 0, 16, 0, 0, 0, 213, 100, 64, 0, 204, 242, 24, 0, 37, 177, 64, 0, 136, 174, 46, 1, 24, 175, 46, 1, 16, 0, 0, 0, 64, 10, 48, 1, 32, 175, 46, 1, 130, 100, 64, 0, 26, 179, 64, 0, 28, 175, 46, 1, 122, 167, 64, 0, 1, 0, 0, 0, 118, 247, 65, 0, 24, 175, 46, 1, 28, 175, 46, 1, 89, 166, 64, 0, 136, 174, 46, 1, 152, 22, 64, 0, 189, 125, 64, 0, 116, 117, 64, 0, 24, 175, 46, 1, 130, 100, 64, 0, 249, 115, 64, 0, 255, 255, 255, 255, 61, 123, 64, 0, 234, 116, 64, 0, 247, 116, 64, 0, 3, 0, 0, 0, 0, 0, 0, 0, 130, 100, 64, 0, 192, 149, 64, 0, 151, 160, 64, 0, 76, 10, 48, 1, 240, 174, 46, 1, 130, 100, 64, 0, 26, 179, 64, 0, 236, 174, 46, 1, 122, 167, 64, 0, 1, 0, 0, 0, 118, 247, 65, 0, 232, 174, 46, 1, 236, 174, 46, 1, 89, 166, 64, 0, 144, 166, 46, 1, 152, 22, 64, 0, 189, 125, 64, 0, 116, 117, 64, 0, 232, 174, 46, 1, 130, 100, 64, 0, 249, 115, 64, 0, 0, 0, 0, 0)
如果我像Unicode一样处理它,我会得到它。
笊∀瀀爀椀挀攀∀㨀∀攀搀琀倀爀椀挀攀∀Ⰰ∀戀攀搀爀漀漀洀猀∀㨀∀㈀∀Ⰰ∀戀愀琀栀爀漀漀洀猀∀㨀∀㈀∀Ⰰ∀猀焀甀愀爀攀䘀攀攀琀∀㨀∀'#$3100'㈀ ∀Ⰰ∀愀搀搀爀攀猀猀∀㨀∀攀搀琀䄀搀搀爀∀Ⰰ∀挀椀琀礀∀㨀∀攀搀琀䌀椀琀礀∀Ⰰ∀瀀漀猀琀愀氀∀㨀∀攀搀琀㰀㹢牅潲㩲⼼㹢嬠崸吠祲湩'#$2067'潴朠瑥瀠潲数瑲⁹景渠湯漭橢捥㱴牢㌾
你觉得我做错了什么?
谢谢!
答案 0 :(得分:2)
php://input
将为您提供传输到服务器的整个数据包,而不仅仅是内容。这也包括标题。
我不是德尔福专家,但我猜你不是按照你的想法发送JSON。
据我所知,这不应该发生,但无论如何我都会提到它:如果使用压缩,你获得的数据可能会被存档,但正如我所提到的,情况应该不是这样。
除非你得到一个好的标题+内容数据,否则意味着发送了错误的数据。