Home > Backend Development > PHP Tutorial > Solution to the problem that the file_get_contents function cannot be used_PHP tutorial

Solution to the problem that the file_get_contents function cannot be used_PHP tutorial

WBOY
Release: 2016-07-13 17:36:36
Original
1068 people have browsed it

Some hosting service providers have turned off the allow_url_fopen option of PHP, which means that file_get_contents cannot be used directly to obtain the content of the remote web page. That is, you can use another function curl.

The following are different ways of writing the same function of the two functions file_get_contents and curl

Usage example of file_get_contents function:

< ?php
$file_contents = file_get_contents(http://www.ccvita.com/);
echo $file_contents;
?>

Example of using curl function:

< ?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 and 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 provider turns off both file_get_contents and curl, an error will occur in the above function.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/508221.htmlTechArticleSome hosting service providers have turned off the allow_url_fopen option of PHP, so they cannot directly use file_get_contents to obtain remote web pages. content. That is, you can use another function...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template