2011-10-04 90 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")