|[Login] [Register] [Forgot your password?]|
||amiga-news.de headlines on your homepage|
Within the scope of a PHP workshop the (German) Amiga-Magazin has recently dealt with the possibility to include our news via a backend within their own PHP website. As the scripts were thoroughly tested since then we don't want to withhold this possibility from our users. Here you can see an example of the result.
Important: The preset time of six hours must not be reduced! Calls with shorter intervals will lead to IP blocking!
Please read in the following translation of the article from Amiga-Magazin how you can include this service in your website:
Fresh news with PHP
Some news sites like amiga-news.de offer a backend service for up-to-date news. To do this the site creators provide a special file on the server that e.g. makes the last ten headlines available. These data can be included in the own start page by using PHP so that you can always offer fresh news on your site.
In listing 3 you see a solution in PHP. You can include this script snip in the own document and with every call are the current headlines of amiga-news.de shown on your page. The PHP script pulls the data file and does a conversion into HTML code. This is written into a temporary file and then included in the page.
Before doing the transfer the script verify the age of the present local data. After reaching an appointed age the head file of amiga-news.de is read and the list is created anew. Then the script overwrites the local data.
In the first part are declared some variables. These are the server address, the port used by the script to transfer the data from amiga-news.de and the file the data are written to. The following variables define some values for time and date. In the example comes a list with names of months next that are later needed for generating the HTML text.
Now the actual program begins. The script gets the headlines every six hours from amiga-news.de. A shorter period of time shouldn't be chosen because of performance reasons. Otherwise data would be pulled too often from the server and unnecessarily increase the data transfer. If the data are too old the script opens a connection via port Port 80 (HTTP) to amiga-news.de, transfers the data and generates the HTML data by using these. If a connection to amiga-news.de can't be established a corresponding error message will appear.
The script now read line by line the data -- the format of the amiga.news.de headlines can be found in listing 4. If the script hits the string %% (beginning of a message) it starts the evaluation of the data. The first line in the headline is the title -- it is read into a corresponding variable ($headline). Then follows the link to the message -- it goes to $headlink. After that follow date and time.
In our case only the date is of interest for us. That's why the back part of the data is simply cut with the strtok command. The space between date and time is the parting sign.
In the example we want always summarize the news of a day and place the date before it. For that reason the program is testing the date -- if it's empty it becomes the current date. With explode the year, month and day are parted into single strings (parting sign is a hyphen -- see listing 4). Then is the string for the output of the current date assembled. A non-breakable space is set at the beginning so that the date is inserted a little bit and the tag for the bold print (<B>) is set.
Now follow the day, the month and the year. In this step are also the predefined month names inserted (see beginning of the listing). At the end there has to be a </B> to deactivate the bold mode (bold print).
With the following if-clause the program tests whether the last read data ($current_date) is greater than the obtained one ($akdate). If this is the case a new date line for HTML has to be generated, as the next message comes from the previous day. Here is the advantage of the automatic type conversion in PHP used. When comparing with »greater« PHP interpretes the string as number. If $current_date is not greater than $akdate there's simply an empty string allocated for $ak_date.
Hint: The variable $akdate has two functions in the script - it is used as memory for the date obtained by amiga-news.de and later on used for the date line in the HTML code.
When the date line is done the HTML code for the headline with link is being generated. At the beginning is the date integrated. Is the headline from the same day as in the line before this string is set to »empty« during the date comparison in the upper part -- no date is inserted. After that the program writes the generated HTML line into th local news file. This is done until all lines of the headline file have been read. The local data file will then be closed and the connection to the server of amiga-news.de will be closed.
In the last part of the script is the integration of the news file (that already contains HTML code) into the website done. The server then sents this part to the client.
The presented solutions can in principe already be integrated in the own website. But it contains almost no text formatting. During the assembling of the string for the date the text is set to bold in the example -- but you can here also change the font size (font tag). You can built a table, too, and feed the other data (time, rubric, editor) into the list.
With kind permission by
© 2001 Amiga Magazin, WEKA Computerzeitschriften GmbH
http://www.amiga-magazin.de/ (ps) (Translation: sk)
[News message: 09. Jul. 2001, 20:28] [Comments: 0]
[Send via e-mail] [Print version] [ASCII version]
Copyright © 1997-2018 by amiga-news.de - all rights reserved.