Home > Backend Development > PHP Tutorial > Detailed explanation of page capture function in php_PHP tutorial

Detailed explanation of page capture function in php_PHP tutorial

WBOY
Release: 2016-07-13 17:06:22
Original
967 people have browsed it

PHP provides a large number of functions for obtaining remote server files, including: file() function, file_get_contents() function, fopen()->fread()->fclose() mode, curl method, fsockopen( ) function, socket mode, etc., I will introduce them separately below.

1. file() function

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

Similar to file_get_contents(), except that file() returns the file as an array. Each cell in the array is a corresponding line in the file, including newlines.

Returns false if failed.

The code is as follows Copy code
 代码如下 复制代码


$url='http://www.bKjia.c0m';
$lines_array=file($url);
$lines_string=implode('',$lines_array);
echo htmlspecialchars($lines_string);

?>

$url='http://www.bKjia.c0m'; $lines_array=file($url);

$lines_string=implode('',$lines_array);

echo htmlspecialchars($lines_string);

?>

2. file_get_contents() function

 代码如下 复制代码

$url='http://www.bKjia.c0m';
$lines_string=file_get_contents($url);
echo htmlspecialchars($lines_string);

?>

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

Same as file(), except 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. If supported by the operating system, memory mapping technology is also used to enhance performance.

 代码如下 复制代码

$url='http://www.bKjia.c0m';
$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);

The code is as follows Copy code

$url='http://www.bKjia.c0m'; $lines_string=file_get_contents($url);

echo htmlspecialchars($lines_string);

?>

 代码如下 复制代码

$url='http://www.bKjia.c0m';
$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);

<🎜>Using file_get_contents and fopen must enable allow_url_fopen. Method: Edit php.ini and set allow_url_fopen = On. When allow_url_fopen is turned off, neither fopen nor file_get_contents can open remote files. <🎜> <🎜><🎜>3. fopen()->fread()->fclose() mode

The code is as follows Copy code
$url='http://www.bKjia.c0m'; <🎜> $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 method<🎜><🎜> <🎜>Using curl requires space to enable curl. Method: Modify php.ini under Windows, remove the semicolon in front of extension=php_curl.dll, and copy ssleay32.dll and libeay32.dll to C:WINDOWSsystem32; install the curl extension under Linux. <🎜>
The code is as follows Copy code
<🎜> $url='http://www.bKjia.c0m'; <🎜> $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
Whether the socket mode can be executed correctly is also related to the server settings. You can check which communication protocols are enabled by the server through phpinfo. For example, my local php socket does not enable http, so I can only use udp to test it.

There is also a function starting with curl_, which can achieve many functions. Do some research when you have time! 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 be used together with the other file functions (such as fgets() , fgetss() , fwrite() , fclose() , and feof() ). It returns a file handle

Enable PHP fsockopen function

PHP fsockopen requires the allow_url_fopen option in PHP.ini to be turned on.

The code is as follows
代码如下 复制代码

set_time_limit(0);
$fp = fsockopen("www.hzhuti.com", 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)
n";
} else {
   $out = "POST / HTTP/1.1rn";
   $out .= "Host: www.bKjia.c0mrn";
   $out .= "Connection: Closernrn";
   fwrite($fp, $out);
   while (!feof($fp)) {
       echo fgets($fp, 128);
   }
   fclose($fp);
}

Copy code
set_time_limit(0);

$fp = fsockopen("www.hzhuti.com", 80, $errno, $errstr, 30);

if (!$fp) {
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template