Some hosting service providers have turned off the allow_url_fopen option of PHP, so they cannot directly use file_get_contents to obtain the content of the remote web page. That is, you can use another function curl.
The following isan example of using the file_get_contents function
in different ways of writing the same function: file_get_contents and curl
:< ?php
$file_contents = file_get_contents('http://www.ccvita.com/');
echo $file_contents;
?>
Example of using curl function instead:
< ?php
$ch = curl_init();
$timeout = 5;
curl_setopt ($ch, CURLOPT_URL, 'http://www.ccvita.com');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);
echo $file_contents;
?>
Use the function_exists function to determine whether PHP supports a function. You can easily write the following function
<?php
function vita_get_url_content($url) {
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;
}
?>
In fact, the above function is still open to discussion. If your hosting service provider turns off both file_get_contents and curl, an error will occur in the above function.