本文介绍了PHP - 当非UTF符号在URL中时,get_headers()返回错误的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我偶然发现了 get_headers()方法的错误结果。

I stumbled upon the wrong result of get_headers() method.

测试网址:

以下是对该网址的简单卷曲请求:

Here's simple curl request to that URL:

但如果我对同一个网址使用 get_headers()我得到另一个结果:

But if I using get_headers() for the same URL I'm getting anothere result:

var_dump(get_headers('http://www.zakon.hr/z/199/Zakon-o-elektroničkoj-trgovini'));

array(4) {
  [0]=>
  string(24) "HTTP/1.0 400 Bad request"
  [1]=>
  string(23) "Cache-Control: no-cache"
  [2]=>
  string(17) "Connection: close"
  [3]=>
  string(23) "Content-Type: text/html"
}

为什么?

推荐答案

最后一个术语包含需要正确编码的UTF-8数据。这个工程:

The last term contains UTF-8 data which needs to be properly encoded. This works:

var_dump(get_headers('http://www.zakon.hr/z/199/' .
    rawurlencode('Zakon-o-elektroničkoj-trgovini')
));

产生此输出:

array(11) {
  [0] =>
  string(15) "HTTP/1.1 200 OK"
  [1] =>
  string(73) "Set-Cookie: JSESSIONID=AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA; Path=/; HttpOnly"
  [2] =>
  string(100) "Set-Cookie: AAAA=AAAAAAAA-AAAA-AAAA-AAAA-AAAAAAAAAAAA; Expires=Wed, 09-Apr-2025 14:57:24 GMT; Path=/"
  [3] =>
  string(37) "Content-Type: text/html;charset=utf-8"
  [4] =>
  string(23) "Content-Language: en-US"
  [5] =>
  string(21) "Content-Length: 74205"
  [6] =>
  string(21) "Vary: Accept-Encoding"
  [7] =>
  string(35) "Date: Mon, 01 Jun 2015 14:57:24 GMT"
  [8] =>
  string(17) "Connection: close"
  [9] =>
  string(22) "Server: lighttpd/2.0.0"
  [10] =>
  string(43) "Set-Cookie: LBSERVERID=srv2-zakonhr; path=/"
}

这篇关于PHP - 当非UTF符号在URL中时,get_headers()返回错误的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 04:54