2011-10-04 35 views
0

所以,據我瞭解,我的問題是,即使它被傳遞,file_get_contents()也不會發送正確的UTF-8 URL,所以服務器收到的$ _GET數據有點搞砸了。我的代碼部分:爲什麼file_get_contents()弄亂了GET數據?

//receiving post data from html form "nacionālā opera" 
$q = $_POST["q"]; 

if (!empty($q)) { 
    $get_data = array( 
     'http' => array(
      'header'=> array("Content-Type: text/plain; charset=utf-8", 
          "User-agent: PHP bots;") 
     ) 
    ); 
    $stream_cont = stream_context_create($get_data); 

    $search = str_replace(" ", "+", $q); 
    $url = 'http://127.0.0.1/test.php?stotele='.$search.'&a=p.search&t=xml&day=1-5&l=lv'; 

    if (mb_detect_encoding($url) === 'UTF-8') { 
     echo '$url encoding is ok...??'; 
    } else { 
     echo 'URL encoding not UTF-8!'; 
     exit(); 
    } 

    $rec_data = file_get_contents($url, false, $stream_cont); 

這裏是打印$ _GET數組時什麼服務器獲取:

stotele => nacionÄ_lÄ_ opera // this should have been "nacionālā opera", but as u see it got distorted 
a => p.search 
t => xml 
day => 1-5 
l => lv 

我希望你明白我想說什麼。這件事讓我發瘋,我無法解決它,如果有人給我提示(是的,我的瀏覽器編碼設置爲UTF-8,表單也發送UTF-8,如果我回顯$ q或$搜索或$網址我得到一個正常的字符串,而不是一些亂七八糟的符號。

回答

2

嘗試使用urlencode

// instead of this: 
// $search = str_replace(" ", "+", $q); 
// use: 
$search = urlencode($q); 
$url = 'http://127.0.0.1/test.php?stotele='.$search.'&a=p.search&t=xml&day=1-5&l=lv'; 
+0

哈哈!我很確定我已經嘗試過這種方式,但它不起作用,但看起來像我做錯了什麼。謝謝!我的問題現在解決了:) – Javatar

0

看到它在php.net

if(mb_detect_encoding($str,"UTF-8, ISO-8859-1, GBK")=="UTF-8") 

if(mb_detect_encoding($str,"UTF-8, ISO-8859-1, GBK")==="UTF-8") 
相關問題