PHP - get_headers() return wrong result when non-UTF symbols are in URL

孤人 提交于 2019-12-24 05:21:29

问题


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

URL for testing: http://www.zakon.hr/z/199/Zakon-o-elektroni%C4%8Dkoj-trgovini

Here's simple curl request to that URL:

As you can see on screenshot there's successful response with 200 OK code.

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"
}

Why is that?


回答1:


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

Produces this output:

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=/"
}


来源:https://stackoverflow.com/questions/30576588/php-get-headers-return-wrong-result-when-non-utf-symbols-are-in-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!