You are here:  » Using cURL and outputting HTML


Using cURL and outputting HTML

Submitted by monkeythumpa on Sat, 2007-06-02 00:14 in

I cannot figure out what I am doing wrong. I get a blank page when I run this script:

<?php
error_reporting
(E_ALL);
set_time_limit(0);
require(
"MagicParser.php");
///// grab the XML file from a dedicated URL ////////////////
$today date('YmD');
$ch curl_init('{URL SAVED}');
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 );
$xml = array();
  function 
myRecordHandler($record)
  {
    print 
"<p>Title: ".$record["TITLE"]."</p>";
  }
MagicParser_parse($content,"myRecordHandler""xml|bcom/bcomresults/results");
?>

Submitted by support on Sat, 2007-06-02 07:07

Hi,

By default, MagicParser_parse() expects a filename parameter to be provided, so you would normally use either a local file such as "data.xml" or a URL such as "http://www.example.com/feed.xml".

As you have already downloaded the XML into the $content variable, you need to use the built in string:// prefix to tell Magic Parser to process the data as a string and not a filename. It should work if you change your call to Magic Parser as follows:

MagicParser_parse("string://".$content,"myRecordHandler", "xml|bcom/bcomresults/results");

If you still don't get any output, the first step is to verify that your curl code has obtained the XML correctly. You could do this simply by printing the content...

print $content;

...but you would have to use View > Source in your browser to verify that it was downloaded correctly as you probably wouldn't see anything because the XML tags would not be displayed...

Hope this helps,
Cheers,
David.

Submitted by monkeythumpa on Sun, 2007-06-03 21:59

Yes $content contains the XML file but for some reason MagicParser is not doing anything with it. I am not even seeing the "Title:" HTML being printed out.

The file is in the correct location, the new "string://" code is in as you gave the example. The XML is going into your script but nothing seems to be coming out. Any other suggestions? fopen is disabled so that is why I need to cURL it.

Submitted by support on Mon, 2007-06-04 06:24

Hi,

My apologies for not spotting this earlier, but the problem is down to the format string that you are using. Firstly, a Magic Parser format string should end in "/" to indicate the end of the path at which you want the parse to extract the repeating records. With the business.com feed, if you want to extract the TITLE element of each result then you would need to use the following format string:

xml|bcom/bcomresults/results/result/

If you omit the final "result/", you will still obtain the data, but the $record parameter in your record handler function would contain every record in the feed, resolved using the @n notation, which would then need to be broken down manually.

Your cURL code should be working fine. I've written the following test script to demonstrate the above, printing out the TITLE elemenet of each record - but rather than pass the URL directory to Magic Parser i've written code to download the XML into a variable called $content just as you are doing in your code. Simple replace this with your cURL code and it should work fine.

business.php:

<?php
  
require("MagicParser.php");
  function 
myRecordHandler($record)
  {
    print 
"<p>Title: ".$record["TITLE"]."</p>";
  }
  
// replace the following code with your cURL code
  
$u "{URL SAVED}";
  
$f fopen($u,"r");
  while(!
feof($f)) $content .= fread($f,1024);
  
fclose($f);
  
// XML is now in $content variable
  
MagicParser_parse("string://".$content,"myRecordHandler","xml|bcom/bcomresults/results/result/");
?>

Here's the script running on this server:

http://www.magicparser.com/examples/business.php

Hope this helps,
Cheers,
David.

Submitted by monkeythumpa on Mon, 2007-06-04 16:10

That works, thanks so much.