Home > Backend Development > PHP Tutorial > Solution about file_get_contents returning empty or the function is unavailable_PHP tutorial

Solution about file_get_contents returning empty or the function is unavailable_PHP tutorial

WBOY
Release: 2016-07-21 15:03:54
Original
1020 people have browsed it

If you use file_get_contents to obtain the contents of a remote file and the result is empty or the function is unavailable, maybe this article can help you!
Using file_get_contents and fopen must enable allow_url_fopen. Method: Edit php.ini and set allow_url_fopen = On. When allow_url_fopen is turned off, neither fopen nor file_get_contents can open remote files. If you are using a virtual host, consider using the curl function instead.
Usage example of curl function:

Copy code The code is as follows:

$ch = curl_init();
$timeout = 5;
curl_setopt ($ch, CURLOPT_URL, 'http://www.jb51.net');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);

Use the function_exists function to determine whether PHP supports it file_get_contents, otherwise use the curl function instead.
PS
1. If your hosting service provider has also turned off curl, then you should change the hosting provider!
2. Setting allow_url_fopen to off does not mean that your host does not support the file_get_content function. I just can't open the remote file. function_exists('file_get_contents') returns true. Therefore, the "Solution to the unavailability of file_get_contents function" circulated on the Internet still cannot solve the problem.
Error code:
Copy code The code is as follows:

if (function_exists('file_get_contents ')) {
$file_contents = @file_get_contents($url);
}else{
$ch = curl_init();
$timeout = 30;
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);
}

should be changed to:
Copy code The code is as follows:

if (function_exists('file_get_contents')) {//Determine whether file_get_contents is supported
$file_contents = @file_get_contents($url);
}
if ($file_contents == ”) {//Determine whether $file_contents is empty
$ch = curl_init();
$timeout = 30;
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);
}

Final code:
Copy code The code is as follows:

function file_get_content($url ) {
if (function_exists('file_get_contents')) {
$file_contents = @file_get_contents($url);
}
if ($file_contents == ”) {
$ch = curl_init();
$timeout = 30;
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;
}

Usage:
echo file_get_content('http://www.jb51.net');

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327815.htmlTechArticleIf you use file_get_contents to get the remote file content and the result is empty or the function is unavailable, maybe this article can help you ! Using file_get_contents and fopen must enable allow_ur...
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