Page 1 of 1

use js to get realtime data from another site(HowHotAmI.com)

Posted: Thu Feb 13, 2003 12:13 am
by Phil
Hi,

I run sfnitelife.com, its a php driven 'vote for the hottest party' site.
I want to be able to let users put a code snipet on their site that will display realtime party voting data (the score from 1-10 and the amount of votes, you know the whole hotornot thing) from my site in real time.

...so a user would put the below code on their site (I'm using HowHotAmI.com as an example):

Code: Select all

<!-- HowHotAmI.com code start -->
<SCRIPT LANGUAGE= "javaScript" SRC="http://howhotami.com/track.php?pic=MTgxMjk="></script>
<!-- HowHotAmI.com code end -->
...and then the question becomes...What does this track.php file look like?

Is track.php JavaScript and PHP? If its just JavaScript (as the

Code: Select all

SCRIPT LANGUAGE= "javaScript"
would make apparent) then how do I call the MySQL db?

From what I can see, the script uses document.write() to output the data if you shorten the url and use:

http://howhotami.com/track.php

Has anyone any code to do this?

cheers,
Phil

Hello

Posted: Thu Feb 13, 2003 4:32 am
by crazyjimsmith
I got this code from slash dot and it gives me a live news feed. What they have used is xml and php. There are php functions that can get xml data from another server.

Anyway here is my site

http://www.bohemeinteractive.com.au

and here is the script that was used

Code: Select all

<?php

/**************************************************
*                                                 *
*  Name: Slashdot Headline Grabber                *
*  Version: 2.0                                   *
*  Release Date: 06/08/2002                       *
*  Written By: Heath C. Ice                       *
*              Nonec Technologies                 *
*              http://www.nonec.com               *
*                                                 *
***************************************************
*                                                 *
* This function will simply grab the headlines    *
* from Slashdot.org and display them on your      *
* website.  The headlines are updated the first   *
* time the page is viewed each 30 minutes.        *
*                                                 *
* Setting up the headline grabber is simple.      *
* First, the server your page is hosted on must   *
* recognize PHP Scripts.  Second, on the page     *
* you want the headlines add the following to the *
* top of the page:<?php require 'slashdot.php'; \?>*
* \Then at the place in the page where you want    *
* the actual headlines displayed put:             *
* <?php Slashdot(); \?>                            *
*                                                 *
* View "slash.php" for an example on using the    *
* code.                                           *
*                                                 *
* Send any questions or comments to               *
* programming@nonec.com                           *
*                                                 *
**************************************************/


$tags = array(
	'STORY' => '<STORY>',
    'STORY' => '</STORY>',
    'TITLE' => '<TITLE>',
    'TITLE' => '</TITLE>',
    'URL' => '<URL>',
    'URL' => '</URL>');


function startElement($parser, $tag, $attrs='')
&#123;
	return;
&#125;

function endElement($parser, $tag, $attrs='')
&#123;
    global $tags, $temp, $current, $fp2;
	if($tag == 'TITLE')
	&#123;
		$temp&#1111;'title'] = $temp&#1111;'data'];
		$temp&#1111;'data'] = "";
	&#125;else if($tag == 'URL')
	&#123;
		$temp&#1111;'url'] = $temp&#1111;'data'];
		$temp&#1111;'data'] = "";
	&#125;else if($tag == 'STORY')
   	&#123;
		$bob = "<a href='".$temp&#1111;'url']."' target='_blank'>".$temp&#1111;'title']."</a>\n";
		fwrite($fp2, $bob);
		$temp&#1111;'url'] = "";
	 	$temp&#1111;'title'] = "";
		$temp = '';
   	&#125;
&#125;

function Data($parser, $data)
&#123;
    global $temp;
    if($data>"!" && $data<"ΒΆ")  // this takes care of all of the extra tabs and other stuff that we dont want
    &#123;
    	$temp&#1111;'data'] .= $data;
    &#125;
&#125;

function Slashdot()
&#123;
	global $fp2;

	$begin_line = ":: ";
 	$end_line = "<br>";
 	$local_file = "slashdot.txt";
 	$news_file = "http://slashdot.org/slashdot.xml";
 	$fp = fopen($local_file, "r");
 	$last_time = fgets($fp, 32);
 	if(time()-$last_time > 1800)
 	&#123;
 		fclose($fp);

 		$fp2 = fopen($local_file, "w");
 		fwrite($fp2, time()."\n");

		$parser = xml_parser_create($type);
		xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, true);
		xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, 'UTF-8');
		xml_set_element_handler($parser, 'startElement','endElement');
		xml_set_character_data_handler($parser, 'Data');

		if (!($fp = fopen($news_file, 'r')))
		&#123;
			die("Failed to open $file\n");
		&#125;

		while ($data = fread($fp, 10000))
		&#123;
		    if (!($data = utf8_encode($data)))
		    &#123;
		        echo 'ERROR'."\n";
		    &#125;
		    if (!xml_parse($parser, $data, feof($fp)))
		    &#123;
		        die(sprintf( "XML error: %s at line %d\n\n",
		        xml_error_string(xml_get_error_code($parser)),
		        xml_get_current_line_number($parser)));
		    &#125;
		&#125;

		xml_parser_free($parser);

		fclose($fp);
		fclose($fp2);
		$fp = fopen($local_file, "r");
 		fgets($fp, 1000);
 	&#125;

 	while(!(feof($fp)))
	&#123;
		$news&#1111;] = fgets($fp, 1000);
	&#125;

	$numlines = count($news);

	for($i=0; $i<$numlines-1; $i++)
	&#123;
		echo $begin_line;
		echo $news&#1111;$i];
		echo $end_line;
	&#125;
	fclose($fp);
&#125;
?>
I am sorry I cant help much more. I think this kind of thing is beyond me, but maybe I have pointed you in the right direction. I think you will have to get in touch with the sites owner and see if you can use the data as well.