There are a number of functions in PHP that get remote server files, including: File () function, file_get_contents () function, fopen ()->fread ()->fclose () mode, curl method, Fsockopen () function, socket mode, and so on, let me introduce you separately.
1. File () function
The file () function reads entire files into an array.
Like 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 the newline character.
If it fails, it returns false.
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
The file_get_contents () function reads the entire file into a string.
As with file (), the difference is that file_get_contents () reads a 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 techniques are also used to enhance performance.
The code is as follows |
Copy Code |
$url = ' http://www.bKjia.c0m '; $lines _string=file_get_contents ($url); Echo htmlspecialchars ($lines _string); ?> |
Use file_get_contents and fopen to open allow_url_fopen. Method: Edit PHP.ini, set allow_url_fopen = On,allow_url_fopen Close when fopen and file_get_contents cannot 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 Mode
Use curl to have space to turn on 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 ; 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
Socket mode can be executed correctly, but also with the server settings, in particular, through the phpinfo to see what communication protocols are opened by the server, such as my local PHP socket does not open HTTP, only use UDP test.
There is also a function that begins with Curl_, which allows for many functions. Have time to study well! Here is an introduction to Fscokopen
1.PHP fsockopen Function Description:
Open Internet or Unix domain socket connection (opens 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
PHP Fsockopen requires the php.ini allow_url_fopen option to open.
The code is as follows |
Copy Code |
Set_time_limit (0); $fp = Fsockopen ("www.hzhuti.com", $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); } |
http://www.bkjia.com/PHPjc/630706.html www.bkjia.com true http://www.bkjia.com/PHPjc/630706.html techarticle A large number of functions are provided in PHP to obtain remote server files, including: File () function, file_get_contents () function, fopen ()->fread ()->fclose () mode, curl method, Fsockopen () function ...