java吧 关注:1,269,347贴子:12,773,636
  • 2回复贴,共1

php读取动态页面生成静态html文件的方法

只看楼主收藏回复

file_get_contents
smarty 可以生成缓存,
其实生成的原理很简单
1、读取模版信息
2、替换模版内容 如将{title}替换成 '标题名'
3、写入到您要生成的静态页面里 如果是动态的(?id=$id),在php页面处理时可以根据接收条件的不同替换不同的内容生成不同的静态页(2page.html),注意模版还是那个模版并没有改变!
下面是一个小偷程序的小例子:不要跟SMARTY原理混淆;
这里的URL必须是全路径的如http://www.baidu.comhttp://localhost/aa.php?id=$id(本机状态脱机、联机都可以) 需要注意的就是php程序运行的超时问题,这里不多说了后面文章有写!
简单说下fread() 和 file_get_contents()的区别,fread读取的是一个相对路径的文件如aa.html aa.php注意没有?,fread办得到的file_get_contents()也能办得到
<?
//获取文件内容
$content=file_get_contents(http://www.www.com/viewPage.php?id=$id );
//检查是否存在旧文件,有则删除
if(file_exists($filename)) unlink($filename);
//设置静态文件路径及文件名
$filename="viewPage/$id.html";
//写入文件
$fp = fopen($filename, 'w');
fwrite($fp, $content);
?>
这样就不用替换了,比较简单
更多、更新PHP视频教程下载地址
http://www.itxdl.cn


1楼2015-05-26 15:24回复
    为了服务器安全着想,所以把allow_url_fopen关掉了。
    当服务器allow_url_fopen = Off 时,就不能用file_get_contents,只有设置ON时可以用。
    <?php /*
    $getstr=file_get_contents("http://www.163.com/weatherxml/54511.xml");
    $qx=explode("\"",strstr($getstr,"qx="));
    $wd=explode("\"",strstr($getstr,"wd="));
    $qximg=explode("\"",strstr($getstr,"qximg="));
    $qximg_=explode(",",$qximg[1]);
    echo "北京&nbsp;".$qx[1]."";
    echo $wd[1];*/
    //echo "<img src='http://news.163.com/img/logo/".$qximg_[0]."'><img src='http://news.163.com/img/logo/".$qximg_[1]."'>";
    ?>
    以下是通curl_init函数来获取163天气预报
    把php.ini里( ;extension=php_curl.dll ) 前面的(;)去掉保存
    把php_curl.dll,libeay32.dll,ssleay32.dll拷到c:\windows\system32里,重启IIS即可,没有装apache
    <?php
    //初始化curl
    $ch = curl_init() or die (curl_error());
    //设置URL参数
    curl_setopt($ch,CURLOPT_URL,"http://www.163.com/weatherxml/54511.xml");
    //要求CURL返回数据
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    //执行请求
    $result = curl_exec($ch) or die (curl_error());
    //取得返回的结果,并显示
    //echo $result;
    // echo curl_error($ch);
    $qx=explode("\"",strstr($result,"qx="));
    $wd=explode("\"",strstr($result,"wd="));
    $qximg=explode("\"",strstr($result,"qximg="));
    $qximg_=explode(",",$qximg[1]);
    echo "北京&nbsp;".$qx[1]."<br />";
    echo $wd[1];
    //关闭CURL
    curl_close($ch);
    ?>


    2楼2015-05-26 15:26
    回复
      2025-07-15 06:40:01
      广告


      4楼2015-07-01 17:03
      回复