Importation of the information from an another's site on the site in the design
Importation of the information from an another's site on the site in the design. (By the example of importation of weather forecasts with Yahoo.)
Goods, sirs!
The example is intended for those who starts to work with php, and not only for them.
Result of job of the program (script) is weather forecast for 5 days for any city interesting you, deduced{removed} as which is pleasant to you, instead of to designers of the site - donor.
The information in such cases undertakes from known servers of weather forecast (where do not write a phrase " use of the information ", etc. is forbidden). In this case the server http://weather.yahoo.com on which there are pages with weather for rather plenty of cities is used, and practically always it is possible to find if not city interesting you, the nearest to him and identical on weather conditions.
It is the finished project working on a site http://sim-sim.ru in section of tourism " weather in the world ".
Unique lack is only that the administrator should enter into a text file (is possible{probable} the variant with mysql, but in that case it was easier to me to make in a file) the name of settlement on the native language and the link to page with weather forecast on him on JAkho server. But anybody for you will not do{make} it.
Therefore, the script will consist of 2 parts:
1. A file with administration (the name of city is entered in the first line, on the next line - the link). To assort job of the given part, I think, does not cost, more than it is enough comments.
2. A file with the program. Job of the program will be in detail described below.
1. Administration
We display the form with the password pass. In a window are entered: numbers{rooms} of the name of the link Then, after pressing the button and check of the password, we write down the new list in a file.
<html>
<head>
<title> admin weather </title>
</head>
<body>
<? php
$adr=$DOCUMENT_ROOT."/weather/weather.ini "; // the address of a file in which names of cities with links will enter the name
$password ='pass'; // unpretentious system of authorization
$eror ='Password eror! ';
$old=file ($adr); // it is readable that now is in a file
if ($submit) {// it is checked on nazhatost` buttons
if ($pass == $password) {
$fp=fopen ($adr, "w");
fwrite ($fp, $ini); // it is written down in a file the changed data
fclose ($fp);
$old=file ($adr);
}
else {
echo $eror;
}
}
?>
<form method=post action = " <? php echo $PHP_SELF?> "> // the information entered into the form, is processed by the same file
password: <input type=text name=pass> <br>
inicialisation: <textarea name = "ini" rows=15 cols=60>
<?
for ($i=0; $i <sizeof ($old); $i ++) {
echo $old [$i], " "; // we display the current variant of a file
}
?>
</textarea>
<br>
<input type=submit name = "submit" value = "Enter">
</form>
</body>
</html>
After input of the information in a file as, we receive:
50
Larnaka
http://weather.yahoo.com/forecast/Larnaca_CY_f.html
51
Pathos
http://weather.yahoo.com/forecast/Paphos_CY_f.html
"44" - number{room} of city.
"Larnaka" - the name of city.
"http://weather.yahoo.com/forecast/Larnaca_CY_f.html" - the link to weather in the city of Larnaka on JAkhe.
Links to cities are organized by a principle:
<a href=http: // www.sim-sim.ru/catalogue/weather.php? weather=50> Larnaka </a>
And it is possible so:
<a href=http: // www.sim-sim.ru/catalogue/weather.php? city=Larnaka> Larnaka </a>
But if city from drukh words in blanks we write "%20"
In that case, numbers{rooms} of cities in the list are not necessary:
Larnaka
http://weather.yahoo.com/forecast/Larnaca_CY_f.html
Pathos
http://weather.yahoo.com/forecast/Paphos_CY_f.html
If at us not a text file, and mysql all is easier.
If who needs a variant with mysql, write to me totoeval@mtu-net.ru
2. The program (actually, the adapter of an another's code to your site)
<table width=100 % border=0 cellspacing=0 cellpadding=2 bgcolor = <? echo $brdcolor;?>>
<tr>
<td>
<br>
<!-----------------------FORECAST------------------------->
<? php
$ini = $ DOCUMENT_ROOT. '/weather/weather.ini ';
$region=file ($ini); // we read a file with the list of cities - links and a file $region
// Definition of coordinates of required city
for ($i=0; $i <sizeof ($region); $i ++) {
if (trim ($region [$i]) == $weather) {// we search for number{room} of city in the list
$city=trim ($region [$i+1]); // the name of city
$adr=trim ($region [$i+2]); // the address of page
}
else {
}
}
// If at us the information not by number{room} of city, and the name, is passed:
for ($i=0; $i <sizeof ($region); $i ++) {
$city=str_replace ($city, "%20", " "); // it is replaced "%20" on " "
if (trim ($region [$i]) == $city) {// we search for the name of city in the list
$adr=trim ($region [$i+1]); // the address of page
}
else {
}
}
// The entrance information for the further code - $adr (the address of page of weather forecast for city on a site http: // weather.yahoo.com) / and $city - the name of city on the native language.
//
// Phrases for search of the helpful information. The original of phrases can be found out on page, for example, this, having analysed a code.
// Here we get{start} in variable phrases on which we shall search for the necessary information (degrees, descriptions of weather, a picture). We use phrases before and after the necessary information.
This block in the future should be changed, when on the original the code of pages (weather.yahoo.com) will change.
$begin_screen ='FORECAST----'; // an initial phrase of the table with weather forecast
$end_screen = ' </b> </td> </tr> </table> </td> </tr> </table> '; // last phrase of the table
$post_gradus = ' </font> </b> <'; // after degrees
$before_gradus = ' <font size = "3" face = "arial"> '; // before degrees
$before_image ='http: // us.i1.yimg.com/us.yimg.com/i/we/fc / '; // before a picture
$post_image = '.gif '; // after a picture
$before_forecast ='top> <font face = "arial" size = "2"> '; // before a phrase of weather
$post_forecast = ' </font> </td> <td> '; // after a phrase of weather
// Files for translations of date and descriptions of weather from English language on native (the variant of translation with bourgeois on language which talked Lenin in this case is used.
$endat=array ("", Mon "," Tue "," Wed "," Thu "," Fri "," Sat "," Sun "," May "," Jul "," Jun "," Aug ",
"Sep", "Oct", "Nov", "Dec", "Jan", "Feb", "Mar", "Apr"); // English dates
$rudat=array ("", Monday "," Tuesday "," environment{Wednesday} "," Thursday "," Friday "," Saturday "," Sunday ",
"May", "July", "June", "August",
"September", "October", "November", "December", "January", "February", "March", "April"); // native dates
$eng=array ("", Thunderstorms "," Sunny "," Partly Cloudy "," Showers "," Rain "," Mostly Cloudy "," Tstorms "," Drizzle "," Cloudy "," Flurries "," Fog "," High "," Low "," Clear "," Sleet ",
"Rain/snow", " Snow Showers ", " Mixed Snow ", "Rain", "Snow", "Mixed"); // English descriptions of weather
$rus=array ("", a thunder-storm "," clearly "," variable overcast "," a downpour "," rains "," it is cloudy "," a thunder-storm "," a drizzle "," cloudy "," snowfall ",
"Fog", "day", "night", "clearly", " a rain with a snow ", " a rain with a snow ", "sleet", " a snow with a rain ", "snow", "peremenno"); // the Russian analogues of weather
$brdcolor = ' * ffffff '; // color of a framework of the table of the forecast (white)
$bgcolor = ' * eeeeee '; // color of a background of cells of the table (it is better - color of a background of page)
// And Here the code of the program which to change is not necessary begins.
// We are engaged in creation of the date displayed.
Function time () will give us the current time. Function date (d. " " .M, $t [$i]) we receive date current and the next 4 days, and date (D, $t [$i]) gives us days of week of all of 5 days. And then we translate dates from English language on native.
$t=array (4);
$d=array (4);
$dn=array (4);
for ($i=0; $i <=4; $i ++) {
$t [$i] =time () + $i*86400;
$d [$i] =date (d. " " .M, $t [$i]);
$dn [$i] =date (D, $t [$i]);
for ($j=0; $j <sizeof ($endat); $j ++) {// we translate words in date and we receive dates on the native language
$d [$i] =str_replace ($endat [$j], $rudat [$j], $d [$i]);
$dn [$i] =str_replace ($endat [$j], $rudat [$j], $dn [$i]);
} // also we receive dates in language which we talks noneshnij Vova
}
// We start to process page.
// We find out lines of the beginning and the end of the table with weather forecast. Then, we shall work with this block.
$screen=file ($adr); // we read page with weather forecast in a variable $screen
for ($i=1; $i <sizeof ($screen); $i ++) {// we process lines with the first on last
if (strpos ($screen [$i], $begin_screen) == false) {// we find the beginning of the table with the forecast
if (strpos ($screen [$i], $end_screen) == false) {// we find its{her} end
}
else {$m = $ i; // $m - number{room} of last line of the table
}
}
else {$k = $ i; // $k - number{room} of the first line of the table
}
} // It is received with $screen [$k] on $screen [$m] - our table with weather. Then budemrabotat` with her that there were no superfluous concurrences, and the program quickly worked.
// It is possible to deduce{remove} her in " native design, but it is not pleasant to us
// We start to search for the helpful information.
$grad=array (9); // a file from 10 numbers of degrees of an ambient temperature
$zed=0; // the counter of degrees
for ($i = $ k; $i <= $m; $i ++) {// we work with degrees
if (strpos ($screen [$i], $before_gradus, 0) == false) {// if there are no degrees in line (if there is no the phrase facing in degrees.
}
else {// if there are degrees in line
$string_grad = $ screen [$i]; // we save a line with degrees in a variable
$string_grad=explode ($before_gradus, $string_grad); // we break a line into a file of lines on a separator which the phrase before degrees is, thus, we receive a line which begins actually with the degree of weather in 1 element of a file.
$end_grad=strpos ($string_grad [1], $post_gradus, 0); // we determine a position of the beginning of the phrase going after degree.
$grad [$zed] =substr ($string_grad [1], 0, $end_grad); // the first degree is received, as podstroka, from 0-th symbol prior to the beginning of a phrase after degree.
$zed ++; // inkrement the counter of degrees
$end_grad=strpos ($string_grad [2], $post_gradus, 0);
$grad [$zed] =substr ($string_grad [2], 0, $end_grad); // the second degree
$zed ++; // inkrement the counter of degrees
}
} // therefore, we have received all degrees on our page
// We translate farengejty in cel`sii
for ($i=0; $i <=9; $i ++) {$grad [$i] =round (5/9 * ($ grad [$i]-32));
}
// In a variant with zabiraniem information from page with cel`sijami
// This line costs{stands} simply zakommentirovat`. I have left so, simply a whim.
// We translate farengejty in cel`sii
// We find words of weather absolutely similarly to search of degrees
$zed=0; // the counter of descriptions of weather
for ($i = $ k; $i <= $m; $i ++) {// we work with descriptions
if (strpos ($screen [$i], $before_forecast, 0) == false) {// if there is no description of weather in line
}
else {// if there are descriptions in line
$string_grad = $ screen [$i]; // we save a line with descriptions in a variable
$string_grad=explode ($before_forecast, $string_grad);
$end_grad=strpos ($string_grad [1], $post_forecast, 0);
$forec [$zed] =substr ($string_grad [1], 0, $end_grad);
for ($j=0; $j <sizeof ($eng); $j ++) {// it is touched all variants of words in descriptions
$forec [$zed] =str_replace ($eng [$j], $rus [$j], $forec [$zed]); // we translate words
}
$zed ++; // inkrement the counter of descriptions
}
} // therefore, we have received all descriptions of weather on our page
$zed=0; // the counter of words of pictures similarly
for ($i = $ k; $i <$m; $i ++) {// we work with pictures
if (strpos ($screen [$i], $before_image, 0) == false) {// if there is no description of weather in line
}
else {// if is a picture in line
$string_grad = $ screen [$i]; // we save a line with pictures in a variable
$string_grad=explode ($before_image, $string_grad);
$end_grad=strpos ($string_grad [1], $post_image, 0);
$for_img [$zed] =substr ($string_grad [1], 0, $end_grad);
$zed ++; // inkrement the counter of pictures
}
} // therefore, we have received all of a picture on our page
?>
// We publish results:
<table width=100 % cellpadding=0 cellspacing=0 border=0>
<tr> <td width=20 % align=center> <font size=3> <b>
<? echo $city; // we deduce{remove} the name of city for which weather forecast is displayed
?> </b> <br> </font> </td> </tr> </table>
<table width=100 % cellpadding=0 cellspacing=0 border=0>
<tr>
<?
for ($i=0; $i <=4; $i ++) {// we display cells with dates and days
echo " <td width=20 % align=center> <font size=2> <b> $d [$i] <br> $dn [$i] </b> </font> </td> ";
}
?> </tr>
</table>
<table width=100 % cellpadding=0 cellspacing=0 border=0 bgcolor = <? echo $brdcolor;?>>
<tr>
<td>
<table width=100 % cellpadding=2 cellspacing=1 border=0>
<tr>
<?
$zed=0; // the counter of deduced{removed} degrees
for ($i=0; $i <=4; $i ++) {// we deduce{remove} cells with forecasts - a picture, degrees, descriptions
$zed1 = $ zed+1;
echo " <td width=20 % align=center bgcolor = $ bgcolor valign=middle> <img src =../pic / ". $ for_img [$i]. " .gif border=0 alt = ". $ forec [$i]. "> <br> <br> <font size=2> ". $forec [$i.] </font> <br> <br> <font size=3> <b> ". $grad [$zed.] " °C <br> ". $grad [$zed1.] " °C </b> </font> </td> ";
$zed = $ zed+2;
}
?>
</tr>
</table>
// A picture it is possible to adhere to pictures JAkhi (names similar on weather coincide as in my case), and it can be made files of conformity of words - descriptions of weather and your pictures. It to desire. That is more durable, it is difficult to judge. And they can change phrases - descriptions, and a picture to rename. It is possible, certainly to agree with administrator Jahi that they did not change one of these elements and to make a start from him , but I did not have it telefona:o)
</td>
</tr>
</table>
Now the program works, and she can be inserted into the design. variant:vysheupomjanutaja Larnaka
If who will be interested " with variations on a subject " or there will be questions, ask to write to me totoeval@mtu-net.ru. And also, write everyone who can advise other variants. It is always glad to criticism and an opportunity to raise{increase} the level

|