Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

curl

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

X-Filename  blahblah.zip
X-Filesize  2677
X-Filetype  application/zip
like image 621
StoneHeart Avatar asked Jul 24 '11 08:07

StoneHeart


People also ask

How do you pass custom headers in curl?

To send an HTTP header with a Curl request, you can use the -H command-line option and pass the header name and value in "Key: Value" format. If you do not provide a value for the header, this will remove the standard header that Curl would otherwise send. The number of HTTP headers is unlimited.

How do I add a custom header to request?

In the Home pane, double-click HTTP Response Headers. In the HTTP Response Headers pane, click Add... in the Actions pane. In the Add Custom HTTP Response Header dialog box, set the name and value for your custom header, and then click OK.

How does PHP handle curl request?

Next, we've set the CURLOPT_POST option to TRUE to set the request method to HTTP POST. Further, we need to use the CURLOPT_POSTFIELDS option to set the POST data that we want to submit along with the request. Finally, we've used the curl_exec function to execute the cURL request.

Which function in PHP do you use to set curl options?

Parameters ¶ A cURL handle returned by curl_init(). The CURLOPT_XXX option to set. The value to be set on option .


2 Answers

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

An array of HTTP header fields to set, in the format 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);
like image 160
Pascal MARTIN Avatar answered Sep 19 '22 18:09

Pascal MARTIN


curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-Filename: blahblah.zip', 'X-Filesize: 2677', 'X-Filetype: application/zip'));
like image 38
Ondrej Slinták Avatar answered Sep 19 '22 18:09

Ondrej Slinták