HTTP response code after redirect to non-ascii domain name

折月煮酒 提交于 2020-01-06 06:01:45

问题


How can I get all headers for domain redirecting to non-ASCII domain?

Related question

The domain www.sendevinci.co.il redirecting to non-ASCII domain www.מגשי-אירוח.co.il.

My code is:

$url = 'http://www.sendevinci.co.il';

$options['http'] = array(
	'method' => "HEAD",
        'ignore_errors' => 1,
        'header' =>
 	       	"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n" .
        	"Accept-Language: en-US,en;q=0.8\r\n".
                "Keep-Alive: timeout=3, max=10\r\n",
                "Connection: keep-alive",
	'user_agent' => self::get_user_agent_string($user_agent),
        "timeout" => 3
);

$context = stream_context_create($options);

$body = file_get_contents($url, NULL, $context);

print_r($http_response_header);

The result is:

Array
(
    [0] => HTTP/1.1 301 Moved Permanently
    [1] => Server: nginx
    [2] => Date: Thu, 07 Jun 2018 13:07:30 GMT
    [3] => Content-Type: text/html; charset=UTF-8
    [4] => Connection: close
    [5] => X-Powered-By: PHP/7.2.6
    [6] => Location: https://www.מגשי-אירוח.co.il
    [7] => X-Powered-By: PleskLin
)

The array should include 200 status as well, but it not.

The code works fine for ascii-name domains redirecting to ascii-name domains.

Thanks for you help!


回答1:


At the HTTP protocol level, a 200 response code with a Location header does not make sense. You can use a Location header with 201 or 202, but generally, 3xx is used for sending the client to a new location. I posit that the response you are receiving is correct. In bullet form, the HTTP location header is valid with:

  • 201 - new resource created; get appropriate new location
  • 202 - request accepted, still processing, check "here"
  • 3xx - general redirection (301/Permanent, 302/Temporary, etc.)

In other words, your assertion that [t]he array should include 200 status as well is not correct. The HTTP status code given by the server in the example is 301.




回答2:


Actually, file_get_contents works ONLY with ascii domain names. So there are no simple solution to get all http headers...



来源:https://stackoverflow.com/questions/50742294/http-response-code-after-redirect-to-non-ascii-domain-name

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