Parse file_get_contents to imitate browser header (user_agent) to obtain data_PHP tutorial

WBOY
Release: 2016-07-21 15:03:14
Original
882 people have browsed it

What is user agent
User Agent is called user agent in Chinese, or UA for short. It is a special string header that allows the server to identify the operating system used by the customer. and version, CPU type, browser and version, browser rendering engine, browser language, browser plug-in, etc.
The website can present different websites by judging different UAs. For example, mobile phone access and PC access display different pages.
When PHP uses the file_get_contents function to collect websites, sometimes it can be viewed with a browser, but no content can be collected.
This is most likely because settings have been made on the server to determine whether it is a normal browser request based on User_agent, because by default PHP's file_get_contents function does not send ua.
If we want to collect such a website, we must let PHP simulate the browser to send UA and trick the website into returning normal content.

The implementation is as follows:
ini_set('user_agent','Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; 4399Box.560; .NET4.0C ; .NET4.0E)');
This is a UA that simulates the IE8 environment. Of course, you can also change it to another one. For example, Firefox
can also be read like this:

Copy the code The code is as follows:

$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"Host: zh.wikipedia.orgrn" .
"Accept-language: zh-cnrn" .
" Accept: *//*"
)
);

http://www.bkjia.com/PHPjc/327875.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327875.htmlTechArticleWhat is user agent? The Chinese name of User Agent is User Agent, or UA for short. It is a special string header that makes The server can identify the operating system and version used by the customer, CPU type, browser...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template