PDA

View Full Version : space news avilable in PHP?



karansokhey
2005-Apr-07, 11:26 PM
I emailed info@universetoday.com but I didnt recive a reply and its been over 2 days. I just thought I would register to the forums and ask some questions. My emailed is quoted below, as I didnt want to take my time typing it out again. *I wasnt sure if Universe Today was admined by a single person or a group of members.

I wanted the script on my website to educate the members. I don't like
the idea of using the java script and placing it on a website. You
can't skin the way it should appear on the page. With PHP you can do
this, and I can help. You can still have it so you update the file
like you do every weekday. It will have a file which they can download
and skin. Another file will be placed on universetoday.com In this
file there will be variables such as story1 and img1, story2 and img2.
You can also have the variable copyright or date... anything you wish.
In the file that the users will download they can customize how the
story should appear by placing where they would like the variables. So
now they can have the images on the right or the last story first...
you get the idea. Im new to PHP but this seems as an easy task and I
have many friends who are able to help me. I can take a couple days
and finish the script and you can have a look at it. You don't have to
take it under your consideration but just give the chance. I'd like to
have Space News on my website but have the ability to skin it. I've
drawn a little diagram which will help explain how it will work :)

http://desiwest.com/karan/space.gif

I think this would give us PHP people benfits.

Fraser
2005-Apr-08, 04:23 AM
Hi Karan, the stories in Universe Today are available as an RSS feed.

http://www.universetoday.com/universetoday.xml

So you can format that data into your website in any way you like. I'm not sure about the images, though. Right now there are 1000s of websites currently depending on me to not change any part of the Javascript file. So, I'm going to hold off changing the way I syndicate things.

I appreciate your suggestions, though.

karansokhey
2005-Apr-08, 08:55 PM
Yes I understand that a lot of people are using the Javascript. It was worth a try. Can I see any examples of how it looks when its customized with the RSS. I never got the chance to see how RSS exactly works. Can you explain it to me please.

gavwvin
2005-Apr-10, 10:32 PM
RSS is an incarnation of xml, which is a format very easily read by computers- similar to html in structure in that you have an opening tag, some data, then a closing tag. PHP has built in functions to parse an xml document such as an rss feed, enabling you to extract and display the information in any way you please.
Basically (unless you have php 5) to get php to parse a xml document you define 3 functions, what to do when php finds an opening tag, what to do when it finds some data between tags, and what to do when it finds a closing tag. In your case you want something like:


if($name=="LINK") {
echo "<a href='".$data."'>Click here for the full UT story</a>";
} else if($name=="TITLE") {
echo "<h2>".$data."</h2>";
} else if($name=="DESCRIPTION") {
echo "<p>".$data."</p>";
}

