为什么curl不发送PHP标头? [英] Why doesn't curl send my headers in PHP?

查看:70
本文介绍了为什么curl不发送PHP标头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码:

$ch = curl_init('http://localhost/testweb/search.php');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        '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',
        'Connection keep-alive',
        'SomeBull   BeingIgnored',
        'Cookie CLASSICPAGE=off',
        'User-Agent Mozilla/5.0 (Windows NT 5.1; rv:16.0) Gecko/20100101 Firefox/16.0'
        ));
$response = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
$body = substr($response, -$info['download_content_length']);
echo $body;

具有以下输出(php.exe mycurl.php):

has the following output (php.exe mycurl.php):

Host: localhost
Accept: */*
User-Agent      Mozilla/5.0 (Windows NT 5.1; rv: 16.0) Gecko/20100101 Firefox/16.0

本地主机上的search.php:

The search.php on localhost:

error_reporting(0);
header("Content-Type: text/plain");
foreach (getallheaders() as $name => $value) {
    echo "$name: $value\n";
}

我的问题是:我设置的标题发生了什么?

My question is: what happened to the headers I set?

推荐答案

标头格式为:

Header: value

您的示例缺少每个标题上的冒号。只需这样调整即可:

Your example is missing the colon on each of the headers. Just adjust it like so:

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    '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',
    'Connection: keep-alive',
    'SomeBull: BeingIgnored',
    'User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:16.0) Gecko/20100101 Firefox/16.0'
  )
);

这篇关于为什么curl不发送PHP标头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