PHP obtains the function code of the remote file content. The following function: Copy the code :? ** Get remote file content @ param $ url file http address * functionfopen_url ($ url) {if (function_exists (file_get_contents) {$ fil:
The code is as follows:
/**
Obtain remote file content
@ Param $ url: the http address of the file
*/
Function fopen_url ($ url)
{
If (function_exists ('File _ get_contents ')){
$ File_content = @ file_get_contents ($ url );
} Elseif (ini_get ('allow _ url_fopen') & ($ file = @ fopen ($ url, 'RB '))){
$ I = 0;
While (! Feof ($ file) & $ I ++ <1000 ){
$ File_content. = strtolower (fread ($ file, 4096 ));
}
Fclose ($ file );
} Elseif (function_exists ('curl _ init ')){
$ Curl_handle = curl_init ();
Curl_setopt ($ curl_handle, CURLOPT_URL, $ url );
Curl_setopt ($ curl_handle, CURLOPT_CONNECTTIMEOUT, 2 );
Curl_setopt ($ curl_handle, CURLOPT_RETURNTRANSFER, 1 );
Curl_setopt ($ curl_handle, CURLOPT_FAILONERROR, 1 );
Curl_setopt ($ curl_handle, CURLOPT_USERAGENT, 'trackback Spam check'); // Reference Spam Check
$ File_content = curl_exec ($ curl_handle );
Curl_close ($ curl_handle );
} Else {
$ File_content = '';
}
Return $ file_content;
}
?>
Explanation:
1, ini_get: Returns the value of the configuration option as a string on success, or an empty string on failure (read the value in the php. ini configuration file)
2,; Whether to allow the treatment of URLs (like http: // or ftp: //) as files.
Allow_url_fopen = On (content in the configuration file)
3. fopen ("rb"): If the 'B' flag is not specified when operating the binary file, some strange problems may occur, including corrupted image files and strange \ r \ n characters.
Note: for portability, we strongly recommend that you always use the 'B' flag when opening a file with fopen.
Note: For the sake of portability, we strongly recommend that you rewrite the code dependent on the 'T' mode to use the correct line terminator and change it to the 'B' mode.
4, strtolower -- Make a string lowercase
5, curl_init (): curl_init -- Initialize a cURL session (Initialize a cUrl session)
Resource curl_init ([string url])
Initializes a new session and return a cURL handle for use with the curl_setopt (), curl_exec (), and curl_close () functions.
Url -- If provided, the CURLOPT_URL option will be set to its value. You can manually set this using the curl_setopt () function.
Returns a cURL handle on success, FALSE on errors.
6, curl_setopt -- Set an option for a cURL transfer (Set)
Bool curl_setopt (resource ch, int option, mixed value)
Sets an option on the given cURL session handle. (for details, see the PHP Manual) There:
CURLOPT_URL: The URL to fetch. You can set this when initializing a session with curl_init ().
CURLOPT_CONNECTTIMEOUT: The number of seconds to wait whilst trying to connect. Use 0 to wait indefinitely. (waiting for an indefinite period is set to 0)
CURLOPT_RETURNTRANSFER: TRUE to return the transfer as a string of the return value of curl_exec () instead of outputting it out directly.
CURLOPT_FAILONERROR: TRUE to fail silently if the HTTP code returned is greater than or equal to 400. The default behavior is to return the page normally, ignoring the code.
CURLOPT_USERAGENT: The contents of the "User-Agent:" header to be used in a HTTP request.
7, curl_exec: Perform a cURL session, This function shocould be called after you initialize a cURL session and all the options for the session are set.
If the call succeeds, TRUE is returned. if the call fails, FALSE is returned. However, if the CURLOPT_RETURNTRANSFER option is set, it will return the result on success, FALSE on failure
8, curl_close -- Close a cURL session
The following is some reference code:
Common functions of PHP collection programs
PHP collects and obtains the content of a specified website.
The pipeline code is as follows :? /** Get remote file content @ param $ url file http address */function fopen_url ($ url) {if (function_exists ('File _ get_contents ') {$ fil...