我的代码中自定义的Headers与实际请求中的Headers不一致

问题描述 投票:0回答:0

我的代码:

<?php
function post($url, $data, $header) {
$ch = curl_init();
if(substr($url, 0, 5) == 'https') {
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($ch);
if($error = curl_error($ch)){
    die($error);
}
curl_close($ch);
return $response;
}

$pdfHeader = [
'Content-Type:application/json;charset=UTF-8',
'Connection:keep-alive', 
'userId:1a2b3c4d5e6f',
'token:abcdefg'
];
$pdfURL = 'http://www.example.com/api';
$pdfParam = 'pdfName=test.pdf&page=1';
echo post($pdfURL, $pdfHeader, $pdfParam); //The api should return a download path of pdf file
?>

标题应该是这样的:

Content-Type:application/json;
charset=UTF-8;
Connection:keep-alive;
userId=1a2b3c4d5e6f;
token:abcdefg

但是在实际请求中,Header变成了这样:

Content-Type: 
Content-Type:application/json;
charset=UTF-8;
Connection: 
Connection:keep-alive;
userId: 
userId:a1b2c3d4e5f6;
token: 
token:abcdefg
php api web http-post request-headers
© www.soinside.com 2019 - 2024. All rights reserved.