Answer the question
In order to leave comments, you need to log in
Why is it impossible to access the header of an incoming request through the $_SERVER array?
Hello! This problem causes me cognitive dissonance! In general, there is such a shit code that processes API requests:
<?php
header("Content-Type:application/json");
if(in_array($_SERVER["CONTENT_TYPE"], ["application/json"])){ //(1)
if(!(isset($_GET["cookie"])) && !($_GET["key"] == "secret")){
http_response_code(403);
$response_data = array(
"error" => "bad_key"
);
}else{
$response_data = array(
"now" => time()
);
}
$response_data = json_encode($response_data);
print $response_data . "\n";
print $_SERVER["HTTP_CONTENT_TYPE"]; //(2)
}
?>
$_SERVER["CONTENT_TYPE"] == "application/json"
?print $_SERVER["CONTENT_TYPE"];
, то ничего не отправится в ответ, но если прописать $_SERVER["HTTP_CONTENT_TYPE"];
, то ответ отправится, но без желанного вывода типа запроса, например, application/json.<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Манипулирование файлами</title>
</head>
<body>
<?php
$options = array(
"key" => "secret"
);
$url = "http://asd/newFolder/formhandler.php?". http_build_query($options);
$c = curl_init($url);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_HTTPHEADER, array("Content-Type:application/json"));
//curl_setopt($c, CURLOPT_COOKIEJAR, "/cookies.cookies");
//curl_setopt($c, CURLOPT_COOKIEFILE, "/cookies.cookies");
$res = curl_exec($c);
$res = json_decode($res);
print "Количество секунд с начала эпохи " . $res->now . "<br>";
//print $res . "<br>";
//print $_SERVER["HTTP_CONTENT_TYPE"];
?>
</body>
</html>
Answer the question
In order to leave comments, you need to log in
я предполагаю, что дефолтом стоит GET
и вот такой прикол
Normal (GET) requests do not have a Content-Type header. For POST requests it would appear as $_SERVER["CONTENT_TYPE"], with a value like multipart/form-data or application/x-www-form-urlencoded.
This is mandated by the CGI/1.1 specification: http://www.ietf.org/rfc/rfc3875.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question