• 技术文章 >php教程 >PHP源码

    php 简单生成html文件类

    2016-06-08 17:27:54原创1038

    class mkHtml{
    var $url;
    var $contents;
    var $path;

    function __construct() {
    $this->url ='http://www.111cn.net/';
    $this->path ='../../default/';
    }

    function __destruct() {
    unset( $this->url );
    unset( $this->path );
    unset( $this->contents );
    }

    function getHtml()
    {
    if(function_exists('file_get_contents') )
    {
    $this->contents =file_get_contents($this->url);
    if( empty( $this->contents ) )
    {
    $this->curl();
    }

    }
    else
    {
    $this->curl();
    }
    }


    function curl()
    {
    $ch = curl_init();
    $timeout = 10;
    curl_setopt ($ch, CURLOPT_URL, $this->url);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $this->contents = curl_exec($ch);
    curl_close($ch);
    }

    function saveHtml()
    {
    $this->getHtml();
    if( !empty( $this->contents ) )
    {
    if( ! is_dir( $this->path ) || ! is_writable( $this->path ) )
    {
    echo '目录不存或不可写!';
    }
    else
    {
    $handle = fopen($this->path.'default.html','w+');
    fwrite($handle,$this->contents);
    fclose($handle);
    echo '生成首页成功';
    }
    }
    else
    {
    echo '未开通文件远程读取函数,请在php.ini中去了extension=php_curl.dll前面";"或设置allow_url_fopen=On';
    }
    }

    }
    //使用方法
    $html = new mkHtml();
    $html->saveHtml();

    //www.111cn.net教程原创,转载注明

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    专题推荐:nbsp this contents curl gt
    上一篇:php中英数字混排的字符串分割代码 下一篇:自己动手写 PHP MVC 框架(40节精讲/巨细/新人进阶必看)

    相关文章推荐

    • 用PHP MySQL进行分页的详细说明(二)• 费了点心思写的Php图像处理类• 收藏PHP常用自定义函数• PHP读取文本文件并逐行输出该行使用最多的字符串及对应次数• 你可能知道但是会犯的错误,细节决定成败
    1/1

    PHP中文网