【问题标题】:CI, how to make xml code in controller?CI,如何在控制器中制作xml代码?
【发布时间】:2013-07-11 08:36:36
【问题描述】:

实际上我正在编写 Ajax 并想制作一个 控制器文件中的xml代码。

怎么可能写xml代码 在控制器文件中?

【问题讨论】:

    标签: php xml codeigniter


    【解决方案1】:

    您想要的样本: 在您的 CI 控制器中:

    class XML extends CI_Controller {
    
     public function my_function(){
       //your xml code here
    
       echo $xml_code; //echo your output
     } 
    }
    

    在你的脚本中你可能会这样写:

    $(function() {
    
      $.ajax({
       url: '/XML/my_function/',
       type: 'POST', 
       dataType: 'string',
       success: function(response){
         //do what you want for response
       });
    });
    

    【讨论】:

    • 为什么dataType是xml以外的字符串?
    • 而且我还必须编写一个 xml 代码并将其提供给变量 $xml_code 对吗?
    • @user2522245 ,是的,您需要将它传递给变量并回显它。如果你有萤火虫,你可以在控制台中检查你的 ajax 的响应。然后尝试探索让你学习 :)
    • 非常感谢 :) 终于明白了~
    【解决方案2】:

    检查以下链接。

    http://ellislab.com/codeigniter/user-guide/libraries/xmlrpc.html
    
    https://github.com/EllisLab/CodeIgniter/wiki/XML-generator-library
    

    【讨论】:

      【解决方案3】:

      是的,可以在控制器中生成 xml 文件:

      只需在变量中创建 xml

           $xml_builder = '<?xml version="1.0" encoding="utf-8"?>     
                                          <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"  xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">  
                                          <soap:Body>     
                                          <Transact xmlns="http://Exxxx/xxxTopUp">    
                                                      <LoginInfo>     
                                                              <AccountID>' . $account_id . '</AccountID>  
                                                              <Username>' . $username . '</Username>  
                                                              <Password>' . $password . '</Password>  
                                                              <BranchID>' . $branch_id . '</BranchID>     
                                                      </LoginInfo>    
                                                      <Telco>' . $telco . '</Telco>   
                                                      <CellphoneNo>' . $cellphone_no . '</CellphoneNo>    
                                                      <ExtTag>' . $Ext_tag . '</ExtTag> 
                                                      <Amount>' . $amount . '</Amount>
                                                      <Token>' . $token . '</Token> 
                                      </Transact>     
                                      </soap:Body>    
                                      </soap:Envelope>';
      

      然后我们使用带有 text/xml 的 http 标头的 POST 通过 CURL 发送 XML。

          $url = "http://xxxxxxxxx.com/xxxtopup/";
      
      
      
          $ch = curl_init();
          curl_setopt($ch, CURLOPT_URL, $url);
          curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
          curl_setopt($ch, CURLOPT_HEADER, 0);
          curl_setopt($ch, CURLOPT_POST, true);
          curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_builder);
          curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
      
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
          $ch_result = curl_exec($ch);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-06-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多