PHP function code for obtaining Remote File Content

Source: Internet
Author: User

Function:
Copy codeThe 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.

Related Article

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.