PHP Capture Crawl page function detailed

Source: Internet
Author: User
Tags curl fread ini unix domain socket

1. File () function

The file () function reads the entire document into an array.

Similar to file_get_contents (), the difference is that file () returns files as an array. Each cell in the array is the corresponding line in the file, including line breaks.

If it fails, it returns false.

The code is as follows Copy Code


<?php
$url = ' http://www.111cn.net ';
$lines _array=file ($url);
$lines _string=implode (", $lines _array);
Echo htmlspecialchars ($lines _string);

?>

2. file_get_contents () function


The file_get_contents () function reads the entire file into a string.

As with file (), the difference is that file_get_contents () reads the file into a string.

The file_get_contents () function is the preferred method for reading the contents of a file into a string. Memory mapping technology is also used to enhance performance if supported by the operating system.

The code is as follows Copy Code

<?php
$url = ' http://www.111cn.net ';
$lines _string=file_get_contents ($url);
Echo htmlspecialchars ($lines _string);

?>

Use file_get_contents and fopen must have space to open allow_url_fopen. Methods: Edit PHP.ini, set allow_url_fopen = on,allow_url_fopen cannot open remote files when fopen and file_get_contents are closed.

3. fopen ()->fread ()->fclose () mode

The code is as follows Copy Code

<?php
$url = ' http://www.111cn.net ';
$handle =fopen ($url, "RB");
$lines _string= "";
do{
$data =fread ($handle, 1024);
if (strlen ($data) ==0) {
Break
}
$lines _string.= $data;
}while (TRUE);
Fclose ($handle);
Echo htmlspecialchars ($lines _string);

4. Curl Mode

Use curl must have space to open curl. Method: Modify php.ini under Windows, remove the semicolon in front of Extension=php_curl.dll, and need to copy Ssleay32.dll and Libeay32.dll to C:windowssystem32 ; Linux to install the Curl extension.

The code is as follows Copy Code

<?php
$url = ' http://www.111cn.net ';
$ch =curl_init ();
$timeout = 5;
curl_setopt ($ch, Curlopt_url, $url);
curl_setopt ($ch, Curlopt_returntransfer, 1);
curl_setopt ($ch, Curlopt_connecttimeout, $timeout);
$lines _string=curl_exec ($ch);
Curl_close ($ch);
Echo htmlspecialchars ($lines _string);


5. Fsockopen () function socket mode
socket mode can be correctly executed, but also related to the server settings, you can see through the phpinfo of the server to open which communication protocols, such as my local PHP socket did not open HTTP, only use UDP test.

There is also a function that starts with curl_, and can be implemented in many functions. Have time to study well! The following is an introduction to Fscokopen


1.PHP fsockopen Function Description:

Open Internet or Unix domain socket connection (open socket link)

Initiates a socket connection to the resource specified by target.

Fsockopen () Returns a file pointer which may is used together with the other file functions (such as fgets (), FGETSS (), Fwrite (), fclose (), and feof (). is to return a file handle

Open PHP Fsockopen This function

The PHP fsockopen requires php.ini allow_url_fopen option to open.

  code is as follows copy code

<?php
Set_time_limit (0);
$fp = Fsockopen ("Www.111cn.net", $errno, $errstr, 30);
if (! $fp) {
   echo "$errstr ($errno) <br/>n";
} else {
   $out = "Post/http/1.1rn";
   $out. = "HOST:WWW.111CN.NETRN";
   $out. = "Connection:closernrn";
   fwrite ($fp, $out);
   while (!feof ($fp)) {
       echo fgets ($FP, 128);
  }
   fclose ($FP);
}

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.