1

I have the following code on my server running on php 5.2.*;

$curl = curl_init();
//$sumName = curl_escape($curl, $sumNameWeb);
$summonerName = urlencode($summonerName);
$url = "https://euw.api.pvp.net/api/lol/euw/v1.4/summoner/by-name/{$summonerName}?api_key=".$key;

curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_URL, $url);
$result = curl_exec($curl);
$result = utf8_encode($result);
$obj = json_decode($result, true);
$statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);

It works fine, however when it comes to special characters like; ë Ö å í .. etc it fails to connect.. I have been trying different ways maybe i would find a fix but i am failing to do so..

ok i have found my error!! however this is my situation.. it is connecting to the server and getting the data.. AND i am using $sumNameWeb to access the JSON when it is decoded however the returned $sumNameWeb special character has changed.. here is the code to access the JSON;

$sumID =  $obj[$sumNameWeb]["id"];
$sumLvl = $obj[$sumNameWeb]["summonerLevel"];

an example is, entering ë and returning ë from the server

Joseph118
  • 505
  • 6
  • 21
  • Check this may be useful: http://stackoverflow.com/questions/7979567/php-convert-any-string-to-utf-8-without-knowing-the-original-character-set-or – fortune Jun 19 '14 at 11:35

4 Answers4

2

Try This

Try to set one more curl parameter into your curl request that filters garbage data from result.

curl_setopt($curl, CURLOPT_ENCODING ,"");

I hope this helps you!!

Deepak Goswami
  • 2,030
  • 1
  • 16
  • 22
0

urlencode encode non-ASCII characters according to the UTF-8 charset encoding. So most likely your problem is that your text (source code) is in other encoding (different from UTF-8). You have to ensure it has UTF-8 encoding.

hindmost
  • 7,125
  • 3
  • 27
  • 39
  • i have been looking into the api and i have found this.. Accept-Charset: ISO-8859-1,utf-8 – Joseph118 Jun 19 '14 at 11:30
  • I've already pointed you the source of the problem. You have to ensure the string you passed to `urlencode` (`$summonerName`) has `UTF-8` encoding. – hindmost Jun 19 '14 at 12:05
0

Add header in the page before any sending curl.

header('Content-Type: text/html; charset=utf-8');
fortune
  • 3,361
  • 1
  • 20
  • 30
0

I faced the same problem. urlencode would not work with these links. I had to specifically replace them my self.

$curl = curl_init();
//$sumName = curl_escape($curl, $sumNameWeb);
$summonerName = urlencode($summonerName);
$url = "https://euw.api.pvp.net/api/lol/euw/v1.4/summoner/by-name/{$summonerName}?api_key=".$key;

$str = $url;
$str = str_replace("{", "%7B", $str);
$str = str_replace("$", "%24", $str);
$str = str_replace("}", "%7D", $str);
$url = $str;

curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_URL, $url);
$result = curl_exec($curl);
$result = utf8_encode($result);
$obj = json_decode($result, true);
$statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);

this should work. If additional characters need to be replaced you can find out their link substitute by following this link: url encoder

Thcsparky
  • 1
  • 1