【问题标题】:How to parse string to XML in curl using CodeIgniter?如何使用 CodeIgniter 在 curl 中将字符串解析为 XML?
【发布时间】:2017-07-27 20:08:05
【问题描述】:

我想将简单文本解析为 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>

我怎样才能得到这个输出?

【问题讨论】:

    标签: php xml codeigniter curl


    【解决方案1】:
    $this->output->set_content_type('text/xml', 'UTF-8');    
    print($xml);
    

    【讨论】:

    • 我收到此错误:调用字符串上的成员函数 asXML()
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-02
    • 1970-01-01
    • 2012-04-08
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多