在PHP中编码CURL请求

时间:2022-10-19 18:19:04

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úlason

How can I fetch this data with proper encoding so its Ari Skúlason?

如何使用适当的编码获取此数据,以便它的AriSkú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);

4 个解决方案

#1


19  

set another curl option for CURLOPT_ENCODING and set it to "" to ensure it will not return any garbage

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

   curl_setopt($ch, CURLOPT_ENCODING ,"");

#2


6  

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'));

#3


0  

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可能会中断或返回错误(在我的情况下)。

#4


0  

you can try

你可以试试

$res= curl_exec ( $ch ); 
$result = iconv("Windows-1251", "UTF-8", $res);

#1


19  

set another curl option for CURLOPT_ENCODING and set it to "" to ensure it will not return any garbage

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

   curl_setopt($ch, CURLOPT_ENCODING ,"");

#2


6  

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'));

#3


0  

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可能会中断或返回错误(在我的情况下)。

#4


0  

you can try

你可以试试

$res= curl_exec ( $ch ); 
$result = iconv("Windows-1251", "UTF-8", $res);