2016-02-12 44 views
1

問題的第一部分已解決。請看看編輯2urlencode&SQL請求不起作用...(cUrl和html特殊字符似乎不起作用)

我有以下問題: 我得到一個用戶名我的數據庫有htmlspecialchar,在這種情況下:exámple。我通過cUrl解析它到Riot-Games 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和它的作品...


我覺得cUrl無法解析字符串中的á,如果我複製網址,在瀏覽器中打開

á轉化爲%C3%A1

是它在某種程度上可以將網址轉換成PHP爲可發送的請求?


在此先感謝;)


編輯

如果我在它的工作原理我的用戶數據庫使用ex%C3%A1mple代替exámple


EDIT 2

我現在用urlencode的用戶名。 然而我絕望...

我簡單的腳本:

echo urlencode($row["username"]) . " " . urlencode('exámple'); 

$row["username"]輸出我保證'exámple'。這是一個SQL請求。

輸出我得到:

EX%E1mple EX%C3%A1mple

解決:

urlencode(utf8_encode($row["username"]))

+1

進行urlencode字符串傳遞捲曲之前。所以它應該是urlencode(你的數據庫值) –

回答

1

使用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; 
+0

爲什麼我的編碼用戶字符串看起來像:'ex%E1mple'? –

+0

請閱讀我的編輯... –

+0

'$ row [「username」]'有什麼? –