472

I'm wondering if/how you can add custom headers to a cURL HTTP request in PHP. I'm trying to emulate how iTunes grabs artwork and it uses these non-standard headers:

X-Apple-Tz: 0
X-Apple-Store-Front: 143444,12

How could I add these headers to a request?

4 Answers 4

980
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'X-Apple-Tz: 0',
    'X-Apple-Store-Front: 143444,12'
]);

https://www.php.net/manual/en/function.curl-setopt.php

0
263

Use the following Syntax

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://www.example.com/process.php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$vars);  //Post Fields
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$headers = [
    'X-Apple-Tz: 0',
    'X-Apple-Store-Front: 143444,12',
    'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
    'Accept-Encoding: gzip, deflate',
    'Accept-Language: en-US,en;q=0.5',
    'Cache-Control: no-cache',
    'Content-Type: application/x-www-form-urlencoded; charset=utf-8',
    'Host: www.example.com',
    'Referer: http://www.example.com/index.php', //Your referrer address
    'User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:28.0) Gecko/20100101 Firefox/28.0',
    'X-MicrosoftAjax: Delta=true'
];

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$server_output = curl_exec ($ch);

curl_close ($ch);

print  $server_output ;
5
  • 44
    You deserve a cookie too Jun 23, 2016 at 16:30
  • 3
    Spoofing the user agent string sounds like a bad idea to me. Here is what the HTTP spec says. Sep 13, 2016 at 10:30
  • 2
    What's $vars in this example?
    – GRX
    Dec 19, 2017 at 10:38
  • 1
    @GRX , $vars is an array of your post data , sth like this : $vars = array('item1' => 'value1','item2' => 'value2');
    – Meisam
    May 19, 2018 at 18:23
  • CURLOPT_POSTFIELDS implies CURLOPT_POST so you don't need to set CURLOPT_POST
    – cquezel
    Dec 9, 2021 at 0:44
26
$subscription_key = '';
$host = '';    
$request_headers = [
    'X-Mashape-Key:' . $subscription_key,
    'X-Mashape-Host:' . $host
];

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);

$season_data = curl_exec($ch);

if (curl_errno($ch)) {
    print "Error: " . curl_error($ch);
    exit();
}

// Show me the result
curl_close($ch);
$json = json_decode($season_data, true);
19

Here is one basic function:

/**
 * 
 * @param string $url
 * @param string|array $post_fields
 * @param array $headers
 * @return type
 */
function cUrlGetData($url, $post_fields = null, $headers = null) {

    $ch = curl_init();
    $timeout = 5;
    curl_setopt($ch, CURLOPT_URL, $url);

    if (!empty($post_fields)) {

        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
    }

    if (!empty($headers))
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $data = curl_exec($ch);

    if (curl_errno($ch)) {

        echo 'Error:' . curl_error($ch);
    }

    curl_close($ch);
    return $data;
}

Usage example:

$url = "http://www.myurl.com";
$post_fields = 'postvars=val1&postvars2=val2';
$headers = ['Content-Type: application/x-www-form-urlencoded'];
$dat = cUrlGetData($url, $post_fields, $headers);
2
  • If some passes an empty $post_fields array like let's say: cUrlGetData($url, []) then the expression 'if ($post_fields)' would also be true and the if block will be executed, but the expression 'if ($post_fields && !empty($post_fields))' will not be true and all will be good :-)
    – Vlado
    Mar 15, 2021 at 13:05
  • 2
    @Vlado the headers should be passed without array keys :) CURLOPT_HTTPHEADER: An array of HTTP header fields to set, in the format array('Content-type: text/plain', 'Content-length: 100') Mar 29, 2021 at 21:48

Not the answer you're looking for? Browse other questions tagged or ask your own question.