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.