无法从file_get_contents进行json_decode字符串 [英] Failed to json_decode string from file_get_contents
本文介绍了无法从file_get_contents进行json_decode字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我最近想从Web服务获取和解码API响应.我以为只是file_get_contents
然后是json_decode
生成的字符串应该可以工作.
I recently wanted to fetch and decode API response from a web service. I thought that just just file_get_contents
and then json_decode
the resulting string should work.
看来我必须处理压缩后的响应和格式错误的JSON,才能最终对字符串进行解码.我该如何处理?
It looks like I have to deal with gzipped response and malformed JSON to finally decode the string. How can I handle these?
推荐答案
您可以使用curl
代替file_get_contents
并获取页面内容而无需任何编码
you can use curl
instead of file_get_contents
and get page content without any encoding
function get_url($link){
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch,CURLOPT_ENCODING, '');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, ($link));
$response = curl_exec($ch);
curl_close($ch);
return ($response);
}
这篇关于无法从file_get_contents进行json_decode字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文