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);
|
2. file_get_contents() function
代码如下 | 复制代码 |
$url='http://www.bKjia.c0m'; ?> |
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'; |
The code is as follows | Copy code | ||||
$url='http://www.bKjia.c0m'; $lines_string=file_get_contents($url); echo htmlspecialchars($lines_string);?>
|
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);<🎜> |
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
|
Copy code | ||||
$fp = fsockopen("www.hzhuti.com", 80, $errno, $errstr, 30);