php header带参,php curl 请求中如何携带header参数

我想在curl post中带Content-Type参数,试了网上很多方法但依然没有用

'Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9) Gecko/2008052906 Firefox/3.0','Referer' => 'http://www.163.com');$url='http://walktest.sinaapp.com/env.php';$ch = curl_init($url);curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);curl_setopt($ch, CURLOPT_HEADER, true);curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);//curl_setopt($ch, CURLOPT_REFERER, 'http://www.baidu.com');$result = curl_exec($ch);curl_close($ch);echo $result;?>

这是我在网上找到的代码,但似乎不起作用

网站的Referer和User-Agent值依然没有改变

但如果我使用的是curl_setopt($ch, CURLOPT_REFERER, 'http://www.baidu.com');这种方法,则Referer的值能改变

Content-Type的值好像没法用curl_setopt($ch, CURLOPT_xxx,xxx)这种方式

所以到底怎样才能改变Content-Type的值?

回复讨论(解决方案)

$headers = array( 'User-Agent' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9) Gecko/2008052906 Firefox/3.0', 'Referer' => 'http://www.163.com','Content-type'=>'content-type');$url='http://walktest.sinaapp.com/env.php';$ch = curl_init($url);>

自己写错了,就不能怪人家不支持!$headers = array( 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9) Gecko/2008052906 Firefox/3.0', 'Referer: http://www.163.com',);$url='http://walktest.sinaapp.com/env.php';$ch = curl_init($url);curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);curl_setopt($ch, CURLOPT_HEADER, true);curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);//curl_setopt($ch, CURLOPT_REFERER, 'http://www.baidu.com');$result = curl_exec($ch);curl_close($ch);echo $result; HTTP/1.1 200 OK Server: nginx Date: Sun, 06 Dec 2015 01:33:43 GMT Content-Type: text/html Transfer-Encoding: chunked Connection: keep-alive Via: 10.67.21.26 Set-Cookie: saeut=117.70.161.80.1449365623790278; path=/; max-age=311040000 firefox3HTTP_USER_AGENT:Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9) Gecko/2008052906 Firefox/3.0HTTP_ACCEPT:*/*REQUEST_METHOD:GETHTTP_USER_AGENT:Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9) Gecko/2008052906 Firefox/3.0HTTP_REFERER:http://www.163.com

自己写错了,就不能怪人家不支持!$headers = array( 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9) Gecko/2008052906 Firefox/3.0', 'Referer: http://www.163.com',);$url='http://walktest.sinaapp.com/env.php';$ch = curl_init($url);curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);curl_setopt($ch, CURLOPT_HEADER, true);curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);//curl_setopt($ch, CURLOPT_REFERER, 'http://www.baidu.com');$result = curl_exec($ch);curl_close($ch);echo $result; HTTP/1.1 200 OK Server: nginx Date: Sun, 06 Dec 2015 01:33:43 GMT Content-Type: text/html Transfer-Encoding: chunked Connection: keep-alive Via: 10.67.21.26 Set-Cookie: saeut=117.70.161.80.1449365623790278; path=/; max-age=311040000 firefox3HTTP_USER_AGENT:Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9) Gecko/2008052906 Firefox/3.0HTTP_ACCEPT:*/*REQUEST_METHOD:GETHTTP_USER_AGENT:Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9) Gecko/2008052906 Firefox/3.0HTTP_REFERER:http://www.163.com

嗯,这个方法确实有效,还有一点,我补充一下,就是我还用了这个方法

curl_setopt($ch, CURLOPT_COOKIE, $cookie);

$cookie的取值也会影响Header是否成功设置

本文原创发布php中文网,转载请注明出处,感谢您的尊重!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值