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

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

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

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

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

file_get_contents('http://www.hhtjim.com/');
print_r($http_response_header);//输出响应头内容

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

PS:

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

<?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-file-get-contents-500-internal-server-error-in-php

其他文章:
本文固定链接:https://www.hhtjim.com/php-file_get_contents-function-to-access-the-url-display-response-headers.html
matrix
本文章由 matrix 于2014年10月01日发布在零零星星分类下,目前没有通告,你可以至底部留下评论。
转载请注明:php的file_get_contents函数访问URL显示响应头-HHTjim'S 部落格
关键字:, , ,

有6 条评论 »

  1. drones drones 2019-1-20 7:55:24 +0800#4

    Pretty section of content. I just stumbled upon your weblog and
    in accession capital to assert that I acquire actually
    enjoyed account your blog posts. Any way I will be subscribing to your
    augment and even I achievement you access consistently quickly.

  2. 佳佳君 佳佳君 2014-12-7 13:35:12 +0800#3

    新技能get!前天才用到file_get_contents想做个gravatar头像代理转发来着,结果还没做好。。。

  3. 五味瓶 五味瓶 2014-11-29 10:50:57 +0800#2

    可以fsockopen

    • Matrix Matrix Moderator 2014-11-30 10:28:11 +0800

      不了解这,很少用它

  4. 晨曦 晨曦 2014-10-17 13:44:06 +0800#1

    变量和函数 晕了

    • Matrix Matrix Moderator 2014-10-17 16:06:31 +0800

      🙄 多了解一些还是好的,至少可以修改出想要的功能

添加新评论 »

 🙈 😱 😂 😛 😭 😳 😀 😆 👿 😉 😯 😮 😕 😎 😐 😥 😡 😈 💡

插入图片

NOTICE: You should type some Chinese word (like “你好”) in your comment to pass the spam-check, thanks for your patience!