PHP CURL isn't processing encoded return data properly
Asked Answered
H

5

21

Im have some minor encoding issues. Im getting a json data string from here (try it yourself):

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?

I tried switching it to utf-8 like this in php

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:

$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);
Hedda answered 18/9, 2013 at 5:50 Comment(2)
do you have control on the url the your are CURLing with? you can change the encoding thereCompressor
note that the second parameter of mb_convert_encoding is the to and the third is the from - so the line above converts from utf-8 to ISO-8859-1. See php.net/manual/en/function.mb-convert-encoding.phpSatin
C
33

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

   curl_setopt($ch, CURLOPT_ENCODING ,"");
Compressor answered 18/9, 2013 at 5:59 Comment(0)
A
9

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'));
Avera answered 4/9, 2014 at 6:45 Comment(2)
Whether it works or not, UTF-8 is not a valid argument for CURLOPT_ENCODING. This specifies the Accept-Encoding Field, and has nothing to do with character encoding. Valid arguments are: identity, gzip, deflate and "".Muns
I had different response for the same URL when request from CURL or from Browser. Lots of thanks for light me about CURLOPT_USERAGENT... now it works fine in both :)Lasseter
T
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).

Tyler answered 26/10, 2017 at 2:52 Comment(0)
T
0

you can try

$res= curl_exec ( $ch ); 
$result = iconv("Windows-1251", "UTF-8", $res);
Thinking answered 31/10, 2017 at 6:59 Comment(0)
S
0

It can help to be check your parameters for mb_convert_encoding are the correct order. You specify the to_encoding as the second parameter and then the from_encoding as the third parameter.

So this code means convert from utf-8 to ISO-8859-1

$r = mb_convert_encoding($r,'ISO-8859-1','utf-8');

Switching them about helped solve the issue for me.

Satin answered 27/4 at 21:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.