Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用内容类型为text/plain的PHP获取外部JSON?_Php_Json - Fatal编程技术网

如何使用内容类型为text/plain的PHP获取外部JSON?

如何使用内容类型为text/plain的PHP获取外部JSON?,php,json,Php,Json,我正在尝试使用PHP后端获取外部JSON响应 但我的问题是,外部端点作为内容类型返回:text/plain;charset=utf-8,这只会让我在读它的时候胡言乱语 string '����������ݒ�J�� ... etc... 有没有办法对这种反应进行编码 这就是我所做的: $response = file_get_contents('external_url'); 我也试过: $json = json_decode(file_get_contents('external_url'

我正在尝试使用PHP后端获取外部JSON响应 但我的问题是,外部端点作为内容类型返回:text/plain;charset=utf-8,这只会让我在读它的时候胡言乱语

string '����������ݒ�J�� ... etc...
有没有办法对这种反应进行编码

这就是我所做的:

$response = file_get_contents('external_url');
我也试过:

$json = json_decode(file_get_contents('external_url'), true);

响应声明的内容类型对PHP来说并不重要,它对这些信息没有任何作用。无论标题是
text/plain
还是JSON,您都会得到完全相同的响应体


更有可能是响应被压缩了,您需要使用或类似的方法对其进行解压缩。

处理结果时有错误吗?如果只显示数据,是否已将文档设置为utf-8<代码>标题('Content-Type:text/html;charset=UTF-8')如果我使用json_解码,我会得到null,如果我在上面设置头,则没有区别。如果我这样做,我只会得到一个错误。。。“gzinflate():数据错误”这些是原始响应中的内容类型:内容编码:gzip内容长度:344662内容类型:text/plain;charset=utf-8好的,我可以将您设置为正确的答案,因为您确实为我指出了正确的方向,所以我必须使用readgzfile。如果需要,您可以将其编辑到您的答案中:)如果必须使用
readgzfile
,则表示您正在将内容存储在文件中。。。这与膨胀的用法有很大不同。。。?!无论如何,
gzip的手册页面膨胀
I链接到其他函数的链接,我明确地将它留给您,让您精确地确定哪一个是合适的。。。