-
Notifications
You must be signed in to change notification settings - Fork 1
/
WC-forecast.php
1336 lines (1175 loc) · 48.9 KB
/
WC-forecast.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
// WC-forecast.php script by Ken True - [email protected]
// Based on WC-forecast.php V3.05 - 13-Jun-2018 and rewritten to use the new WeatherUnderground api.weather.com
// json data 5 day forecast to replace the deprecated weatherunderground.com API which
// was turned off in March, 2019.
//
// Version 1.00 - 28-Feb-2019 - initial release
// Version 1.01 - 02-Mar-2019 - corrected some WU variable names to WC variable names
// Version 1.02 - 04-Mar-2019 - rewrote icon printing for clairity and Day-over-Night display option
// Version 1.03 - 10-Mar-2019 - added displays for all available icons in day-over-night mode
// Version 1.04 - 22-Mar-2019 - fix TWC/WU JSON degree sign \xc2\xba to correct UTF-8 \xc2\xb0
// Version 1.05 - 01-Jan-2021 - fix for PoP icon displays
//
$Version = "WC-forecast.php (ML) Version 1.05 - 01-Jan-2021";
//
// error_reporting(E_ALL); // uncomment to turn on full error reporting
//
// script available at http://saratoga-weather.org/script-WCforecast.php
//
// you may copy/modify/use this script as you see fit,
// no warranty is expressed or implied.
//
// This script parses the Weather Channel 5-day forecast JSON API and loads icons/text into
// arrays so you can use them in your weather website.
//
// NOTE: You must leave an attribution link to weatherunderground.com in the output page.
//
// output: creates XHTML 1.0-Strict HTML page (or inclusion)
//
// You can also invoke options directly in PHP like this
//
// $doIncludeWC = true;
// include("WC-forecast.php"); for just the text
// or ------------
// $doPrintWC = false;
// include("WC-forecast.php"); for setting up the $WCforecast... variables without printing
//
// or ------------
// $doIncludeWC = true;
// $doPrintHeadingWC = true;
// $doPrintIconsWC = true;
// $doPrintTextWC = false
// include("WC-forecast.php"); include mode, print only heading and icon set
//
// Variables returned (useful for printing an icon or forecast or two...)
//
// $WCforecastcity - Name of city from WC Forecast header
//
// The following variables exist for $i=0 to $i= number of forecast periods minus 1
// a loop of for ($i=0;$i<count($WCforecastday);$i++) { ... } will loop over the available
// values.
//
// $WCforecastday[$i] - period of forecast
// $WCforecasttext[$i] - text of forecast
// $WCforecasttemp[$i] - Temperature with text and formatting
// $WCforecastpop[$i] - Number - Probabability of Precipitation ('',10,20, ... ,100)
// $WCforecasticon[$i] - base name of icon graphic to use
// $WCforecastcond[$i] - Short legend for forecast icon
// $WCforecasticons[$i] - Full icon with Period, <img> and Short legend.
// $WCforecastthunder[$i] - thunder risk (text)
// $WCforecastuv[$i] - formatted UVIndex and word description
// $WCforecastsnow[$i] - amount of snow forecast (or empty if no snow)
// $WCforecastrain[$i] - amount of rain forecast (or empty if no rain)
//
//
// Settings ---------------------------------------------------------------
//REQUIRED: a WU API KEY.. sign up at https://www.wunderground.com/member/api-keys
$WCAPIkey = 'specify-for-standalone-use-here'; // use this only for standalone / non-template use
// NOTE: if using the Saratoga template, add to Settings.php a line with:
// $SITE['WCAPIkey'] = 'your-api-key-here';
// and that will enable the script to operate correctly in your template
//
// Select which units will be used for the displays:
//
//$WCunits = 'e'; // 'e'= US units F,mph,inHg,in,in
$WCunits = 'm'; // 'm'= metric C,km/h,hPa,mm,cm
//$WCunits = 'h'; // 'h'= UK units C,mph,mb,mm,cm
//$WCunits = 's'; // 's'= SI units C,m/s,hPa,mm,cm
//
$iconDir ='./forecast/images/'; // directory for carterlake icons './forecast/images/'
$iconType = '.jpg'; // default type='.jpg'
// use '.gif' for animated icons from http://www.meteotreviglio.com/
//
//
$WC_LOC = 'Saratoga, CA, USA|37.27465,-122.02295';
//
// The optional multi-city forecast .. make sure the first entry is for the $WC_LOC location
// The contents will be replaced by $SITE['WCforecasts'] if specified in your Settings.php
//*
$WCforecasts = array(
// Location name to display|lat,long (separated by | character)
'Saratoga, CA, USA|37.27465,-122.02295',
'Auckland, NZ|-36.910,174.771', // Awhitu, Waiuku New Zealand
'Assen, NL|53.02277,6.59037',
'Blankenburg, DE|51.8089941,10.9080649',
'Cheyenne, WY, USA|41.144259,-104.83497',
'Carcassonne, FR|43.2077801,2.2790407',
'Braniewo, PL|54.3793635,19.7853585',
'Omaha, NE, USA|41.19043,-96.13114',
'Johanngeorgenstadt, DE|50.439339,12.706085',
'Athens, GR|37.97830,23.715363',
'Haifa, IL|32.7996029,34.9467358',
'Tahoe Vista, CA, USA|39.2403,-120.0528',
'Auburn, CA, USA|38.8962,-121.0789',
);
//*/
$commaDecimal = false; // set to true to process numbers with a comma for a decimal point
//
$maxWidth = '640px'; // max width of tables (could be '100%')
$maxForecastLegendWords = 4; // more words in forecast legend than this number will use our forecast words
$autoSetTemplate = true; // =true; set icons based on wide/narrow template design
// // =false; don't autoset maxWidth based on Saratoga wide/narrow
$foldIconRow = true; // =true; display icons in rows of 5 if long texts are found
$iconRowDayNight = true; // =false; 9 icons in a row, folded over if long texts.
// // =true; icons always in two rows Day over Night
$cacheFileDir = './'; // default cache file directory
$cacheName = "WC-forecast-json.txt"; // locally cached page from WC
$refetchSeconds = 3600; // cache lifetime (3600sec = 60 minutes)
$charsetOutput = 'ISO-8859-1'; // default character encoding of output ='ISO-8859-1' for Saratoga templates
$lang = 'en'; // default language ='en' for English
// ---- end of settings ---------------------------------------------------
// overrides from Settings.php if available
global $SITE;
if (isset($SITE['WCforecasts'])) {$WCforecasts = $SITE['WCforecasts']; }
if (isset($SITE['WCAPIkey'])) {$WCAPIkey = $SITE['WCAPIkey']; }
if (isset($SITE['WCunits'])) {$WCunits = $SITE['WCunits']; }
if (isset($SITE['fcsturlWC'])) {$WC_LOC = $SITE['fcsturlWC'];}
if (isset($SITE['fcsticonsdir'])) {$iconDir = $SITE['fcsticonsdir'];}
if (isset($SITE['fcsticonstype'])) {$iconType = $SITE['fcsticonstype'];}
if (isset($SITE['charset'])) {$charsetOutput = strtoupper($SITE['charset']); }
if (isset($SITE['lang'])) {$lang = $SITE['lang'];}
if(isset($SITE['cacheFileDir'])) {$cacheFileDir = $SITE['cacheFileDir']; }
if(isset($SITE['foldIconRow'])) {$foldIconRow = $SITE['foldIconRow']; }
if(isset($SITE['iconRowDayNight'])) {$iconRowDayNight = $SITE['iconRowDayNight']; }
if(isset($SITE['RTL-LANG'])) {$RTLlang = $SITE['RTL-LANG']; }
if (isset($SITE['commaDecimal'])) {$commaDecimal = $SITE['commaDecimal'];}
// end of overrides from Settings.php
//
// -------------------begin code ------------------------------------------
if (isset($_REQUEST['sce']) && strtolower($_REQUEST['sce']) == 'view' ) {
//--self downloader --
$filenameReal = __FILE__;
$download_size = filesize($filenameReal);
header('Pragma: public');
header('Cache-Control: private');
header('Cache-Control: no-cache, must-revalidate');
header("Content-type: text/plain;charset=ISO-8859-1");
header("Accept-Ranges: bytes");
header("Content-Length: $download_size");
header('Connection: close');
readfile($filenameReal);
exit;
}
$Status = "<!-- $Version on PHP ".phpversion()." -->\n";
//------------------------------------------------
if(preg_match('|specify|i',$WCAPIkey)) {
print "<p>Note: the WC-forecast.php script requires an API key from WeatherUnderground to operate.<br/>";
print "Visit <a href=\"https://www.wunderground.com/member/api-keys\">Weather Underground</a> to ";
print "register for an API key. You must have a PWS submitting data to WU to acquire an API key.</p>\n";
if( isset($SITE['fcsturlWC']) ) {
print "<p>Insert in Settings.php an entry for:<br/><br/>\n";
print "\$SITE['WCAPIkey'] = '<i>your-key-here</i>';<br/><br/>\n";
print "replacing <i>your-key-here</i> with your WU/TWC API key.</p>\n";
}
return;
}
$NWSiconlist = array( // translation from api.weather.gov icons to NWS-styled icons Day,Night
0 => array('tor.jpg','ntor.jpg'), // Tornado, 00.png, Forecast, Night + Day
1 => array('tropstorm-noh.jpg','tropstorm-noh.jpg'), // Tropical Storm, 01.png, Forecast + Observations, Night + Day
2 => array('hurr-noh.jpg','hurr-noh.jpg'), // Hurricane, 02.png, Forecast, Night + Day
3 => array('scttsra.jpg','nscttsra.jpg'), // Strong Storms, 03.png, Forecast, Night + Day
4 => array('tsra.jpg','ntsra.jpg'), // Thunder and Hail, 04.png, Forecast + Observations, Night + Day
5 => array('rasn.jpg','nrasn.jpg'), // Rain to Snow Showers, 05.png, Forecast + Observations, Night + Day
6 => array('raip.jpg','nraip.jpg'), // Rain / Sleet, 06.png, Forecast + Observations, Night + Day
7 => array('mix.jpg','nmix.jpg'), // Wintry Mix Snow / Sleet, 07.png, Forecast + Observations, Night + Day
8 => array('fzrara.jpg','fzrara.jpg'), // Freezing Drizzle, 08.png, Forecast + Observations, Night + Day
9 => array('mist.jpg','mist.jpg'), // Drizzle, 09.png, Forecast + Observations, Night + Day
10 => array('fzra.jpg','nfzra.jpg'), // Freezing Rain, 10.png, Forecast + Observations, Night + Day
11 => array('ra.jpg','nra.jpg'), // Light Rain, 11.png, Forecast + Observations, Night + Day
12 => array('ra.jpg','nra.jpg'), // Rain, 12.png, Forecast + Observations, Night + Day
13 => array('sn.jpg','nsn.jpg'), // Scattered Flurries, 13.png, Forecast + Observations, Night + Day
14 => array('sn.jpg','nsn.jpg'), // Light Snow, 14.png, Forecast + Observations, Night + Day
15 => array('blizzard.jpg','nblizzard.jpg'), // Blowing / Drifting Snow, 15.png, Forecast + Observations, Night + Day
16 => array('sn.jpg','nsn.jpg'), // Snow, 16.png, Forecast + Observations, Night + Day
17 => array('ip.jpg','nip.jpg'), // Hail, 17.png, Forecast + Observations, Night + Day
18 => array('ip.jpg','nip.jpg'), // Sleet, 18.png, Forecast + Observations, Night + Day
19 => array('du.jpg','ndu.jpg'), // Blowing Dust / Sandstorm, 19.png, Forecast + Observations, Night + Day
20 => array('fg.jpg','nfg.jpg'), // Foggy, 20.png, Forecast + Observations, Night + Day
21 => array('hazy.jpg','hazy.jpg'), // Haze / Windy, 21.png, Forecast + Observations, Night + Day
22 => array('fu.jpg','nfu.jpg'), // Smoke / Windy, 22.png, Forecast + Observations, Night + Day
23 => array('wind.jpg','nwind.jpg'), // Breezy, 23.png, Forecast, Night + Day
24 => array('wind.jpg','nwind.jpg'), // Blowing Spray / Windy, 24.png, Forecast + Observations, Night + Day
25 => array('cold.jpg','ncold.jpg'), // Frigid / Ice Crystals, 25.png, Forecast + Observations, Night + Day
26 => array('ovc.jpg','novc.jpg'), // Cloudy, 26.png, Forecast + Observations, Night + Day
27 => array('bkn.jpg','nbkn.jpg'), // Mostly Cloudy, 27.png, Forecast + Observations, Night + Day
28 => array('bkn.jpg','nbkn.jpg'), // Mostly Cloudy, 28.png, Forecast + Observations, Day
29 => array('sct.jpg','nsct.jpg'), // Partly Cloudy, 29.png, Forecast + Observations, Night
30 => array('sct.jpg','nsct.jpg'), // Partly Cloudy, 30.png, Forecast + Observations, Day
31 => array('skc.jpg','nskc.jpg'), // Clear, 31.png, Forecast + Observations, Night
32 => array('skc.jpg','nskc.jpg'), // Sunny, 32.png, Forecast + Observations, Day
33 => array('few.jpg','nfew.jpg'), // Fair / Mostly Clear, 33.png, Forecast + Observations, Night
34 => array('few.jpg','nfew.jpg'), // Fair / Mostly Sunny, 34.png, Forecast + Observations, Day
35 => array('raip.jpg','nraip.jpg'), // Mixed Rain & Hail, 35.png, Forecast, Day
36 => array('hot.jpg','hot.jpg'), // Hot, 36.png, Forecast, Day
37 => array('scttsra.jpg','nscttsra.jpg'), // Isolated Thunderstorms, 37.png, Forecast, Day
38 => array('tsra.jpg','ntsra.jpg'), // Thunderstorms, 38.png, Forecast + Observations, Night + Day
39 => array('hi_shwrs.jpg','hi_nshwrs.jpg'), // Scattered Showers, 39.png, Forecast, Day
40 => array('shra.jpg','nshra.jpg'), // Heavy Rain, 40.png, Forecast + Observations, Night + Day
41 => array('sn.jpg','nsn.jpg'), // Scattered Snow Showers, 41.png, Forecast, Day
42 => array('sn.jpg','nsn.jpg'), // Heavy Snow, 42.png, Forecast + Observations, Night + Day
43 => array('blizzard.jpg','nblizzard.jpg'), // Blizzard, 43.png, Forecast, Night + Day
44 => array('na.jpg','na.jpg'), // Not Available (N/A), 44.png, Forecast, Night + Day
45 => array('hi_shwrs.jpg','hi_nshwrs.jpg'), // Scattered Showers, 45.png, Forecast, Night
46 => array('sn.jpg','nsn.jpg'), // Scattered Snow Showers, 46.png, Forecast, Night
47 => array('scttsra.jpg','nscttsra.jpg'), // Scattered Thunderstorms, 47.png, Forecast + Observations, Night + Day
) ;
//*/
if(!function_exists('langtransstr')) {
// shim function if not running in template set
function langtransstr($input) { return($input); }
}
if(!function_exists('json_last_error')) {
// shim function if not running PHP 5.3+
function json_last_error() { return('- N/A'); }
$Status .= "<!-- php V".phpversion()." json_last_error() stub defined -->\n";
if(!defined('JSON_ERROR_NONE')) { define('JSON_ERROR_NONE',0); }
if(!defined('JSON_ERROR_DEPTH')) { define('JSON_ERROR_DEPTH',1); }
if(!defined('JSON_ERROR_STATE_MISMATCH')) { define('JSON_ERROR_STATE_MISMATCH',2); }
if(!defined('JSON_ERROR_CTRL_CHAR')) { define('JSON_ERROR_CTRL_CHAR',3); }
if(!defined('JSON_ERROR_SYNTAX')) { define('JSON_ERROR_SYNTAX',4); }
if(!defined('JSON_ERROR_UTF8')) { define('JSON_ERROR_UTF8',5); }
}
WC_loadLangDefaults (); // set up the language defaults
$WCLANG = 'en-US'; // Default to English for API
$RTLlang = ',he,jp,cn,'; // languages that use right-to-left order
$lang = strtolower($lang);
if(isset($WClanguages[$lang]) ) { // if $lang is specified, use it
$SITE['lang'] = $lang;
$WCLANG = $WClanguages[$lang];
if($charsetOutput !== 'UTF-8') {
$charsetOutput = (isset($WClangCharsets[$lang]))?$WClangCharsets[$lang]:$charsetOutput;
}
}
if(isset($_GET['lang']) and isset($WClanguages[strtolower($_GET['lang'])]) ) { // template override
$lang = strtolower($_GET['lang']);
$SITE['lang'] = $lang;
$WCLANG = $WClanguages[$lang];
if($charsetOutput !== 'UTF-8') {
$charsetOutput = (isset($WClangCharsets[$lang]))?$WClangCharsets[$lang]:$charsetOutput;
}
}
$doRTL = (strpos($RTLlang,$lang) !== false)?true:false; // format RTL language in Right-to-left in output
// get the selected forecast location code
$haveIndex = '0';
if (!empty($_GET['z']) && preg_match("/^[0-9]+$/i", htmlspecialchars($_GET['z']))) {
$haveIndex = htmlspecialchars(strip_tags($_GET['z'])); // valid zone syntax from input
}
if(!isset($WCforecasts[0])) {
// print "<!-- making NWSforecasts array default -->\n";
$WCforecasts = array("$WC_LOC"); // create default entry
}
// print "<!-- NWSforecasts\n".print_r($WCforecasts,true). " -->\n";
// Set the default zone. The first entry in the $SITE['NWSforecasts'] array.
list($Nl,$Nn) = explode('|',$WCforecasts[0].'|||');
$FCSTlocation = $Nl;
$WC_LOC = $Nn;
if(!isset($WCforecasts[$haveIndex])) {
$haveIndex = 0;
}
// locations added to the drop down menu and set selected zone values
$dDownMenu = '';
for ($m=0;$m<count($WCforecasts);$m++) { // for each locations
list($Nlocation,$Nname) = explode('|',$WCforecasts[$m].'|||');
$seltext = '';
if($haveIndex == $m) {
$FCSTlocation = $Nlocation;
$WC_LOC = $Nname;
$seltext = ' selected="selected" ';
}
$dDownMenu .= " <option value=\"$m\"$seltext>".langtransstr($Nlocation)."</option>\n";
}
// build the drop down menu
$ddMenu = '';
// create menu if at least two locations are listed in the array
if (isset($WCforecasts[0]) and isset($WCforecasts[1])) {
if($doRTL) {$RTLopt = ' style="direction: rtl;"'; } else {$RTLopt = '';};
$ddMenu .= '<tr align="center">
<td style="font-size: 14px; font-family: Arial, Helvetica, sans-serif">
<script type="text/javascript">
<!--
function menu_goto( menuform ){
selecteditem = menuform.logfile.selectedIndex ;
logfile = menuform.logfile.options[ selecteditem ].value ;
if (logfile.length != 0) {
location.href = logfile ;
}
}
//-->
</script>
<form action="" method="get">
<p><select name="z" onchange="this.form.submit()"'.$RTLopt.'>
<option value=""> - '.langtransstr('Select Forecast').' - </option>
' . $dDownMenu .
$ddMenu . ' </select></p>
<div><noscript><pre><input name="submit" type="submit" value="'.langtransstr('Get Forecast').'" /></pre></noscript></div>
</form>
</td>
</tr>
';
}
$Force = false;
if (isset($_REQUEST['force']) and $_REQUEST['force']=="1" ) {
$Force = true;
}
$doDebug = false;
if (isset($_REQUEST['debug']) and strtolower($_REQUEST['debug'])=='y' ) {
$doDebug = true;
}
$fileName = $WC_LOC;
if ($doDebug) {
$Status .= "<!-- WC LOC: $fileName -->\n";
}
if ($autoSetTemplate and isset($_SESSION['CSSwidescreen'])) {
if($_SESSION['CSSwidescreen'] == true) {
$maxWidth = '900px';
$Status .= "<!-- autoSetTemplate using ".$SITE['CSSwideOrNarrowDefault']." aspect. -->\n";
}
if($_SESSION['CSSwidescreen'] == false) {
$maxWidth = '640px';
$Status .= "<!-- autoSetTemplate using ".$SITE['CSSwideOrNarrowDefault']." aspect. -->\n";
}
}
$cacheName = $cacheFileDir . $cacheName;
// unique cache per index:UOM:language used
$cacheName = preg_replace('|\.txt|is',"-$haveIndex-$WCunits-$lang.txt",$cacheName);
$APIfileName = WC_get_APIURL($fileName); // transform WC page URL to API query URL
if (! $Force and file_exists($cacheName) and filemtime($cacheName) + $refetchSeconds > time()) {
$html = implode('', file($cacheName));
$Status .= "<!-- loading from $cacheName (" . strlen($html) . " bytes) -->\n";
} else {
$Status .= "<!-- loading from $APIfileName. -->\n";
$html = WC_fetchUrlWithoutHanging($APIfileName,false);
$RC = '';
if (preg_match("|^HTTP\/\S+ (.*)\r\n|",$html,$matches)) {
$RC = trim($matches[1]);
}
$Status .= "<!-- RC=$RC, bytes=" . strlen($html) . " -->\n";
if (preg_match('|30\d|',$RC)) { // handle possible blocked redirect
preg_match('|Location: (\S+)|is',$html,$matches);
if(isset($matches[1])) {
$sURL = $matches[1];
if(preg_match('|opendns.com|i',$sURL)) {
$Status .= "<!-- NOT following to $sURL --->\n";
} else {
$Status .= "<!-- following to $sURL --->\n";
$html = WC_fetchUrlWithoutHanging($sURL,false);
$RC = '';
if (preg_match("|^HTTP\/\S+ (.*)\r\n|",$html,$matches)) {
$RC = trim($matches[1]);
}
$Status .= "<!-- RC=$RC, bytes=" . strlen($html) . " -->\n";
}
}
}
if(preg_match('|daypart|Uis',$html)) {
$fp = fopen($cacheName, "w");
if (!$fp) {
$Status .= "<!-- unable to open $cacheName for writing. -->\n";
} else {
$write = fputs($fp, $html);
fclose($fp);
$Status .= "<!-- saved cache to $cacheName (". strlen($html) . " bytes) -->\n";
}
} elseif (file_exists($cacheName)) {
$html = implode('', file($cacheName));
$Status .= "<!-- WC API return has no forecast .. reusing existing cache file -->\n";
$Status .= "<!-- loading from $cacheName (" . strlen($html) . " bytes) -->\n";
}
}
// establish JSON charset. Usually in UTF-8
preg_match('|charset="{0,1}(\S+)"{0,1}|i',$html,$matches);
if (isset($matches[1])) {
$charsetInput = strtoupper($matches[1]);
} else {
$charsetInput = 'UTF-8';
}
$doIconv = ($charsetInput == $charsetOutput)?false:true; // only do iconv() if sets are different
$Status .= "<!-- using charsetInput='$charsetInput' charsetOutput='$charsetOutput' doIconv='$doIconv' doRTL='$doRTL' -->\n";
$i = strpos($html,"\r\n\r\n");
$headers = substr($html,0,$i-1);
$content = substr($html,$i+4);
if(preg_match('|Transfer-Encoding: chunke|Ui',$headers)) {
$Status .= "<!-- unchunking response -->\n";
$Status .= "<!-- in=".strlen($html);
$html = preg_replace("|\r\n[0-9a-fA-F]+\r\n|is",'',$html); // kludge, but should get them all :)
$Status .= " out=".strlen($html). " bytes -->\n";
}
// process the file .. select out the 7-day forecast part of the page
$UnSupported = false;
// --------------------------------------------------------------------------------------------------
$Status .= "<!-- processing JSON entries for forecast -->\n";
$i = strpos($html,"\r\n\r\n");
$headers = substr($html,0,$i-1);
$content = substr($html,$i+4);
$rawJSON = $content;
$Status .= "<!-- rawJSON size is ".strlen($rawJSON). " bytes -->\n";
$rawJSON = WC_prepareJSON($rawJSON);
$JSON = json_decode($rawJSON,true); // get as associative array
$Status .= WC_decode_JSON_error();
if($doDebug) {$Status .= "<!-- JSON\n".print_r($JSON,true)." -->\n"; }
/* //Ken's dump debugging code
$Status = htmlentities($Status);
$Status = preg_replace("|\n|is","<br/>\n",$Status);
print $Status;
return;
//Ken's dump debugging code
*/
if(json_last_error() === JSON_ERROR_NONE) { // got good JSON .. process it
$UnSupported = false;
$WCforecastcity = $FCSTlocation;
if($doIconv) {$WCforecastcity = iconv($charsetInput,$charsetOutput.'//TRANSLIT',$WCforecastcity);}
if($doDebug) {
$Status .= "<!-- WCforecastcity='$WCforecastcity' -->\n";
}
$WCtitle = "5-day Forecast";
if($doDebug) {
$Status .= "<!-- JSON daypart:0:daypartName count=" .
count( $JSON['daypart'][0]['daypartName']) . "-->\n";
}
$n = 0;
$FCpart = $JSON['daypart'][0];
$showTempsAs = ($WCunits == 'e')?'F':'C';
// process the forecast periods
$n = 0;
$firstIconPeriod = '';
foreach ($FCpart['temperature'] as $i => $t) {
if(empty($FCpart['daypartName'][$i])) {
continue; // skip the empty ones if any
}
if ( $doDebug) {
$Status .= "<!-- processing forecastday[$n]='" . $FCpart['daypartName'][$i] . "' -->\n";
}
$dayOrNight = $FCpart['dayOrNight'][$i];
if($firstIconPeriod == '') { $firstIconPeriod = $dayOrNight; }
if($dayOrNight == 'D') {
$WCforecasttemp[$n] = "<span style=\"color: #ff0000;\">".$t."°$showTempsAs</span>";
} else {
$WCforecasttemp[$n] = "<span style=\"color: #0000ff;\">".$t."°$showTempsAs</span>";
}
$WCforecastday[$n] = trim($FCpart['daypartName'][$i]);
if($doIconv) {$WCforecastday[$n] = iconv($charsetInput,$charsetOutput.'//TRANSLIT',$WCforecastday[$n]);}
$WCforecasttitles[$n] = $WCforecastday[$n];
if ($doDebug) {
$Status .= "<!-- WCforecastday[$n]='" . $WCforecastday[$n] . "' -->\n";
}
$WCforecasttext[$n] = trim($FCpart['narrative'][$i]);
$WCforecasttext[$n] = str_replace("\xc2\xba","\xc2\xb0",$WCforecasttext[$n]); // fix wrong degree symbol
if($doIconv) {$WCforecasttext[$n] = iconv($charsetInput,$charsetOutput.'//TRANSLIT',$WCforecasttext[$n]);}
if ($doDebug) {
$Status .= "<!-- WCforecasttext[$n]='" . $WCforecasttext[$n] . "' -->\n";
}
$WCforecastpop[$n] = $FCpart['precipChance'][$i];
if ($doDebug) {
$Status .= "<!-- WCforecastpop[$n]='" . $WCforecastpop[$n] . "' -->\n";
}
$WCforecastcond[$n] = $FCpart['wxPhraseLong'][$i];
$WCforecastcond[$n] = str_replace('/',', ',$WCforecastcond[$n]);
if($doIconv) {$WCforecastcond[$n] = iconv($charsetInput,$charsetOutput.'//TRANSLIT',$WCforecastcond[$n]);}
if ($doDebug) {
$Status .= "<!-- forecastcond[$n]='" . $WCforecastcond[$n] . "' -->\n";
}
$WCforecasticon[$n] = WC_img_replace($FCpart['iconCode'][$i],$dayOrNight,$WCforecastcond[$n],round($WCforecastpop[$n],-1)); // V1.05 fix
if ($doDebug) {
$Status .= "<!-- WCforecasticon[$n]='" . $WCforecasticon[$n] . "' -->\n";
}
$WCforecasticons[$n] = $WCforecastday[$n] . "<br/>" .
$WCforecasticon[$n] . "<br/>" .
$WCforecastcond[$n];
if(!empty($FCpart['thunderIndex'][$i]) and !empty($FCpart['thunderCategory'][$i])) {
$WCforecastthunder[$n] = '<span style="color: black; background-color: #FD9125;">' .$FCpart['thunderCategory'][$i] . '</span>';
if($doIconv) {$WCforecastthunder[$n] = iconv($charsetInput,$charsetOutput.'//TRANSLIT',$WCforecastthunder[$n]);}
} else {
$WCforecastthunder[$n] = '';
}
if ($doDebug) {
$Status .= "<!-- WCforecastthunder[$n]='" . $WCforecastthunder[$n] . "' -->\n";
}
if(!empty($FCpart['uvIndex'][$i]) and $FCpart['uvIndex'][$i] > 0) {
$WCforecastuv[$n] = 'UV: '. $FCpart['uvIndex'][$i] . ' <br/>'. WC_get_uvrange($FCpart['uvIndex'][$i],$FCpart['uvDescription'][$i]);
if($doIconv) {$WCforecastuv[$n] = iconv($charsetInput,$charsetOutput.'//TRANSLIT',$WCforecastuv[$n]);}
} else {
$WCforecastuv[$n] = '';
}
if ($doDebug) {
$Status .= "<!-- WCforecastuv[$n]='" . $WCforecastuv[$n] . "' -->\n";
}
if(!empty($FCpart['snowRange'][$i])) {
$WCforecastsnow[$n] = $FCpart['snowRange'][$i];
$WCforecastsnow[$n] .= ($WCunits == 'e')?' in':' cm';
if($doIconv) {$WCforecastsnow[$n] = iconv($charsetInput,$charsetOutput.'//TRANSLIT',$WCforecastsnow[$n]);}
} else {
$WCforecastsnow[$n] = '';
}
if ($doDebug) {
$Status .= "<!-- WCforecastsnow[$n]='" . $WCforecastsnow[$n] . "' -->\n";
}
if(!empty($FCpart['qpf'][$i]) and empty($FCpart['snowRange'][$i])) {
$WCforecastrain[$n] = $FCpart['qpf'][$i];
if($WCunits !== 'e') {$WCforecastrain[$n] = round($WCforecastrain[$n],1); }
if($WCforecastrain[$n] == 0) {$WCforecastrain[$n] = '<1'; }
if($commaDecimal) {$WCforecastrain[$n] = str_replace('.',',',$WCforecastrain[$n]); }
$WCforecastrain[$n] .= ($WCunits == 'e')?' in':' mm';
if($doIconv) {$WCforecastrain[$n] = iconv($charsetInput,$charsetOutput.'//TRANSLIT',$WCforecastrain[$n]);}
} else {
$WCforecastrain[$n] = '';
}
if ($doDebug) {
$Status .= "<!-- WCforecastrain[$n]='" . $WCforecastrain[$n] . "' -->\n";
}
$n++;
} // end of process json forecasts
} // end got good JSON decode/process
// end process JSON style --------------------------------------------------------------------
// All finished with parsing, now prepare to print
if(count($WCforecasticons) < 1) {
print "Error: no forecast data was found in JSON from api.weather.gov.\n";
return;
}
$wdth = intval(100/count($WCforecasticons));
$ndays = intval(count($WCforecasticon)/2);
$WCtitle = preg_replace('|5|i',$ndays,$WCtitle,1);
$doNumIcons = count($WCforecasticons);
// establish what we're going to print based on the options set
$IncludeMode = false;
$PrintMode = true;
if (isset($doPrintWC) && ! $doPrintWC ) {
print $Status;
return;
}
if (isset($_REQUEST['inc']) &&
strtolower($_REQUEST['inc']) == 'noprint' ) {
print $Status;
return;
}
if (isset($_REQUEST['inc']) && strtolower($_REQUEST['inc']) == 'y') {
$IncludeMode = true;
}
if (isset($doIncludeWC)) {
$IncludeMode = $doIncludeWC;
}
$printHeading = true;
$printIcons = true;
$printText = true;
if (isset($doPrintHeadingWC)) {
$printHeading = $doPrintHeadingWC;
}
if (isset($_REQUEST['heading']) ) {
$printHeading = substr(strtolower($_REQUEST['heading']),0,1) == 'y';
}
if (isset($doPrintIconsWC)) {
$printIcons = $doPrintIconsWC;
}
if (isset($_REQUEST['icons']) ) {
$printIcons = substr(strtolower($_REQUEST['icons']),0,1) == 'y';
}
if (isset($doPrintTextWC)) {
$printText = $doPrintTextWC;
}
if (isset($_REQUEST['text']) ) {
$printText = substr(strtolower($_REQUEST['text']),0,1) == 'y';
}
// ------- now do the actual HTML generation for the page
if (! $IncludeMode and $PrintMode) { ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>WeatherUnderground <?php echo $WCtitle . ' - ' . $WCforecastcity; ?></title>
<meta http-equiv="Content-Type" content="text/html; charset=<?php echo $charsetOutput; ?>" />
</head>
<body style="font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px; background-color:#FFFFFF">
<?php
} // end printmode and not includemode
print $Status;
// if the forecast text is blank, prompt the visitor to force an update
if($UnSupported) {
print <<< EONAG
<h1>Sorry.. this forecast can not be processed at this time.</h1>
EONAG
;
}
if (strlen($WCforecasttext[0])<2 and $PrintMode and ! $UnSupported ) {
echo '<br/><br/>Forecast blank? <a href="' . $_SERVER['PHP_SELF'] . '?force=1">Force Update</a><br/><br/>';
}
if ($PrintMode and ($printHeading or $printIcons)) { ?>
<table width="<?php print $maxWidth; ?>" style="border: none;" class="WCforecast">
<?php echo $ddMenu ?>
<?php if($printHeading) { ?>
<tr align="center" style="background-color: #FFFFFF;">
<td><b>WeatherUnderground <?php echo $WCtitle; ?>: </b><span style="color: green;">
<?php echo $WCforecastcity; ?></span>
</td>
</tr>
<?php } // end print heading
if ($printIcons) {
// ------------ Icon printing new method -----------------
$iconOrder = array( // defines which icons go where based on presentation selection
'S' => array( array(0,1,2,3,4,5,6,7,8) ), // One row (Straight Line)
'TR' => array( array(0,1,2,3,4), array(5,6,7,8,9) ), // Two Rows
'NO' => array( array(-1,1,3,5,7,9), array(0,2,4,6,8,10) ), // Night first icon, Day over Night
'DO' => array( array(0,2,4,6,8,10), array(1,3,5,7,9,11) ) // Day First icon, Day over Night
);
// if RTL, reverse 'em all
if(isset($SITE['CSSscreen']) and $doRTL) {
$doRTL = false;
$Status .= "<!-- RTL language selected in Saratoga template. \$doRTL = false; -->\n";
}
if($doRTL) {
foreach ($iconOrder as $type => $MO) {
foreach ($MO as $row => $cols) {
$iconOrder[$type][$row] = array_reverse($iconOrder[$type][$row]);
}
}
}
/* just a bit of test code to show how it works
print "<pre>\n";
foreach ($iconOrder as $type => $MO) {
print "--- $type ---\n";
foreach ($MO as $row => $cols) {
print "Row: $row ";
foreach ($cols as $k => $idx) {
print "($idx) ";
}
print " \n";
}
print "------------------\n";
}
print "</pre>\n";
# end of test code
//*/
// see if we need to fold the icon rows due to long text length
$doFoldRow = false; // don't assume we have to fold the row..
if($foldIconRow) {
$iTitleLen =0;
$iTempLen = 0;
$iCondLen = 0;
for($i=0;$i<$doNumIcons;$i++) {
$iTitleLen += strlen(strip_tags($WCforecasttitles[$i]));
$iCondLen += strlen(strip_tags($WCforecastcond[$i]));
$iTempLen += strlen(strip_tags($WCforecasttemp[$i]));
}
print "<!-- lengths title=$iTitleLen cond=$iCondLen temps=$iTempLen -->\n";
$maxChars = 135;
if($iTitleLen >= $maxChars or
$iCondLen >= $maxChars or
$iTempLen >= $maxChars ) {
print "<!-- folding icon row -->\n";
$doFoldRow = true;
}
}
if(isset($_REQUEST['foldrow']) and $_REQUEST['foldrow'] == 'y') {$doFoldRow = true;}
// set the icon display method based on settings
$iList = $iconOrder['S']; // assume single row
if($doFoldRow) {
$iList = $iconOrder['TR']; // folded row
}
if($iconRowDayNight) { // day-over-night selected
$iList = $iconOrder['DO']; // use day-first order;
if($firstIconPeriod == 'N') {
$iList = $iconOrder['NO']; // use night-first order
}
}
if($doDebug) {print "<!-- order of icons selected is ".print_r($iList,true)." -->\n";}
$wdth = round(100 / count($iList[0])); // set percentage width for <td> elements based on icon display method
$blankRow = array_fill(0,10,' <br/> ');
print "<tr>\n";
print " <td align=\"center\">\n";
print " <table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">\n";
// print the icons in the selected order
foreach ($iList as $k => $tRow) {
WC_printRow($WCforecasttitles,$tRow,'font-size: 8pt;');
WC_printRow($WCforecasticon,$tRow,'');
WC_printRow($WCforecastcond,$tRow,'');
WC_printRow($WCforecastrain,$tRow,'color: green;');
WC_printRow($WCforecastsnow,$tRow,'color: darkblue;');
WC_printRow($WCforecastthunder,$tRow,'');
WC_printRow($WCforecasttemp,$tRow,'');
WC_printRow($WCforecastuv,$tRow,'');
if(count($iList)>1) { # insert a blank row betweeen icon rows if needed
WC_printRow($blankRow,$tRow,'');
}
} // end of print icons loop ?>
</table><!-- end icon table -->
</td>
</tr>
<?php } // end print icons ?>
</table><!-- end header+icons table -->
<p> </p>
<?php } // end print header or icons
if ($PrintMode and $printText) {
// print the detail text forecasts ?>
<table style="border: 0" width="<?php print $maxWidth; ?>" class="WCforecast">
<?php
for ($i=0;$i<count($WCforecasttitles);$i++) {
print " <tr valign =\"top\" align=\"left\">\n";
if(!$doRTL) { // normal Left-to-right
print " <td style=\"width: 20%;\"><b>$WCforecasttitles[$i]</b><br /> <br /></td>\n";
print " <td style=\"width: 80%;\">$WCforecasttext[$i]</td>\n";
} else { // print RTL format
print " <td style=\"width: 80%; text-align: right;\">$WCforecasttext[$i]</td>\n";
print " <td style=\"width: 20%; text-align: right;\"><b>$WCforecasttitles[$i]</b><br /> <br /></td>\n";
}
print " </tr>\n";
}
?>
</table>
<?php } // end print text ?>
<?php if ($PrintMode) {
// print the required footer information ?>
<p> </p>
<?php
print '<p class="WCforecast">';
print langtransstr('WeatherUnderground forecast for'); ?>
<a href="https://www.wunderground.com/cgi-bin/findweather/getForecast?query=<?php echo $fileName; ?>">
<?php echo $WCforecastcity; ?></a>.
<?php if($iconType <> '.jpg') {
// print credit for .gif icons if used
print "<br/>";
print langtransstr('Animated forecast icons courtesy of');
print " <a href=\"https://www.meteotreviglio.com/\">www.meteotreviglio.com</a>.";
}
?>
</p>
<?php
} // end printmode
if (! $IncludeMode and $PrintMode ) { ?>
</body>
</html>
<?php
}
// END of main script -----------------------------------------------------------------------
// Functions --------------------------------------------------------------------------------
// get contents from one URL and return as string
function WC_fetchUrlWithoutHanging($url,$useFopen) {
global $Status, $needCookie;
$overall_start = time();
if (! $useFopen) {
// Set maximum number of seconds (can have floating-point) to wait for feed before displaying page without feed
$numberOfSeconds=4;
// Thanks to Curly from ricksturf.com for the cURL fetch functions
$data = '';
$domain = parse_url($url,PHP_URL_HOST);
$theURL = str_replace('nocache','?'.$overall_start,$url); // add cache-buster to URL if needed
$Status .= "<!-- curl fetching '$theURL' -->\n";
$ch = curl_init(); // initialize a cURL session
curl_setopt($ch, CURLOPT_URL, $theURL); // connect to provided URL
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // don't verify peer certificate
curl_setopt($ch, CURLOPT_USERAGENT,
'Mozilla/5.0 (WC-forecast.php - saratoga-weather.org)');
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $numberOfSeconds); // connection timeout
curl_setopt($ch, CURLOPT_TIMEOUT, $numberOfSeconds); // data timeout
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // return the data transfer
curl_setopt($ch, CURLOPT_NOBODY, false); // set nobody
curl_setopt($ch, CURLOPT_HEADER, true); // include header information
if (isset($needCookie[$domain])) {
curl_setopt($ch, $needCookie[$domain]); // set the cookie for this request
curl_setopt($ch, CURLOPT_COOKIESESSION, true); // and ignore prior cookies
$Status .= "<!-- cookie used '" . $needCookie[$domain] . "' for GET to $domain -->\n";
}
$data = curl_exec($ch); // execute session
if(curl_error($ch) <> '') { // IF there is an error
$Status .= "<!-- Error: ". curl_error($ch) ." -->\n"; // display error notice
}
$cinfo = curl_getinfo($ch); // get info on curl exec.
/*
curl info sample
Array
(
[url] => http://saratoga-weather.net/clientraw.txt
[content_type] => text/plain
[http_code] => 200
[header_size] => 266
[request_size] => 141
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 0.125
[namelookup_time] => 0.016
[connect_time] => 0.063
[pretransfer_time] => 0.063
[size_upload] => 0
[size_download] => 758
[speed_download] => 6064
[speed_upload] => 0
[download_content_length] => 758
[upload_content_length] => -1
[starttransfer_time] => 0.125
[redirect_time] => 0
[redirect_url] =>
[primary_ip] => 74.208.149.102
[certinfo] => Array
(
)
[primary_port] => 80
[local_ip] => 192.168.1.104
[local_port] => 54156
)
*/
$Status .= "<!-- HTTP stats: " .
" RC=".$cinfo['http_code'] .
" dest=".$cinfo['primary_ip'] ;
if(isset($cinfo['primary_port'])) {
$Status .= " port=".$cinfo['primary_port'] ;
}
if(isset($cinfo['local_ip'])) {
$Status .= " (from sce=" . $cinfo['local_ip'] . ")";
}
$Status .=
"\n Times:" .
" dns=".sprintf("%01.3f",round($cinfo['namelookup_time'],3)).
" conn=".sprintf("%01.3f",round($cinfo['connect_time'],3)).
" pxfer=".sprintf("%01.3f",round($cinfo['pretransfer_time'],3));
if($cinfo['total_time'] - $cinfo['pretransfer_time'] > 0.0000) {
$Status .=
" get=". sprintf("%01.3f",round($cinfo['total_time'] - $cinfo['pretransfer_time'],3));
}
$Status .= " total=".sprintf("%01.3f",round($cinfo['total_time'],3)) .
" secs -->\n";
//$Status .= "<!-- curl info\n".print_r($cinfo,true)." -->\n";
curl_close($ch); // close the cURL session
//$Status .= "<!-- raw data\n".$data."\n -->\n";
$i = strpos($data,"\r\n\r\n");
$headers = substr($data,0,$i);
$content = substr($data,$i+4);
if($cinfo['http_code'] <> 200) {
$Status .= "<!-- headers:\n".$headers."\n -->\n";
}
return $data; // return headers+contents
} else {
// print "<!-- using file_get_contents function -->\n";
$STRopts = array(
'http'=>array(
'method'=>"GET",
'protocol_version' => 1.1,
'header'=>"Cache-Control: no-cache, must-revalidate\r\n" .
"Cache-control: max-age=0\r\n" .
"Connection: close\r\n" .
"User-agent: Mozilla/5.0 (WC-forecast.php - saratoga-weather.org)\r\n" .
"Accept: text/plain,text/html\r\n"
),
'https'=>array(
'method'=>"GET",
'protocol_version' => 1.1,
'header'=>"Cache-Control: no-cache, must-revalidate\r\n" .
"Cache-control: max-age=0\r\n" .
"Connection: close\r\n" .
"User-agent: Mozilla/5.0 (WC-forecast.php - saratoga-weather.org)\r\n" .
"Accept: text/plain,text/html\r\n"
)
);
$STRcontext = stream_context_create($STRopts);
$T_start = WC_fetch_microtime();
$xml = file_get_contents($url,false,$STRcontext);
$T_close = WC_fetch_microtime();
$headerarray = get_headers($url,0);
$theaders = join("\r\n",$headerarray);
$xml = $theaders . "\r\n\r\n" . $xml;
$ms_total = sprintf("%01.3f",round($T_close - $T_start,3));
$Status .= "<!-- file_get_contents() stats: total=$ms_total secs -->\n";
$Status .= "<-- get_headers returns\n".$theaders."\n -->\n";
// print " file() stats: total=$ms_total secs.\n";
$overall_end = time();
$overall_elapsed = $overall_end - $overall_start;
$Status .= "<!-- fetch function elapsed= $overall_elapsed secs. -->\n";
// print "fetch function elapsed= $overall_elapsed secs.\n";
return($xml);
}
} // end WC_fetch_URL
// ------------------------------------------------------------------
function WC_fetch_microtime()
{
list($usec, $sec) = explode(" ", microtime());