实际上我正在编写 Ajax 并想在控制器文件中创建一个 xml 代码。
怎么可能在控制器文件中写xml代码?
您想要的示例:在您的 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
   });
});
    是的,可以在控制器中生成 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>';
然后我们通过 CURL 使用带有 text/xml 的 http 标头的 POST 发送 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);
    检查以下链接。
http://ellislab.com/codeigniter/user-guide/libraries/xmlrpc.html
https://github.com/EllisLab/CodeIgniter/wiki/XML-generator-library