在PHP中编码CURL请求

Ste*_*tay 18 php encoding curl http utf-8

我有一些小的编码问题.我从这里得到一个json数据字符串(自己尝试):

http://cdn.content.easports.com/fifa/fltOnlineAssets/C74DDF38-0B11-49b0-B199-2E2A11D1CC13/2014/fut/items/web/179899.json
Run Code Online (Sandbox Code Playgroud)

数据中的名称如下所示

Ari Skúlason
Run Code Online (Sandbox Code Playgroud)

如何使用适当的编码获取此数据,以便它的AriSkúlason?

我尝试在php中将它切换为utf-8

echo mb_convert_encoding($r,'ISO-8859-1','utf-8');
Run Code Online (Sandbox Code Playgroud)

让我更接近,但它仍然不对

Ari Sk?lason
Run Code Online (Sandbox Code Playgroud)

我的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);
Run Code Online (Sandbox Code Playgroud)

Mp *_*ega 25

设置另一个curl选项 CURLOPT_ENCODING并将其设置为""以确保它不会返回任何垃圾

   curl_setopt($ch, CURLOPT_ENCODING ,"");
Run Code Online (Sandbox Code Playgroud)


ami*_*beh 7

你可以使用标题

   header('Content-type: text/html; charset=UTF-8');
Run Code Online (Sandbox Code Playgroud)

并在解码字符串之后

 $page = utf8_decode(curl_exec($ch));
Run Code Online (Sandbox Code Playgroud)

它对我有用

要么

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');
Run Code Online (Sandbox Code Playgroud)

加上这个之后

$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'));
Run Code Online (Sandbox Code Playgroud)

  • 无论是否有效,UTF-8都不是CURLOPT_ENCODING的有效参数.这指定了Accept-Encoding字段,与字符编码无关.有效的参数是:identity,gzip,deflate和"". (9认同)