What is the user agent
The user agent in Chinese is called "UA," which is a special string header that enables the server to identify the operating system and version, CPU type, browser and version, browser rendering engine, browser language, browser plugin, and so on that the client uses.
Websites can be judged by different UA to render different sites, such as mobile Access and PC access to display different pages.
PHP in the use of file_get_contents function to collect the site, sometimes it can be seen with a browser, but it is not to pick up any content.
This is most likely the server was set up, based on user_agent to determine whether the normal browser request, because the default PHP file_get_contents function is not sent UA.
If we want to collect such sites, we have to let the PHP simulation browser send UA, deceive the site back to normal content.
Implemented 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 an analog IE8 environment of UA, of course, you can also be replaced by other. Like Firefox.
You can also read this:
Copy Code code as follows:
$opts = Array (
' HTTP ' =>array (
' Method ' => ' get ',
' Header ' => ' host:zh.wikipedia.org\r\n '.
"Accept-language:zh-cn\r\n".
"User-agent:mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; trident/4.0; 4399box.560;. net4.0c;. NET4.0E) ".
"Accept: *//*"
)
);