Encabezados personalizados PHP cURL

Resuelto 123 asked hace 54 años • 4 respuestas

Me pregunto si y cómo se pueden agregar encabezados personalizados a una solicitud HTTP cURL en PHP. Estoy intentando emular cómo iTunes toma las ilustraciones y utiliza estos encabezados no estándar:

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

¿Cómo podría agregar estos encabezados a una solicitud?

123 avatar Jan 01 '70 08:01 123
Aceptado
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

Brad avatar Nov 13 '2011 23:11 Brad

Utilice la siguiente sintaxis

$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;
Dadaso Zanzane avatar Feb 05 '2015 15:02 Dadaso Zanzane
$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);
Sapna Bhayal avatar Sep 28 '2018 11:09 Sapna Bhayal

Aquí hay una función básica:

/**
 * 
 * @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;
}

Ejemplo de uso:

$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);
Vlado avatar Feb 19 '2018 13:02 Vlado