Phpfile_get_contents setting proxy page capture example-PHP source code

Source: Internet
Author: User
The file_get_contents function can directly open local files in php or directly capture remote server files. For simple collection, we can use file_get_contents to directly perform operations, if there is anti-collection, we may need a proxy to perform the operation. Next I will introduce the file_get_contents page capture example. The file_get_contents function can directly open local files in php or directly capture remote server files. For simple collection, we can use file_get_contents to directly perform operations, if there is anti-collection, we may need a proxy to perform the operation. Next I will introduce the file_get_contents page capture example.

Script ec (2); script


Example 1: Obtain the common page

For example, you can access the 54master forum to capture all elements in the h3 mark on the homepage.

The Code is as follows:


$ Url = http://www.111cn.net;
$ Contents = @ file_get_contents ($ url );
// Preg_match_all ("/

(.*?)

/Is ", $ contents, $ content );
Preg_match_all ("/(.*?) /Is ", $ contents, $ content );
Print_r ($ content [0]);

Example 2: Set the code IP address to collect data

Use file_get_contents and stream_context_create.

The Code is as follows:

The Code is as follows:

$ AContext = array (
'Http' => array (
'Proxy' => 'tcp: // 192.168.0.2: 8080', // set the proxy ip address and port number you want to use here.
'Request _ fulluri '=> true,
),
);
$ CxContext = stream_context_create ($ aContext );
$ SFile = maid ("http://www.111cn.net", False, $ cxContext );
Echo $ sFile;

The above Code is applicable to normal conditions. However, if you need to log on to the target page or access through authentication information, you can add another code.

The Code is as follows:

$ Auth = base64_encode ('login: password'); // LOGIN: PASSWORD the name and PASSWORD of your account.
$ AContext = array (
'Http' => array (
'Proxy' => 'tcp: // 192.168.0.2: 8080', // set the proxy ip address and port number you want to use here.
'Request _ fulluri '=> true,
'Header' => "Proxy-Authorization: Basic $ auth ",
),
);
$ CxContext = stream_context_create ($ aContext );
$ SFile = file_get_contents (http://www.111cn.net, False, $ cxContext );
Echo $ sFile;

In this way, you can use a proxy to capture pages or files.

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.