dongyin0628 2015-06-01 14:49
浏览 76
已采纳

当非UTF符号在URL中时,PHP-get_headers()返回错误的结果

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:

enter image description here

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条回答 默认 最新

  • dsebywql016137 2015-06-01 14:59
    关注

    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=/"
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号