本文实例分析了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程序设计有所帮助。

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


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

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

Atom editor mac version download
The most popular open source editor

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software