I have done this on my website:
http://www.astronomyforbeginners.com/astro.../news.php?js=no (http://www.astronomyforbeginners.com/astronomy/news.php?js=no)
I can post or send you the php code if you like.
(Although note that the rss feed on UT does not contain urls for the images, so you can't show images for the stories)

karansokhey
2005-Apr-13, 09:13 PM
yes please could you post the code for me. I really wanted the images but what can you do :( btw why cant you have them? *new to php and all the rest but html* btw the news looks really cool on your website. if your allowed to upload a file could you do that please or send it to karansokhey@gmail.com

thank you

gavwvin
2005-Apr-14, 09:23 PM
here goes:


<?php
function start_element($xml_parser,$name,$attr)
{
global $inside_item, $tag;
if($inside_item) $tag=$name;
else if($name=="ITEM") $inside_item=true;
}
function end_element($xml_parser,$name)
{
global $inside_item, $tag, $title, $description, $link, $stories;
if($name=="ITEM") {
/*This is the bit you change to suit your display*/
$stories[]="
<h2><a target='_blank' href='".$link."'>".$title."</a></h2>
<p>".$description."</p><br />
<hr />
<br />
";
$title="";
$link="";
$description="";
$inside_item=false;
}
}
function inside_element($xml_parser,$data)
{
global $inside_item, $tag, $title, $description, $link;
if($inside_item) {
switch($tag) {
case "TITLE":
$title.=$data;
break;

case "DESCRIPTION":
$description.=$data;
break;

case "LINK":
$link.=$data;
break;
}
}
}
//--------end xml functions---------

function rss_feed($provider, $view = "all")
{
//variables
global $inside_item, $tag, $title, $description, $link, $stories;
$inside_item = false;
$title = "";
$description = "";
$link = "";
$tag = "";
$stories = array();

//create parser
$parser = xml_parser_create();
xml_set_element_handler($parser, "start_element", "end_element");
xml_set_character_data_handler($parser, "inside_element");

//read xml file
if($fh=@fopen($provider, "rb")) {
while($fdata=fread($fh, 4096)) {
if(!@xml_parse($parser, $fdata,feof($fh))) {
$failed = true;
return false;
}
}
} else {
$failed=true;
return false;
}
fclose($fh);
xml_parser_free($parser);

//output
if(!$failed) {
if($view=="r") {
return $stories[rand(0,(count($stories)-1))];
} else if($view=="1") {
return $stories[0];
} else {
$output="";
foreach($stories as $key=>$story) {
$output.=$story;
}
return $output;
}
}
}
?>

paste the above code into the page you want to use the feed (or in a functions file that is included from every page or something)
Then to get the output you do this:


$ut = rss_feed("http://www.universetoday.com/universetoday.xml");
echo $ut;

to output all the stories. Change the bit I've commented in the end_element() function to change the look of each story. You can also call the rss_feed function with a second argument to give one story chosen at random:


$ut = rss_feed("http://www.universetoday.com/universetoday.xml","r");
echo $ut;

or just the top story:


$ut = rss_feed("http://www.universetoday.com/universetoday.xml","1");
echo $ut;


As for images, you'll have to ask fraser about that. I would like to see the image urls included as well, but its up to him.

Incidentally you can use the function for any feed that uses the same format, just call the function with the appropriate URL address of the feed.

karansokhey
2005-Apr-15, 12:51 AM
Cool thanks a lot. It works. I was just wondering if I only wanted to show 3 or 4 stories. How would that be done? I've already tried


$ut = rss_feed("http://www.universetoday.com/universetoday.xml","1,2,3");
echo $ut;

$ut = rss_feed("http://www.universetoday.com/universetoday.xml","1","2","3");
echo $ut;

$ut = rss_feed("http://www.universetoday.com/universetoday.xml","1");
echo $ut;
$ut = rss_feed("http://www.universetoday.com/universetoday.xml","2");
echo $ut;
$ut = rss_feed("http://www.universetoday.com/universetoday.xml","3");
echo $ut;

$ut = rss_feed("http://www.universetoday.com/universetoday.xml","3");
echo $ut;
Thanks bro.

karansokhey
2005-Apr-15, 12:58 AM
oh almost forgot to request something from fraser. do you think you could add images to the rss feed? it makes it so much more interesting. Right now it just looks like plain text that wouldnt mean anything to the audiance such as kids.

gavwvin
2005-Apr-15, 07:08 AM
Have to adapt the function for that (the 3 or 4 stories). Sent you a PM with the modifications to the function (don't want to bore everyone else by posting all that code again :))

karansokhey
2005-Apr-16, 06:13 PM
thanks a lot gavwvin. It works! :lol:

armator
2009-Nov-02, 07:36 AM
Special thanks to gavwvin for this script.

Could you please help for this script ?

Which command or commands we can add for str_replace or ereg_replace in this script ?

And also which command we can add description limit ?

Thanks advance for your kind helps.

Brgds
armator

01101001
2009-Nov-02, 01:47 PM
Welcome to BAUT Forum.


Thanks advance for your kind helps.

You seem to be addressing a member, gavwvin (http://www.bautforum.com/members/gavwvin.html), whose last activity was March 2006, 3-1/2 years ago. I'd be surprised if gavwvin is still reading. Maybe someone else will help.

armator
2009-Nov-03, 09:02 AM
01101001 thanks your reply.

I hope someone help.

slang
2009-Nov-05, 08:37 AM
Unfortunately my box with apache and php on it had a "heads meet platters" incident, so I can't play around with the code. But it's not clear to me what you are asking anyway. Maybe someone might have a few suggestions if you're a bit more specific in describing what you want to achieve.