You are here:  » CURL XML parsing


CURL XML parsing

Submitted by derek on Tue, 2005-12-20 22:08 in

Is it possible to parse the XML like this:

$ch = curl_init('http://www.whatever.com/abc.xml');

curl_setopt(...)
:
:

$content = curl_exec($ch);

MagicParser($content,"myRecordHandler");

function myRecordHandler($record){
... print_r($record);
}

It does not work, any ideas ?

Submitted by support on Tue, 2005-12-20 22:13

Hi,

As curl is just a content retrieval mechanism there is no reason why this should not work. Housekeeping out of the way first, it may be a copy'n'paste error but I notice that the call to MagicParser_parse() is missing the _parse component...! You need:

MagicParser_parse($content,"myRecordHandler");

(additional required part in bold)

That aside, the thing to do here is to debug by inspecting the value of $content after your call to curl_exec() to ensure that you have retrieved a valid XML document. I would do something like this:

<?php
  
// ... rest of your script ...
  
$content curl_exec($ch);
  print 
"<textarea rows='10' cols='80'>";
  print 
htmlentities($content);
  print 
"</textarea>";
  exit();
?>

This will enable you to prove visually that you have a valid XML document before trying to do the parse.

Hope this helps!
David.

Submitted by derek on Tue, 2005-12-20 22:33

<?php
error_reporting
(E_ALL);
set_time_limit(0);
require(
"MagicParser.php");
///// grab the XML file from a dedicated URL ////////////////
//$today = date('YmD');
$today "20051020";
$ch curl_init('[snip]');
curl_setopt $chCURLOPT_HEADER);
curl_setopt$chCURLOPT_RETURNTRANSFER1);
curl_setopt$chCURLOPT_SSL_VERIFYPEER0);
curl_setopt$chCURLOPT_SSL_VERIFYHOST0);
$content curl_exec $ch );
curl_close $ch );
echo 
$content// this echo out some XML data
$xml = array();
MagicParser_parse($content,"myRecordHandler");
  function 
myRecordHandler($record){
        global 
$xml;
        
$xml[] = $record;
}
echo 
"<pre>";
print_r($xml);   // this return nothing
echo "</pre>";
?>

Submitted by support on Tue, 2005-12-20 22:35

Can you post a snippet of the value of $content as this will help understand what the parser has made of it....

Submitted by derek on Tue, 2005-12-20 22:39

put this in IE, and it will show some XML,
that's what I am getting in that curl_init
thanks for your help

https://208.210.197.183/ahe/webservices/CustomerWebService.aspx?username=getPica&password=&lastupdatedate=20051020&action=GetCustomer

Submitted by support on Tue, 2005-12-20 22:42

Ok, the Parser is fine with it...

http://www.magicparser.com/demo?fileID=43A88883017A3&record=1
(will be deleted shortly)

So, something else is the problem, i'll have another look at your code...

Submitted by support on Tue, 2005-12-20 22:45

The next debug step I would take is to set a break point in your record handler function; so you would have:

<?php
  
function myRecordHandler($record)
  {
    
dummy();
  }
?>

This will cause your script to fail with unknown function dummy(). This will prove that the parser is working and calling myRecordHandler()...

Submitted by derek on Tue, 2005-12-20 22:45

$xml = array();
MagicParser_parse($content,"myRecordHandler");
function myRecordHandler($record){
global $xml;
$xml[] = $record;
}
echo "";
print_r($xml); // this return nothing
echo "";

The code is short, only few lines, it is strange,
please help

Submitted by support on Tue, 2005-12-20 22:47

I see the problem....

You are passing a string value to MagicParser - it is expecting a filename (which can be a local file or a URL).

What you need to do is write the output to a file and then pass the filename of the local temporary file to MagicParser. I'll put together some code to do that, check back here in a few minutes...

Submitted by support on Tue, 2005-12-20 22:53

Have you created temporary files before? You will need a directory that has write access. It may be that /tmp is writable on your system, so I would try something like this:

Please study the code I have added between your curl retrieval and the call to MagicParser - it will write try to write a file to the hard disk of your server so please understand what the code is doing first! You will also need to replace [SNIP] with the correct URL of course. I suggest that you just study this code and add the changes necessary to your current version.

<?php
error_reporting
(E_ALL);
set_time_limit(0);
require(
"MagicParser.php");
///// grab the XML file from a dedicated URL ////////////////
//$today = date('YmD');
$today "20051020";
$ch curl_init('[snip]');
curl_setopt $chCURLOPT_HEADER);
curl_setopt$chCURLOPT_RETURNTRANSFER1);
curl_setopt$chCURLOPT_SSL_VERIFYPEER0);
curl_setopt$chCURLOPT_SSL_VERIFYHOST0);
$content curl_exec $ch );
curl_close $ch );
echo 
$content// this echo out some XML data
// create a temporary filename
$tempfile "/tmp/test.xml";
// open the temporary file with write access
$fp fopem($tempfile,"w");
// write the XML to the temporary file
fwrite($fp,$content);
// close the file
fclose($fp);
// now pass the temporary file to Magic Parser
$xml = array();
MagicParser_parse($tempfile,"myRecordHandler");
  function 
myRecordHandler($record){
        global 
$xml;
        
$xml[] = $record;
}
echo 
"<pre>";
print_r($xml);   // this return nothing
echo "</pre>";
?>