Home > Article > Backend Development > File_get_contents and curl function usage_PHP tutorial
The file_get_contents () application is very simple, but if some server php.ini settings turn off allow_url_fopen, this function will be invalid. Generally, personal servers can set it, but if it is a virtual host, it is not within their control. But curl is another function that opens the content of the remote page. The usage is as follows: // create a new curl resource // set URL and other appropriate options // grab URL and pass it to the browser // close curl resource, and free up system resources Of course, this feature may also be turned off. Using the above two methods, you can use function_exists() to determine the use if(function_exists(file_get_contents)) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
?>
$file_contents = file_get_contents($url);
} else {
$ch = curl_init();
$timeout = 5;
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);
}
return $file_contents;
if(function_exists(file_get_contents)) {
$file_contents = file_get_contents($url);
} else {
$ch = curl_init();
$timeout = 5;
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);
}
return $file_contents;