PHP的var_dump / print_r输出出现乱码-编码问题? [英] PHP's var_dump / print_r output is garbled - encoding issue?

查看:377
本文介绍了PHP的var_dump / print_r输出出现乱码-编码问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一个问题,在服务器上 var_dump print_r 的输出完全乱码了。 print_r 输出纯乱码(例如 ] {W 8 ...- ...等),而 var_dump 至少给出 string(1664),其后跟类似的乱码(尽管这次用双引号引起来) )。

I'm having a problem where on a server the output of var_dump and print_r come out entirely garbled. print_r outputs pure gibberish (eg. ��]{W�8�����- ... etc), while var_dump at least gives string (1664), followed by similar gibberish (though this time wrapped in double quotes).

这看起来像是字符编码问题,但是找不到能解决的编码(而且我不知道为什么只应该转储PHP对象输出非ASCII字符),并且 echo 可以正常工作。另外,我想知道这是否可能是gzip问题。无论哪种方式,我都怀疑它一定是PHP或Apache的配置文件,但我不知道如何解决它。

This looks like a character encoding issue, but no encoding I can find seems to fix it (and I don't know why just dumping a PHP object should be outputting non-ascii characters anyway), and echo works fine. Alternatively, I wonder if it could be a gzip issue. Either way, I suspect it must be something in PHP or Apache's configuration, but I have no idea how to fix it.

如果有人有任何帮助,我将非常感谢有关如何解决此问题的建议!

I'd be very grateful if anyone has any suggestions as to how fix this!

更新:在进一步调查中,看来这是一个我要转储的特定对象的特定问题。有问题的对象是通过API请求的JSON解码(通过curl)。 json_decode curl 是否可能被错误配置/破坏了编码?

Update: on further investigation, it seems it's a problem specific to the particular object I'm trying to dump. The object in question is decoded JSON requested (via curl) from an API. Is it possible that either json_decode or curl could be misconfigured / mangling the encoding?

推荐答案

对于它的价值,我终于找到了这个问题的根源(我认为!)

For what it's worth, I finally got to the bottom of this problem (I think!)

问题似乎是,无论是否为JSON,API的输出都通过 json_decode 运行。 MySQL错误导致错误页面,而不是JSON响应,该页面在 var_dump之前通过 json_decode (通过接收该错误的API处理代码)运行时如上所产生的乱码沙拉。

The problem seems to be that the API's output was being run through json_decode whether it was JSON or not. MySQL errors were causing an error page, not a JSON response, which when run through json_decode (by the API-handling code that received it) before var_dump produced garbled character salad, as above.

这篇关于PHP的var_dump / print_r输出出现乱码-编码问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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