为什么 file_get_contents() 会弄乱 GET 数据?
所以,据我所知,我的问题是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试使用 urlencode
Try using urlencode
在 php.net 上看到它
或
saw it on php.net
or