PHP access to remote file content function code _php tips

Source: Internet
Author: User
Tags curl sessions
The following functions:
Copy Code code as follows:

?
/**
Get Remote file contents
@param $url File HTTP address
*/
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 explanations:
1,ini_get:returns the value of the configuration option as a string on success, or a empty string on failure (read php.ini Values in the configuration file)
2,; Whether to allow the treatment of URL (like http://or ftp://) as files.
Allow_url_fopen = On (content in configuration file)
3,fopen ("RB"): If you do not specify ' B ' tags when manipulating binaries, you may encounter some strange problems, including bad picture files and strange questions about the \ r \ n characters.
Note: For portability reasons, it is strongly recommended that you always use the ' B ' flag when you open a file with fopen ().
Note: Again, for transplant considerations, it is strongly recommended that you rewrite the code that relies on the ' t ' pattern to use the correct line terminator and change it to ' B ' mode.
4,strtolower--Make a string lowercase
5,curl_init (): Curl_init--Initialize A Curl session (initialize a Curl conversation)
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 is set to its value. You can manually set this using the curl_setopt () function.
Returns a CURL handle on success and FALSE on errors.
6,curl_setopt--Set an option for a curl transfer (provide settings)
BOOL Curl_setopt (resource ch, int option, mixed value)
Sets an option on the given CURL session handle. (See the PHP Manual for details) There:
Curlopt_url:the URL to fetch. You can also set this when initializing a sessions with Curl_init ().
Curlopt_connecttimeout:the number of seconds to whilst trying to connect. Use 0 to wait indefinitely. (indefinite wait set to 0)
Curlopt_returntransfer:true to return the transfer as a string, the return value of curl_exec () instead of outputting I T out directly.
Curlopt_failonerror:true to fail silently if the HTTP code returned are 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 is used in a HTTP request.
7,curl_exec:perform A Curl session, this function should is called after you initialize a Curl sessions and all optio NS for the session are set.
Returns TRUE if successful, and returns FALSE if it fails. However, if the Curlopt_returntransfer option is set, it'll return the result of success, FALSE on Failure
8,curl_close--Close a Curl session

Here are some reference codes:
PHP Acquisition Program Common functions
PHP captures the contents of the specified URL

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.