0

I have tried following the advice in this thread, but I still seem to be having trouble getting a usable array from a raw XML CURL response (actually I just want a couple of bits of data from the output)...

Here is my code:

echo "Curl started...<br />\n\n";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
$response=curl_exec ($ch);
curl_close ($ch);

$response_xml = simplexml_load_string($response);
$system_ref = $response_xml->OBJECT->SYSTEMREFERENCE; 

echo "Curl finished...<br />\n\n";

print_r($response_xml);

Here is the raw response which is fed into $response :

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE IAD.IF.OBJECTRESPONSE SYSTEM "http://www.someplace.com/dtd/IADIF-objectresponse.dtd">
<IAD.IF.OBJECTRESPONSE>
<HEAD>
  <PARTNER>somepartner</PARTNER>
  <UPLOAD_REFERENCE/>
  <IMPORTTYPE>STORING AD</IMPORTTYPE>
  <PROCESSEDTIME>2013.05.17 11:29:29</PROCESSEDTIME>
  <SOURCE>originalsource_342423424.xml</SOURCE>
</HEAD>
<OBJECT STATUS="ok">
  <ORDERNO/>
  <VERSION></VERSION>
  <USERREFERENCE></USERREFERENCE>
  <SYSTEMREFERENCE>34324242</SYSTEMREFERENCE>
</OBJECT>
</IAD.IF.OBJECTRESPONSE>

So, if I echo or print_r $response, I get a raw string output... but if I print_r $response_xml (as in the example above) , I don't seem to get anything.

For the moment, my goal is to simply get the SYSTEMREFERENCE data into a variable...

Can anyone please advise?

---------

PS. Been lurking a while but this seems to be my first question on Stackoverflow, so please go easy on me. Hope I've done it right. :)

Community
  • 1
  • 1
Alek
  • 33
  • 1
  • 4

2 Answers2

0

Quickest way is:

preg_match( '/<SYSTEMREFERENCE>(.+?)<\/SYSTEMREFERENCE>/i', $str, $res );
print_r( $res );

Also look at PHP DOM module, but the above is the quickest.

Kohjah Breese
  • 4,008
  • 6
  • 32
  • 48
  • This is almost working but it is producing strange results. If I print_r($res), I get an array of data with just the systemreference data.. If I then, for example, do $systemref = $res[0], when I echo $systemref the first time it echoes just the data, but when I echo it later or in other functions, it comes back with the tags in place too - 34324242 Any ideas?? – Alek May 27 '13 at 11:30
0

Your own code already has what you need, the value is in your $system_ref variable, just echo it. I've written this code snippet based on yours and it outputs the result you're after:

<?php
$response = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE IAD.IF.OBJECTRESPONSE SYSTEM "http://www.someplace.com/dtd/IADIF-objectresponse.dtd">
<IAD.IF.OBJECTRESPONSE>
    <HEAD>
        <PARTNER>somepartner</PARTNER>
        <UPLOAD_REFERENCE />
        <IMPORTTYPE>STORING AD</IMPORTTYPE>
        <PROCESSEDTIME>2013.05.17 11:29:29</PROCESSEDTIME>
        <SOURCE>originalsource_342423424.xml</SOURCE>
    </HEAD>
    <OBJECT STATUS="ok">
        <ORDERNO />
        <VERSION />
        <USERREFERENCE />
        <SYSTEMREFERENCE>34324242</SYSTEMREFERENCE>
    </OBJECT>
</IAD.IF.OBJECTRESPONSE>
XML;

$response_xml = new SimpleXMLElement($response);
$system_ref   = $response_xml->OBJECT->SYSTEMREFERENCE; 

echo $system_ref;

Output:

34324242
Rolando Isidoro
  • 4,983
  • 2
  • 31
  • 43
  • This works with your example, but for some reason doesn't with mine. I can only assume that my $response is not actually holding the data I am seeing... or in some other kind of format... :( – Alek May 27 '13 at 10:33
  • Looking further, it's definitely the case that SimpleXMLElement is not liking my $response and just dies (actually gives some kind of 404 output??). Actually, if I print_r($response); , the response gets printed out in one big line with lots of whitespaces between the items. Could that be the problem? – Alek May 27 '13 at 10:44
  • From [SimpleXMLElement _construct documentation](http://www.php.net/manual/en/simplexmlelement.construct.php): ***Errors/Exceptions - Produces an E_WARNING error message for each error found in the XML data and additionally throws an Exception if the XML data could not be parsed.*** Look at your PHP log, it should tell you what are the problems with your XML if any. – Rolando Isidoro May 27 '13 at 15:44