Parse file_get_contents Mimic browser header (user_agent) get data _php tips

Source: Internet
Author: User

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: *//*"
)
);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.