如何使用 curl 和 PHP 设置自定义请求标头键?

2022-08-30 20:53:54

我正在使用curl和php上传文件。我需要帮助来设置自定义请求标头键,例如

X-Filename  blahblah.zip
X-Filesize  2677
X-Filetype  application/zip

答案 1

您必须使用 curl_setopt() 函数及其CURLOPT_HTTPHEADER选项(引用):

要设置的 HTTP 标头字段的数组,格式为array('Content-type: text/plain', 'Content-length: 100')


基本上,在你的情况下,你会有这样的东西:

$headers = array(
    'X-Filename: blahblah.zip', 
    'X-Filesize: 2677', 
    'X-Filetype: application/zip', 
);
curl_setopt($your_resource, CURLOPT_HTTPHEADER, $headers);

答案 2
curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-Filename: blahblah.zip', 'X-Filesize: 2677', 'X-Filetype: application/zip'));

推荐