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. :)

4

2 回答 2

0

你自己的代码已经有了你需要的东西,值在你的$system_ref变量中,只是回显它。我已经根据你的代码编写了这个代码片段,它会输出你想要的结果:

<?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;

输出:

34324242
于 2013-05-18T21:45:25.497 回答
0

最快的方法是:

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

也看看 PHP DOM 模块,但上面是最快的。

于 2013-05-18T18:59:55.803 回答