我有以下方法,该方法是使用 cURL 从网络服务器获取响应。
function login (string $_login, string $_password) : string {
$url = "https://acweb.net.br/api/orcamentos/login";
$fields = [
"login" => $_login,
"password" => $_password
];
$headers = [
"Try" => "Trying"
];
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url);
curl_setopt( $ch, CURLOPT_POST, true);
curl_setopt( $ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
return curl_exec( $ch );
}
一切正常!
我可以通过
获得 $ _ POST 的值
print_r ($_POST)
但是我无法获得 CURLOPT_HTTPHEADER 的值。
编辑:
我确实尝试过:
print_r ($_SERVER)
但它不在那里。
如何获取 CURLOPT_HTTPHEADER 的值?
$ _ SERVER 中的所有 HTTP_headers :
[HTTP_HOST] => ctemcasb.com.br
[HTTP_CONNECTION] => keep-alive
[HTTP_UPGRADE_INSECURE_REQUESTS] => 1
[HTTP_USER_AGENT] => Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36
[HTTP_SEC_FETCH_USER] => ?1
[HTTP_ACCEPT] => text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3
[HTTP_SEC_FETCH_SITE] => none
[HTTP_SEC_FETCH_MODE] => navigate
[HTTP_ACCEPT_ENCODING] => gzip, deflate, br
[HTTP_ACCEPT_LANGUAGE] => pt-BR,pt;q=0.9,en-US;q=0.8,en;q=0.7
[HTTP_COOKIE] => PHPSESSID=j4cqqdc83fia68nk0gsglqk1bv
HTTP_TRY ,不存在。
现在呢?
我是在服务器上完成的:
print_r($_SERVER)
和
print_r ($_SERVER["HTTP_TRY]);
答案 0 :(得分:1)
标头不应是关联数组,而应是字符串的索引数组。
$headers = [
'Try: Trying',
'Content-Type: text/html',
...
];
然后,您应该可以使用$_SERVER['HTTP_TRY']
访问标头,因为自定义标头的前缀为HTTP_