php中get_meta_tags()、CURL与user-agent用法分析_PHP
本文实例分析了php中get_meta_tags()、CURL与user-agent用法。分享给大家供大家参考。具体分析如下:
get_meta_tags()函数用于抓取网页中形式的标签,并装入一维数组,name为元素下标,content为元素值,上例中的标签可以获得数组:array('A'=>'1', 'b'=>'2'),其他标签不处理,并且此函数只处理到标签时截止,之后的也不再继续处理,不过
之前的还是会处理.user-agent是浏览器在向服务器请求网页时,提交的不可见的头信息的一部分,头信息是一个数组,包含多个信息,比如本地缓存目录,cookies等,其中user-agent是浏览器类型申明,比如IE、Chrome、FF等.
今天在抓取一个网页的标签的时候,总是得到空值,但是直接查看网页源代码又是正常的,于是怀疑是否服务器设置了根据头信息来判断输出,先尝试使用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,获取之后在使用正则表达式分析.
伪造办法,代码如下:
代码如下:
// 初始化一个 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程序设计有所帮助。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
