Home > Backend Development > PHP Tutorial > php file_get_contents returns empty invalid solution_PHP tutorial

php file_get_contents returns empty invalid solution_PHP tutorial

WBOY
Release: 2016-07-20 10:59:13
Original
1672 people have browsed it

The file_get_contents function is mostly used to collect content on remote servers, but before using the file_get_contents function, we must turn on allow_url_fopen in php.ini. ​

Problem Description

fopen(), file_get_contents(), getimagesize(), etc. cannot obtain the content on the network normally. The specific performance is that if the parameter is a URL, it will return a null value

If it is windows, you can find it

allow_url_fopen is on

If it is possible in linux

Recompile PHP and remove the –with-curlwrapper parameter - remember to execute make clean before compiling.


We use allow_url_fopen when windows account is not opened

The code is as follows Copy code
 代码如下 复制代码

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

< ?php

$file_contents = file_get_contents(''http://www.bkjia.com/'');

echo $file_contents;
代码如下 复制代码


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;
}

?>
cannot get the value, but we can use function_exists to determine whether this function is available.

The code is as follows Copy code
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; }
http://www.bkjia.com/PHPjc/445629.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445629.htmlTechArticleThe file_get_contents function is mostly used to collect content on the remote server, but before using the file_get_contents function, we have to use the file_get_contents function in php.ini Allow_url_fopen must be turned on. Problem description...
Related labels:
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