PHP gets the function code for the contents of the remote file

  • 2020-03-31 20:33:39
  • OfStack

The following function:
 
<? 
 
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; 
} 
?> 

Related explanation:
1,ini_get: Returns the value of the configuration option as a string on success, or an empty string on failure(read 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 configuration file)
3,fopen("rb"): if you don't specify a 'b' flag when working with binaries, you may encounter some strange problems, including broken picture files and strange problems with \r\n characters.
Note: for portability, it is strongly recommended that you always use the 'b' flag when opening a file with fopen().
Note: again, for portability reasons, it is strongly recommended that you rewrite code that relies on the 't' pattern to use the correct line terminator and change to 'b' mode.
4. Strtolower -- Make a string lowercase
5,curl_init() :curl_init -- Initialize a cURL session
Resource curl_init ([string url])
Initializes a new session and return a cURL handle for use with the curl_setopt (), the curl_exec (), and curl_close () functions provides.
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
Bool curl_setopt (resource ch, int option, mixed value)
Sets an option on the given cURL session handle. (see the PHP manual for details.)
CURLOPT_URL: The URL to fetch. You can also set this when initializing a session with curl_init().
CURLOPT_CONNECTTIMEOUT: The number of seconds to wait indefinite trying to connect.use 0 to wait indefinitely.
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 the if the HTTP code returned is greater than or equal to 400. The default behaviors is to return to the page normally, ignoring the code.
CURLOPT_USERAGENT: The contents of The "user-agent:" header to be used in an HTTP request.
7,curl_exec: Perform a cURL session, This function should be called after you initialize a cURL session and all the options for the session are set.
Returns TRUE on success and FALSE on failure. 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

Here are some reference codes:
(link: #)
(link: #)

Related articles: