PHP - get_headers() return 非 UTF 符号在 URL 中时的错误结果
PHP - get_headers() return wrong result when non-UTF symbols are in URL
我偶然发现了 get_headers()
方法的错误结果。
URL 用于测试:http://www.zakon.hr/z/199/Zakon-o-elektroni%C4%8Dkoj-trgovini
这是对 URL 的简单 curl 请求:
如您在屏幕截图中所见,已成功响应 200 OK 代码。
但是如果我使用 get_headers()
相同 URL 我会得到另一个结果:
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 数据。这有效:
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=/"
}
我偶然发现了 get_headers()
方法的错误结果。
URL 用于测试:http://www.zakon.hr/z/199/Zakon-o-elektroni%C4%8Dkoj-trgovini
这是对 URL 的简单 curl 请求:
如您在屏幕截图中所见,已成功响应 200 OK 代码。
但是如果我使用 get_headers()
相同 URL 我会得到另一个结果:
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 数据。这有效:
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=/"
}