Php file_get_contents setting proxy page capture example

Source: Internet
Author: User
Tags auth


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: Copy code


$ Url = http://www.111cn.net;
$ Contents = @ file_get_contents ($ url );
// Preg_match_all ("/<p class =" right forumcount "> (.*?) </P>/is ", $ contents, $ content );
Preg_match_all ("/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: Copy code

$ 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: Copy code

$ 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.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.