TinyPortal

Development => Support => Topic started by: mrandall131 on May 01, 2007, 07:39:22 PM

Title: Using APIs
Post by: mrandall131 on May 01, 2007, 07:39:22 PM
Ok, I beling to a couple sites that have APIs all set for me to pull info from.  Has anyone had experience with them?  I haven't tried yet, but would like to learn.  Got any good sites to reference on how to handle info with respect to APIs?
Title: Re: Using APIs
Post by: eldacar on May 02, 2007, 10:41:32 AM
Depends entirely on each API. They could be completely different.
Title: Re: Using APIs
Post by: mrandall131 on May 02, 2007, 01:29:48 PM
For example, http://www.360voice.com/api/games-listfav.asp?tag=muddyeiger&num=10&sort=3 is one of the APIs I'd like to use in an article/block on my gaming site.
Title: Re: Using APIs
Post by: mrandall131 on May 02, 2007, 08:37:20 PM
Better yet, this is the test code that works when it's in a block, but doesn't when in an article.  Must be the quotes vs apostrophe?

Quote from: code
<?php

$name_v = array();
$points_v = array();

$tag = "MuddyEiger";
$file = "http://www.360voice.com/api/games-listfav.asp?tag=MuddyEiger&num=10";
$open = $file;
if (!($xml = domxml_open_file($open,
  DOMXML_LOAD_PARSING + //0
  DOMXML_LOAD_COMPLETE_ATTRS + //8
  DOMXML_LOAD_SUBSTITUTE_ENTITIES + //4
  DOMXML_LOAD_DONT_KEEP_BLANKS
  ,$error))) {
  echo "Document Could not be Read.
".$file."
".$tag."
";
  exit;
}

$xmldoc = domxml_open_mem($xml,
  DOMXML_LOAD_PARSING + //0
  DOMXML_LOAD_COMPLETE_ATTRS + //8
  DOMXML_LOAD_SUBSTITUTE_ENTITIES + //4
  DOMXML_LOAD_DONT_KEEP_BLANKS
  );
$elements = $xml->get_elements_by_tagname("game");


for($i = 0; $i < count($elements); $i++) {
  $element = $elements[$i];
  $child = $element->first_child();
  while($child) {
   if ($child->tagname == 'name') { $name_v[] = $child->get_content(); }
   if ($child->tagname == 'currentgs') {$points_v[] = $child->get_content(); }
   if ($child->tagname == 'totalgs') {$totals_v[] =  $child->get_content(); }
   if ($child->tagname == 'tile') {$tiles_v[] = $child->get_content(); }
   $child = $child->next_sibling();
   //echo $child->tagname . "
";
  }
}
echo $name_v[0];
echo "
<img src='".$tiles_v[0]."'>";
xml_parser_free($xmldoc);

?>
Title: Re: Using APIs
Post by: JPDeni on May 03, 2007, 01:36:00 AM
Quotethis is the test code that works when it's in a block, but doesn't when in an article
You don't say how it doesn't work, which makes it pretty difficult to figure out what the problem is. However, the obvious thing is that you start with


<?php


and end with


?>


You have to remove those, for both blocks and articles.
Title: Re: Using APIs
Post by: mrandall131 on May 03, 2007, 05:35:52 AM
Ok, for both, I have those removed.  I'll leave the article up and running on my site, www.KillingAuthority.com  You can see the article "Test" at the top of the main page.  That's obviously wrong.  It should look like the block on the right column, last one, titled "MuddyEiger's Top 10 Games"
Title: Re: Using APIs
Post by: JPDeni on May 03, 2007, 06:06:59 AM
It looks like you have it in a regular article and not a php article.
Title: Re: Using APIs
Post by: mrandall131 on May 03, 2007, 11:16:00 AM
Yeah, that's what I thought too, but in the article manager, it has "PHP" next to it in parenthesis.  Is it possible that it's not recognizing it as PHP for some other reason?

[edit]
Took a snapshot of the article in the manager, here it is.
Title: Re: Using APIs
Post by: vanguard on May 03, 2007, 11:43:52 AM
Anyone use the YouTube API?
Title: Re: Using APIs
Post by: G6Cad on May 03, 2007, 11:48:06 AM
mrgray, please start your own thread with your question ;)
Title: Re: Using APIs
Post by: JPDeni on May 03, 2007, 03:42:16 PM
mrandall131, just to be sure about your article type, delete the article you have and create a new one, being sure that you create a php article from the start. Sometimes there are problems when you start with an html article and then switch to a php article.

If you still have problems, someone else will try to figure it out with you.
Title: Re: Using APIs
Post by: mrandall131 on May 03, 2007, 03:48:55 PM
Ok, just did that.  Deleted the article, went into the article manager, clicked add php, added a title, pasted this code into it


define('XML_DECLARATION', '<?xml version="1.0" encoding="iso-8859-1"?>' . "\n");

$name_v = array();
$points_v = array();


$tag = "MuddyEiger";
$file = "http://www.360voice.com/api/games-listfav.asp?tag=MuddyEiger&num=10";
$open = $file;
if (!($xml = domxml_open_file($open,
  DOMXML_LOAD_PARSING + //0
  DOMXML_LOAD_COMPLETE_ATTRS + //8
  DOMXML_LOAD_SUBSTITUTE_ENTITIES + //4
  DOMXML_LOAD_DONT_KEEP_BLANKS
  ,$error))) {
  echo "Document Could not be Read.<br />".$file."<br />".$tag."<br />";
  exit;
}

$xmldoc = domxml_open_mem($xml,
  DOMXML_LOAD_PARSING + //0
  DOMXML_LOAD_COMPLETE_ATTRS + //8
  DOMXML_LOAD_SUBSTITUTE_ENTITIES + //4
  DOMXML_LOAD_DONT_KEEP_BLANKS
  );
$elements = $xml->get_elements_by_tagname("game");


for($i = 0; $i < count($elements); $i++) {
  $element = $elements[$i];
  $child = $element->first_child();
  while($child) {
   if ($child->tagname == 'name') { $name_v[] = $child->get_content(); }
   if ($child->tagname == 'currentgs') {$points_v[] = $child->get_content(); }
   if ($child->tagname == 'totalgs') {$totals_v[] =  $child->get_content(); }
   if ($child->tagname == 'tile') {$tiles_v[] = $child->get_content(); }
  if ($child->tagname == 'percentcomplete') {$complete_v[] = $child->get_content(); }
   $child = $child->next_sibling();
   //echo $child->tagname . "<br />";
  }
}

for($i = 0; $i < count($elements); $i++) {
echo $name_v[$i];
echo "<br /><img src='".$tiles_v[$i]."'>";
echo "<br />Current Score: ".$points_v[$i]."/".$totals_v[$i];
echo "<br />Percent Complete: ".$complete_v[$i]."<br /><br />";
}

xml_parser_free($xmldoc);


and this is what the article looked like on the front page.
Title: Re: Using APIs
Post by: JPDeni on May 03, 2007, 05:17:00 PM
Now delete the code. Save the page. Paste the code in again. Save it again. See what happens.

That's the last thing that I can think of.
Title: Re: Using APIs
Post by: mrandall131 on May 03, 2007, 05:20:55 PM
Tried, about 5 times so far.  For some reason, it's not seeing it as PHP.  For now, I created a blank article.  Then, I created a block with the PHP in it, as it works in a block.  Then, I made the block display when the blank article is clicked on.  Round about way, but at least it does what I need it to.

http://www.killingauthority.com/index.php?page=13