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: