PHP cURL 自定义标头

Posted

技术标签:

【中文标题】PHP cURL 自定义标头【英文标题】:PHP cURL custom headers 【发布时间】:2011-12-28 06:39:51 【问题描述】:

我想知道是否/如何在 php 中向 cURL HTTP 请求添加自定义标头。我正在尝试模拟 iTunes 如何抓取艺术品并使用这些非标准标题:

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

如何将这些标头添加到请求中?

【问题讨论】:

【参考方案1】:
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'X-Apple-Tz: 0',
    'X-Apple-Store-Front: 143444,12'
));

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

【讨论】:

【参考方案2】:

使用以下语法

$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 ;

【讨论】:

你也应该得到一个 cookie 欺骗用户代理字符串对我来说是个坏主意。 Here is what the HTTP spec says. 本例中的$vars 是什么? @GRX , $vars 是你的帖子数据的数组,像这样: $vars = array('item1' => 'value1','item2' => 'value2'); CURLOPT_POSTFIELDS 暗示 CURLOPT_POST 所以你不需要设置 CURLOPT_POST【参考方案3】:
$subscription_key  ='';
    $host = '';    
    $request_headers = array(
                    "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);

【讨论】:

【参考方案4】:

这是一个基本功能:

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

使用示例:

$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);

【讨论】:

如果有人传递了一个空的 $post_fields 数组,例如:cUrlGetData($url, []) 那么表达式 'if ($post_fields)' 也将为真并且 if 块将被执行,但表达式 'if ($post_fields && !empty($post_fields))' 不会是真的,一切都会好的:-) @Vlado 标头应该通过 without array keys :) CURLOPT_HTTPHEADER: An array of HTTP header fields to set, in the format array('Content-type: text/plain', 'Content-length: 100')

以上是关于PHP cURL 自定义标头的主要内容,如果未能解决你的问题,请参考以下文章

使用 CURL 发送自定义标头

将自定义标头添加到 AFNetworking,我做错了啥?

如何在curl中设置自定义的HTTP头

如何将自定义响应标头添加到来自 azure blob 的响应?

带有 pycurl 的自定义标头

通过 RSpec 发送自定义标头