zoukankan      html  css  js  c++  java
  • 接口开发之PHP创建XML文件

    用PHP的DOM控件来创建输出

    输出的格式为XML

    接口开发的相关文件及说明

    <?php
        header("Content-type: text/xml");//头文件非常重要
    
        //创建xml文件
        $dom=new DOMDocument('1.0','utf-8');
        
        //建立<root>元素
        $root=$dom->createElement('root');
        
        //把<root>元素追加到文档里面
        $dom->appendChild($root);
        
        //建立<book>元素并将其作为<root>的子元素
        $book=$dom->createElement('book');
        $root->appendChild($book);
        
        //接受GET过来的数据
        $key=$_GET['key'];
        $name=$_GET['name'];
    
        //引入数据库配置文件
        include("config.inc.php");  
    
        //将get来的数据与数据库里的数据进行校验
        $sql1=mysql_query("select count(id) as sum from user where `key` = ".$key."");
        
        //返回结果集
        $result=mysql_fetch_array($sql1);
    
        //如果数据库里存在key
        if($result[0]>0){
            //校验name
            $sql2=mysql_query("select * from book where `name` = '".$name."'");
            $row=mysql_fetch_row($sql2);
    
            if(mysql_num_rows($sql2)>0){
    
                //为<book>创建name元素,并追加name的值
                $name=$dom->createElement('name');
                $nameText=$dom->createTextNode($row[1]);
                $name->appendChild($nameText);
                $book->appendChild($name);
                
                //为<book>创建author元素,并追加author的值
                $author=$dom->createElement('author');
                $authorText=$dom->createTextNode($row[2]);
                $author->appendChild($authorText);
                $book->appendChild($author);
                
                //为<book>创建press元素,并追加press的值
                $press=$dom->createElement('press');
                $pressText=$dom->createTextNode($row[3]);
                $press->appendChild($pressText);
                $book->appendChild($press);
                
                //为<book>创建price元素,并追加prcie的值
                $price=$dom->createElement('price');
                $priceText=$dom->createTextNode($row[4]);
                $price->appendChild($priceText);
                $book->appendChild($price);
    
                //为<book>创建time元素,并追加time的值
                $time=$dom->createElement('time');
                $timeText=$dom->createTextNode($row[5]);
                $time->appendChild($timeText);
                $book->appendChild($time);
    
                //为<book>创建intro元素,并追加intro的值
                $intro=$dom->createElement('intro');
                $introText=$dom->createTextNode($row[6]);
                $intro->appendChild($introText);
                $book->appendChild($intro);
    
    
            }else{//如果name不存在
                //建立<error_code>,并追加error_code的值
                $error_code=$dom->createElement('error_code');
                $error_code_Text=$dom->createTextNode('错误代码1001');
                $error_code->appendChild($error_code_Text);
                $book->appendChild($error_code);
                
                //建立<result>,并追加result的值
                $result=$dom->createElement('result');
                $result_Text=$dom->createTextNode('书名不存在或错误');
                $result->appendChild($result_Text);
                $book->appendChild($result);
    
            }
    
        }else{//如果key不存在
            //建立<error_code>,并追加error_code的值
            $error_code=$dom->createElement('error_code');
            $error_code_Text=$dom->createTextNode('错误代码1002');
            $error_code->appendChild($error_code_Text);
            $book->appendChild($error_code);
            
            //建立<result>,并追加result的值
            $result=$dom->createElement('result');
            $result_Text=$dom->createTextNode('密钥错误或不存在');
            $result->appendChild($result_Text);
            $book->appendChild($result);
        }
    
        //在一字符串变量中建立XML结构
        $xmlText=$dom->saveXML();
        
        //输出XML字符串
        echo $xmlText;
    ?>
    

    传参key=12345,name=C语言。  

    然后输出的结果是:

    这里是用户用javascript调用xml文件

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" lang="zh-CN">
    <head>
    <title>获取数据</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="description" content="" />
    <meta name="keywords" content="" />
    <script type="text/javascript">
    window.onload=function(){     
            var btn=document.getElementById('btn');
            btn.onclick=function(){
                var title=document.getElementById('title').value;      
                var name,author,press,price,book;
                if(window.XMLHttpRequest){
                    //code for IE+7,Firefox,Chrome,Opera,Safari
                    xmlhttp=new XMLHttpRequest();
                }else{
                    //code foe IE6,IE5
                    xmlhttp=new ActiveXObject("Microsoft.XMLHttp");
                }
                url='http://localhost/api/index.php?key=12345&name='+title;
                //console.log(url);
                xmlhttp.open("GET",url,false);
                xmlhttp.send();
                xmldoc=xmlhttp.responseXML;           
                book=xmldoc.getElementsByTagName("book");
                error_code=xmldoc.getElementsByTagName("error_code");
                if(error_code.length==1){
                    alert("书名错误或不存在");           
                }
                name=xmldoc.getElementsByTagName("name")[0];
                author=xmldoc.getElementsByTagName("author")[0];
                press=xmldoc.getElementsByTagName("press")[0];
                price=xmldoc.getElementsByTagName("price")[0];
                text="<table border='1'><tr><th>书名</th><th>作者</th><th>出版社</th><th>价格</th></tr>";
                text+="<tr>";
                text+="<td>"+name.firstChild.nodeValue+"</td>";
                text+="<td>"+author.firstChild.nodeValue+"</td>";
                text+="<td>"+press.firstChild.nodeValue+"</td>";
                text+="<td>"+price.firstChild.nodeValue+"</td>";
                text+="</tr>";
                text+="</table>";
                document.getElementById("text").innerHTML=text;
            } 
    }
    </script>
    <style type="text/css">
    </style>
    </head>
        <body>
        书名:<input type="text" name="title" id="title"/>
        <input type="submit" id="btn" value="查询"/>
        <div id="text"></div>
        </body>
    </html>
    

      

    这里是接口开发的相关代码:

    http://download.csdn.net/detail/yxhbk/9506715

    这里是接口开发说明文档:

    http://wenku.baidu.com/view/14706f8369eae009591bec44

  • 相关阅读:
    location 匹配规则
    nginx+keepalived 高可用方案
    Nginx 静态文件服务
    web服务器-nginx优化
    Oracle 修改字符集(AL32UTF8 转换成UTF8字符集)
    xshell复制粘贴
    关于mysql中的DDL,DML,DQL和DCL
    LVS实现Kubernetes集群高可用
    k8s实践(一):Centos7.6部署k8s(v1.14.2)集群
    Centos7.6部署k8s v1.16.4高可用集群(主备模式)
  • 原文地址:https://www.cnblogs.com/yxhblogs/p/5446805.html
Copyright © 2011-2022 走看看