如何使用CodeIgniter将字符串解析为curl中的XML?

时间:2017-07-27 20:08:06

标签: php xml codeigniter curl

我想将简单文本解析为XML格式。我尝试了很多事情,但我无法做到。

我试过下面的代码:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Home extends CI_Controller {

public function index()
{
    $url = "https:myURL";
    $data = array("username" => "USERID");
    //$ctype = array("Content-Type" => "text/xml");
    $this->curl->create($url);
    //$this->curl->http_header("Content-Type", "application/xml");
    $this->curl->http_header("Accept", "application/xml");
    //$this->curl->http_header("charset", "UTF-8");
    $this->curl->option(CURLOPT_HTTPHEADER, array('Content-type: 
    application/xml-dtd'));
    $this->curl->post($data);
    $xml = $this->curl->execute();
    $xml = new SimpleXMLElement($xml);
    print_r($xml);
    exit;
    $this->load->view('home_view');
}
}

运行此代码后,我收到此输出。

SimpleXMLElement Object ( [responseId] => 290 [status] => SUCCESS [result] => SimpleXMLElement Object ( [seed] => 426477071456611 ) )

但我的要求是这样的:

<?xml version="1.0" encoding="utf-8"?>
<response>
   <responseId>246</responseId>
   <status>SUCCESS</status>
<result>
    <seed>292174305068328</seed>
</result>
</response>

如何获得此输出?

1 个答案:

答案 0 :(得分:1)

$this->output->set_content_type('text/xml', 'UTF-8');    
print($xml);