前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >php的file_get_contents函数访问URL显示响应头

php的file_get_contents函数访问URL显示响应头

作者头像
HHTjim 部落格
发布2022-09-26 11:01:59
7320
发布2022-09-26 11:01:59
举报
文章被收录于专栏:HHTjim'S 部落格

php的file_get_contents函数访问URL显示响应头

作者:matrix 被围观: 5,529 次 发布时间:2014-10-01 分类:零零星星 | 6 条评论 »

这是一个创建于 2892 天前的主题,其中的信息可能已经有所发展或是发生改变。

在用 file_get_contents 访问 http 时,stream wrapper 会把响应头放到当前作用域下的 $http_response_header 数组变量里。

所以说变量$http_response_header就保存了需要的响应头,输出这个变量也就能拿到响应头。

代码语言:javascript
复制
file_get_contents('http://www.hhtjim.com/');
print_r($http_response_header);//输出响应头内容

参考:

http://www.jbxue.com/article/16319.html

PS:

平时用file_get_contents()函数读取url的网页内容,还不了解原来这玩意还有很多参数可以设置。

代码语言:javascript
复制
<?php
$url = 'http://www.baidu.com';
$opts = array('http'=>array('header' => "User-Agent:Mozilla/5.0 (Windows NT 6.2; WOW64; rv:32.0) Gecko/20100101 Firefox/32.0\r\n"));
$context = stream_context_create($opts);
$data = file_get_contents($url,false,$context);
print_r($data);

说明:

在sae上测试无法抓取(包括curl),才改用file_get_contents函数,并且添加User-Agent参数。

参考:http://blog.sina.com.cn/s/blog_4ae555810101cuef.html

http://stackoverflow.com/questions/22498581/[php](https://www.hhtjim.com/tag/php)-file-get-contents-500-internal-server-error-in-[php](https://www.hhtjim.com/tag/php)

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • php的file_get_contents函数访问URL显示响应头
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档