1、file_get_contents() 函数把整个文件读入一个字符串中。file_get_contents() 函数是用于将文件的内容读入到一个字符串中的首选方法。
利用file_get_contents和file_put_contents函数,可以把一个正在浏览的页面的内容保存为一个本地的文件中。
示例1:
<?phpdate_default_timezone_set('Asia/Chongqing'); //设置时区echo "正在读取页面内容……<br>";$url = "https://v1.21lhz.cn/API/Six/api.php?type=JSon";$page_content = file_get_contents($url);echo "正在将页面内容保存到文件中……<br>";$file_path = "webindex.txt";file_put_contents($file_path,$page_content) or die("不能写入文件");echo "保存完毕!……<br>";?>
示例2:
<?phpheader("content-type:text/html;charset=utf-8;");echo "正在读取页面内容……<br>";$info=file_get_contents("https://v1.21lhz.cn/API/Yiyan/api.php?type=json");//echo $info;$new_info = json_decode($info,true);//print_r($new_info);$page_content = $new_info["text"];//print_r($realtext);//echo "随机一言:".$page_contentecho "正在将页面内容保存到文件中……<br>";$file_path = "webindex1.html";file_put_contents($file_path,$page_content, FILE_APPEND | LOCK_EX) or die("不能写入文件");echo "保存完毕!……<br>";?>
以上就是file_get_contents()和file_put_contents() 的区别和用法的全部内容了,希望能够帮助到你,找建站教程记得来极光资源网!