Php simulates users to obtain remote file content-PHP source code

Source: Internet
Author: User
Ec (2); 1, ini_get: returnsthevalueofconfigurationoptionasastringonsuccess, oranemptystringonfailure (read php tutorial. ini configuration file value) 2,; WhethertoallowthetreatmentofURLs (likehttp: script ec (2); script

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 tutorial. 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 questions about rn 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)


/**
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;
}
?>

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.