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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6805656/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:17:52  来源:igfitidea点击:

How to set custom request header keys with curl and PHP?

phpcurl

提问by StoneHeart

I'm using curl and php to upload file. I need help to set a custom request header keys like

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

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

回答by Pascal MARTIN

You must use the curl_setopt()function, and its CURLOPT_HTTPHEADERoption (quoting) :

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

An array of HTTP header fields to set, in the format array('Content-type: text/plain', 'Content-length: 100')

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


Basically, in your case, you'd have something like this :


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

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

回答by Ondrej Slinták

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