欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php中get_meta_tags()、CURL与user-agent用法分析

程序员文章站 2023-08-26 10:49:02
本文实例分析了php中get_meta_tags()、curl与user-agent用法。分享给大家供大家参考。具体分析如下: get_meta_tags()函数用于抓取...

本文实例分析了php中get_meta_tags()、curl与user-agent用法。分享给大家供大家参考。具体分析如下:

get_meta_tags()函数用于抓取网页中<meta name="a" content="1"><meta name="b" content="2">形式的标签,并装入一维数组,name为元素下标,content为元素值,上例中的标签可以获得数组:array('a'=>'1', 'b'=>'2'),其他<meta>标签不处理,并且此函数只处理到</head>标签时截止,之后的<meta>也不再继续处理,不过<head>之前的<meta>还是会处理.

user-agent是浏览器在向服务器请求网页时,提交的不可见的头信息的一部分,头信息是一个数组,包含多个信息,比如本地缓存目录,cookies等,其中user-agent是浏览器类型申明,比如ie、chrome、ff等.

今天在抓取一个网页的<meta>标签的时候,总是得到空值,但是直接查看网页源代码又是正常的,于是怀疑是否服务器设置了根据头信息来判断输出,先尝试使用get_meta_tags()来抓取一个本地的文件,然后这个本地文件将获取的头信息写入文件,结果如下,其中替换成了/,方便查看,代码如下:

复制代码 代码如下:
array (
  'http_host' => '192.168.30.205',
  'path' => 'c:/program files/common files/netsarang;c:/program files/nvidia corporation/physx/common;c:/program files/common files/microsoft shared/windows live;c:/program files/intel/icls client/;c:/windows/system32;c:/windows;c:/windows/system32/wbem;c:/windows/system32/windowspowershell/v1.0/;c:/program files/intel/intel(r) management engine components/dal;c:/program files/intel/intel(r) management engine components/ipt;c:/program files/intel/opencl sdk/2.0/bin/x86;c:/program files/common files/thunder network/kankan/codecs;c:/program files/quicktime alternative/qtsystem;c:/program files/windows live/shared;c:/program files/quicktime alternative/qtsystem/; %java_home%/bin;%java_home%/jre/bin;',
  'systemroot' => 'c:/windows',
  'comspec' => 'c:/windows/system32/cmd.exe',
  'pathext' => '.com;.exe;.bat;.cmd;.vbs;.vbe;.js;.jse;.wsf;.wsh;.msc',
  'windir' => 'c:/windows',
  'server_signature' => '',
  'server_software' => 'apache/2.2.11 (win32) php/5.2.8',
  'server_name' => '192.168.30.205',
  'server_addr' => '192.168.30.205',
  'server_port' => '80',
  'remote_addr' => '192.168.30.205',
  'document_root' => 'e:/wamp/www',
  'server_admin' => 'admin@admin.com',
  'script_filename' => 'e:/wamp/www/user-agent.php',
  'remote_port' => '59479',
  'gateway_interface' => 'cgi/1.1',
  'server_protocol' => 'http/1.0',
  'request_method' => 'get',
  'query_string' => '',
  'request_uri' => '/user-agent.php',
  'script_name' => '/user-agent.php',
  'php_self' => '/user-agent.php',
  'request_time' => 1400747529,
)

果然在数组中没有http_user_agent这个元素,apache在向另外一台服务器发送请求的时候是没有ua的,之后查了一下资料,get_meta_tags()函数没有伪造ua的能力,所以只能使用其他办法解决了.

后来使用curl来获取,就获取到了网页,不过使用上稍微麻烦一点,首先伪造ua,获取之后在使用正则表达式分析<meta>.

伪造办法,代码如下:

复制代码 代码如下:
// 初始化一个 curl
$curl = curl_init();
 
// 设置你需要抓取的url
curl_setopt($curl, curlopt_url, 'http://localhost/user-agent.php');
 
// 设置是否将文件头输出到浏览器,0不输出
curl_setopt($curl, curlopt_header, 0);
 
// 设置ua,这里是将浏览器的ua转发到服务器,也可以手动指定值
curl_setopt($curl, curlopt_useragent, $_server['http_user_agent']);
 
// 设置curl 参数,要求结果返回到字符串中还是输出到屏幕上。0输出屏幕并返回操作结果的bool值,1返回字符串
curl_setopt($curl, curlopt_returntransfer, 1);
// 运行curl,请求网页
$data = curl_exec($curl);
 
// 关闭url请求
curl_close($curl);
 
// 处理获得的数据
var_dump($data);

希望本文所述对大家的php程序设计有所帮助。