Home >Backend Development >PHP Tutorial >About the use of xml in php (SEO)

About the use of xml in php (SEO)

WBOY
WBOYOriginal
2016-08-08 09:19:021086browse

Regarding the use of xml, this place contains two functions written for SEO collection. They are for reference only. Welcome to communicate. If you have any questions, please ask. You can check the documentation for specific functions.

<code><span><span>function</span><span>edit_xml_file</span><span>(<span>$xml_file_path</span>,<span>$datas</span>,<span>$is_index</span> = false,<span>$is_compress</span> = true)</span>{</span><span>$doc</span> = <span>new</span> DomDocument(<span>'1.0'</span>, <span>'utf-8'</span>);
     <span>$doc</span>->formatOutput = <span>true</span>;
     <span>$flag</span> = <span>false</span>;
     <span>if</span>( !@file_exists(<span>$xml_file_path</span>) || !<span>$doc</span>->load(<span>$xml_file_path</span>)) {    
        <span>$flag</span> = create_xml_file(<span>$xml_file_path</span>,<span>$datas</span>,<span>$is_index</span>,<span>$is_compress</span>);
         <span>return</span><span>$flag</span>;
     } 
    <span>//读取文件数据</span><span>$xmldata</span> = simplexml_load_file(<span>$xml_file_path</span>);
    <span>$newxmldata</span> = <span>array</span>();
    <span>$xmL_url</span> = <span>array</span>();
    <span>foreach</span> (<span>$xmldata</span><span>as</span><span>$key</span>=><span>$obj</span>){
         <span>$obj</span> = (<span>array</span>) <span>$obj</span>;
         <span>if</span>(<span>isset</span>(<span>$obj</span>[<span>'lastmod'</span>])){
             <span>$obj</span>[<span>'lastmod'</span>] = date(<span>'c'</span>,time());
         }
         <span>$xmL_url</span>[] = <span>$obj</span>[<span>'loc'</span>];
         <span>$newxmldata</span>[][<span>$key</span>] = <span>$obj</span>;
    }
    <span>//将新添加的数据放在一起</span><span>foreach</span> (<span>$datas</span><span>as</span><span>$data</span>){
        <span>if</span>(!in_array(<span>$data</span>[<span>$key</span>][<span>'loc'</span>], <span>$xmL_url</span>)){
            <span>$newxmldata</span>[] = <span>$data</span>;
        }
    }

    <span>//根据新数据重新更新创建文件</span><span>if</span>(!<span>empty</span>(<span>$newxmldata</span>) && unlink(<span>$xml_file_path</span>)){
        <span>$flag</span> = create_xml_file(<span>$xml_file_path</span>,<span>$newxmldata</span>,<span>$is_index</span>,<span>$is_compress</span>);
    }
    <span>unset</span>(<span>$datas</span>);
    <span>return</span><span>$flag</span>;
}

<span>/** xml,追加数据
 *<span> @param</span> xml_file_path 文件路径,$data 数据,$is_index 为了区分是地图文件还是地图索引,默认是地图文件
 *<span> @author</span> zdj
 *<span> @date</span> 2015-01-23
 */</span><span><span>function</span><span>add_data_xml_file</span><span>(<span>$xml_file_path</span>,<span>$datas</span>,<span>$is_index</span> = false,<span>$is_compress</span> = true)</span>{</span><span>$doc</span> = <span>new</span> DomDocument(<span>'1.0'</span>, <span>'utf-8'</span>);
    <span>$doc</span>->formatOutput = <span>true</span>;
    <span>$flag</span> = <span>false</span>;
    <span>if</span>( !@file_exists(<span>$xml_file_path</span>) || !<span>$doc</span>->load(<span>$xml_file_path</span>)) {
        <span>$flag</span> = create_xml_file(<span>$xml_file_path</span>,<span>$datas</span>,<span>$is_index</span>,<span>$is_compress</span>);
        <span>return</span><span>$flag</span>;
    }
    <span>$root</span> = <span>$doc</span>->documentElement;
    <span>foreach</span> (<span>$datas</span><span>as</span><span>$data</span>){
        <span>foreach</span> (<span>$data</span><span>as</span><span>$key</span>=><span>$obj</span>){
            <span>$key_dom</span> = <span>$doc</span>->createElement(<span>$key</span>);
            <span>foreach</span> (<span>$obj</span><span>as</span><span>$attribute</span> =><span>$attribute_value</span>){
                <span>$attribute_dom</span> = <span>$doc</span>->createElement(<span>$attribute</span>);
                <span>$attribute_dom_value</span> = <span>$doc</span>->createTextNode(<span>$attribute_value</span>);
                <span>$attribute_dom</span>->appendChild(<span>$attribute_dom_value</span>);
                <span>$key_dom</span>->appendChild(<span>$attribute_dom</span>);
            }
            <span>$root</span>->appendChild(<span>$key_dom</span>);
        }
    }
    <span>$doc</span>->appendChild(<span>$root</span>);
    <span>$doc</span>->save(<span>$xml_file_path</span>);
    <span>//创建压缩的文件</span><span>$xml_gz_file_path</span> = <span>$xml_file_path</span>.<span>'.gz'</span>;
    <span>if</span>(<span>$is_compress</span>){
        <span>$fp</span> = gzopen (<span>$xml_gz_file_path</span>, <span>'w9'</span>);
        gzwrite (<span>$fp</span>, <span>$doc</span>->saveXML());
        gzclose(<span>$fp</span>);
    }
    <span>//给文件赋予权限//给文件赋予权限</span><span>if</span>(file_exists(<span>$xml_file_path</span>)){
        chmod(<span>$xml_file_path</span>, <span>0744</span>);
    }
    <span>if</span>(file_exists(<span>$xml_gz_file_path</span>)){
        chmod(<span>$xml_gz_file_path</span>, <span>0744</span>);
    }
    <span>unset</span>(<span>$datas</span>);
}</code>

Copyright Statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission.

The above introduces the use of XML in PHP (SEO), including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
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