问题的第一部分解决了。请看编辑 2
我有以下问题:我用htmlspecialchar
从数据库中获取一个用户名,在这种情况下:exámple
。我通过cUrl
到拳头游戏API解析它。
$url = 'https://euw.api.pvp.net/api/lol/euw/v1.4/summoner/by-name/exámple?api_key=<my-api-key>'
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
return $output;
curl_close($ch);
我的结果是 :{ "status": { "message": "Not Found", "status_code": 404 } }
但是用户存在!我可以在浏览器中打开 URL,它可以工作...
我认为如果我复制 url 并在浏览器中打开它,cUrl
无法解析字符串中的 á
á
转换为%C3%A1
是否可以以某种方式将 PHP
中的 URL 转换为可发送的请求?
提前致谢;)
编辑
如果我在我的用户数据库中使用 ex%C3%A1mple
而不是exámple
它就可以了!
编辑 2
我现在使用urlencode
用户名。然而我绝望了...
我的简单脚本:
echo urlencode($row["username"]) . " " . urlencode('exámple');
$row["username"]
输出我保证'exámple'
。这是一个 SQL 请求。
我得到的输出:
ex%E1mple ex%C3%A1mple
解决:
urlencode(utf8_encode($row["username"]))
使用urlencode
函数对名称进行编码。请参阅下面的代码:
$name='exámple';
$encoded_name = urlencode('exámple'); //output: ex%C3%A1mple
$url = 'https://euw.api.pvp.net/api/lol/euw/v1.4/summoner/by-name/'.$encoded_name.'?api_key=<my-api-key>';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
echo $output;