PHP CURL 未正确处理编码的返回数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18864773/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP CURL isn't processing encoded return data properly
提问by Steven Baltay
Im have some minor encoding issues. Im getting a json data string from here (try it yourself):
我有一些小的编码问题。我从这里获取一个 json 数据字符串(自己尝试):
http://cdn.content.easports.com/fifa/fltOnlineAssets/C74DDF38-0B11-49b0-B199-2E2A11D1CC13/2014/fut/items/web/179899.json
The name in the data is shown like this
数据中的名字是这样显示的
Ari Sk?olason
How can I fetch this data with proper encoding so its Ari Skúlason?
我怎样才能用正确的编码来获取这些数据,以便它的 Ari Skúlason?
I tried switching it to utf-8 like this in php
我尝试在 php 中像这样将其切换为 utf-8
echo mb_convert_encoding($r,'ISO-8859-1','utf-8');
which got me closer, but its still not right
这让我更接近,但仍然不对
Ari Sk?lason
my php curl request:
我的 php curl 请求:
$location = 'http://cdn.content.easports.com/fifa/fltOnlineAssets/C74DDF38-0B11-49b0- B199-2E2A11D1CC13/2014/fut/items/web/179899.json';
$ch = curl_init($location);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept: application/json'));
$r = curl_exec($ch);
curl_close($ch);
echo mb_detect_encoding($r);
$r = mb_convert_encoding($r,'ISO-8859-1','utf-8');
print_r($r);
回答by Netorica
set another curl option for CURLOPT_ENCODING
and set it to "" to ensure it will not return any garbage
设置另一个 curl 选项 CURLOPT_ENCODING
并将其设置为 "" 以确保它不会返回任何垃圾
curl_setopt($ch, CURLOPT_ENCODING ,"");
回答by amir rasabeh
You Can use header
你可以使用标题
header('Content-type: text/html; charset=UTF-8');
and after decode string
和解码字符串后
$page = utf8_decode(curl_exec($ch));
It's worked for me
它对我有用
or
或者
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_ENCODING, 'UTF-8');
curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
after add this
添加这个之后
$page = curl_exec($ch);
$dom = new DOMDocument('1.0', 'utf-8');
libxml_use_internal_errors(true);
@$dom->loadHTML(mb_convert_encoding($page, 'HTML-ENTITIES', 'UTF-8'));
回答by Jake Pucan
You may also try.
你也可以试试。
...
...
$results = curl_exec($init);
curl_close($init);
return json_decode(utf8_encode($results));
utf8_encode encoded ASCII character. Returning a non-encoded ASCII may break or return an error (In my case).
utf8_encode 编码的 ASCII 字符。返回未编码的 ASCII 可能会中断或返回错误(在我的情况下)。
回答by Taron
you can try
你可以试试
$res= curl_exec ( $ch );
$result = iconv("Windows-1251", "UTF-8", $res);