页面静态化的三种方法

2018-07-29  本文已影响0人  码课sir

方法一

<?php
$str=file_get_contents('http://www.php.com/index.php');//通过http访问返回的是apache执行后的结果
file_put_contents('./index.html',$str);//将生成的html代码写到静态页面中

方法二

<?php
ob_start(  );//开启缓存
require './index.php';
$str=ob_get_contents(  );//获取缓存中的内容
ob_end_clean(  );//关闭缓存
file_put_contents('./index.html',$str);//将缓存的内容写到文件中

方法三

注:通过url扩展实现静态化(注意:此方法得保证php.ini中extension=php_url.dll扩展是打开的

<?php
$curl=curl_init(  );//第一步:初始化curl资源
//第二步:设置curl参数
curl_setopt($curl,CURLOPT_URL,'http://www.php.com/index.php');//设置请求地址
curl_setopt($curl,CURLOPT_RETURNTRANSFER,TRUE);//以文件流形式返回
//第三步:执行curl对象
$str=curl_exec($curl);//执行$curl,返回就是响应主体
file_put_contents('./index.html',$str);
上一篇 下一篇

猜你喜欢

热点阅读