2017-07-27 97 views
0

我想将简单的文本解析为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
$this->output->set_content_type('text/xml', 'UTF-8');  
print($xml); 
+0

我收到此错误:对字符串 –

+0

谢谢你这么多,我解决我的问题调用一个成员函数asXML()...... –

+0

欢迎您 – Oluwaseye