简体   繁体   English

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

[英]How to parse string to XML in curl using CodeIgniter?

I want to parse simple text into XML format. 我想将简单的文本解析为XML格式。 I tried lots of thing to do it but I am not able to do it. 我尝试了很多事情来做,但是我做不到。

I have tried below code: 我试过下面的代码:

<?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');
}
}

I am getting this output after running this code. 运行此代码后,我得到此输出。

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

but my requirement is something like this : 但我的要求是这样的:

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

How can I get this output? 如何获得此输出?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM