为什么 file_get_contents() 会弄乱 GET 数据?

发布于 2024-12-07 17:21:49 字数 1201 浏览 0 评论 0原文

所以,据我所知,我的问题是 file_get_contents() 没有发送正确的 UTF-8 URL,即使它正在被传递,所以服务器接收的 $_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 或 $ search 或 $url 我得到一个正常的字符串,而不是一些混乱的符号。

So, I as I understand, my problem is that file_get_contents() does not send proper UTF-8 URL even though it's being passed, so the $_GET data which server is receiving is a bit messed up. Part of my code:

//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);

Here is what the server gets when printing the $_GET array:

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 

I hope you understand what I am trying to say. This thing drives me crazy and I can't solve it, would be nice if someone gave me hints(and yes, my browser encoding is set to UTF-8, also form is sending UTF-8 and if I echo $q or $search or $url I get a normal string, not some messed up symbols.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一个人练习一个人 2024-12-14 17:21:49

尝试使用 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';

Try using 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';
别把无礼当个性 2024-12-14 17:21:49

在 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" ) 

saw it on php.net

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

or

if( mb_detect_encoding($str,"UTF-8, ISO-8859-1, GBK")==="UTF-8" ) 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文