3

So I am trying to get a XML response after calling a URL with params (GET request). I found this code below, which is working.

$url = "http://...";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt($ch, CURLOPT_ENCODING, "gzip,deflate");     
$response = curl_exec($ch);
curl_close($ch);
echo $response;

But as response I am getting a huge string with no commas (so I cannot explode it). And this string has only values, no keys.

Is there a way to get an associative array instead?

The XML is like:

<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>    
<transaction>    
    <date>2011-02-10T16:13:41.000-03:00</date>    
    <code>9E884542-81B3-4419-9A75-BCC6FB495EF1</code>    
    <reference>REF1234</reference>  
    <type>1</type>    
    <status>3</status>    
    <paymentMethod>    
        <type>1</type>    
        <code>101</code>    
    </paymentMethod>    
    <grossAmount>49900.00</grossAmount>    
    <discountAmount>0.00<discountAmount>
    (...)  

SO I would like to have an array like:

date => ...
code => ...
reference => ...
(and so on)

Is that possible? If so, how?

EDIT: I don´t agree with the "this questions is already answered" tag. No code found on the indicated topic solved my issue. But, anyhow, I found a way, with the code below.

$url = http://...;    
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$transaction= curl_exec($curl);
curl_close($curl);

$transaction = simplexml_load_string($transaction);
var_dump($transaction); //retrieve a object(SimpleXMLElement)
Mauro Lopes
  • 75
  • 1
  • 7
  • 2
    You can use this http://php.net/manual/en/function.xml-parse-into-struct.php – Ôrel Apr 30 '15 at 11:19
  • there is 2 way, easiest is put everything in array and send by json encode. and another is you have to generate xml by adding the headers and content. – Sundar Bons Apr 30 '15 at 11:19
  • Orel, thanks but I forgot to mention that the string I am receiving has only values, not keys. – Mauro Lopes Apr 30 '15 at 12:02
  • Sundar, thanks, but how this would fit in my code? And I am receiving only. I cannot change anything made by the sender. – Mauro Lopes Apr 30 '15 at 12:04
  • you can parse the xml, it is quite simple. see http://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php – michi Apr 30 '15 at 13:20
  • Thanks, michi, but I am getting a big string with only values, not keys. Can you fix my code so I can get XML and then parse it? – Mauro Lopes Apr 30 '15 at 14:09
  • There is no code shown in your question (about the part you ask about), so it is pretty broad. So the general question remains how to parse and process XML with PHP. That question has been already answered before, please relate to existing material (there is even more than that reference question and perhaps you can make more concrete what you actually want to ask about in the sense of programming it). See as well http://stackoverflow.com/help - Otherwise you wonder why every answer you get does not answer *"your"* question. – hakre Apr 30 '15 at 14:38

2 Answers2

0

I use something like this (very universal solution):

http://www.akchauhan.com/convert-xml-to-array-using-dom-extension-in-php5/

Only thing is I exclude the attributes part as I don't need them for my cases

<?php
class xml2array {

    function xml2array($xml) {
        if (is_string($xml)) {
            $this->dom = new DOMDocument;
            $this->dom->loadXml($xml);
        }

        return FALSE;
    }

    function _process($node) { 
        $occurance = array();

        foreach ($node->childNodes as $child) {
            $occurance[$child->nodeName]++;
        }

        if ($node->nodeType == XML_TEXT_NODE) { 
            $result = html_entity_decode(htmlentities($node->nodeValue, ENT_COMPAT, 'UTF-8'), 
                                 ENT_COMPAT,'ISO-8859-15');
        } else {
            if($node->hasChildNodes()){
                $children = $node->childNodes;

                for ($i=0; $i < $children->length; $i++) {
                    $child = $children->item($i);

                    if ($child->nodeName != '#text') {
                        if($occurance[$child->nodeName] > 1) {
                            $result[$child->nodeName][] = $this->_process($child);
                        } else {
                            $result[$child->nodeName] = $this->_process($child);
                        }
                    } else if ($child->nodeName == '#text') {
                        $text = $this->_process($child);

                        if (trim($text) != '') {
                            $result[$child->nodeName] = $this->_process($child);
                        }
                    }
                }
            } 
        }

        return $result;
    }

    function getResult() {
        return $this->_process($this->dom);
    }
}
?>

And call it from your script like this:

$obj = new xml2array($response);
$array = $obj->getResult();

The code is very self explanatory, Objective approach and it can easily be modified to exclude or include parts at desire.

simply load XML into DOM Object, then recursively check for children and fetch respective values.

Hope it helps

Slobodan Antonijević
  • 2,533
  • 2
  • 17
  • 27
  • Man, I don't know how to use your code to say it works or not. As I said, I have this string, with all the values from the XML (and without the keys). So my goal is to modify the code I am using to have an associative array instead. – Mauro Lopes Apr 30 '15 at 13:49
0

I have had good luck using code like this:

$url = "http://feeds.bbci.co.uk/news/rss.xml";
$xml = file_get_contents($url);

if ($rss = new SimpleXmlElement($xml)) {
    echo $rss->channel->title;
}
Ian Morse
  • 56
  • 3
  • 3
    this is not good luck, but using the `SimpleXML` parser – michi Apr 30 '15 at 13:21
  • You're right @michi, the link you posted in comments is very good. I was trying to add a snippet that would get the poster working quickly. – Ian Morse Apr 30 '15 at 13:43
  • Well, it did not work. I think the reason is because my URL does not end with ".xml", as in your code (it has params). – Mauro Lopes Apr 30 '15 at 13:56