2017-07-27 91 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