concise clean commented caching code


SUBMITTED BY: Guest

DATE: Oct. 1, 2012, 4:55 p.m.

FORMAT: PHP

SIZE: 4.4 kB

HITS: 2234

  1. <?php
  2. // Use Database?
  3. $USE_DATA = false;
  4. // Search zipcode
  5. $zip_code = $_GET[ ($_GET['zipcode2'] != NULL) ? 'locale' : 'city' ];
  6. // Connect to cache
  7. $cache = new Memcache;
  8. $cache_connected = $cache->connect('localhost', 11211);
  9. $cache_key = 'weather_zip_'.$zip_code;
  10. $cache_data = $cache->get($cache_key);
  11. if ($cache_data) {
  12. // There's a cache hit, nothing left to do.
  13. echo $cache_data;
  14. } else {
  15. // Get weather.com state code
  16. include_once( $USE_DATA ? 'database.php' : 'state_list.php' );
  17. // Fetch raw data from weather.com
  18. $request_url1 = 'http://wxdata.weather.com/wxdata/obs_hirad/'.$state_code.'.xml?key=47ff9b4c-91d5-11e0-b754-001c23d537c5&units=m&locale='.$_GET['locale'].'';
  19. $xml1 = simplexml_load_file($request_url1);
  20. $request_url2 = 'http://wxdata.weather.com/wxdata/df/'.$state_code.'.xml?key=47ff9b4c-91d5-11e0-b754-001c23d537c5&day=1,2,3,4,5,6&units=m&locale='.$_GET['locale'].'';
  21. $xml2 = simplexml_load_file($request_url2, NULL, LIBXML_NOCDATA);
  22. $request_url3 = 'http://wxdata.weather.com/wxdata/loc/'.$state_code.'.xml?key=47ff9b4c-91d5-11e0-b754-001c23d537c5&locale='.$_GET['locale'].'';
  23. $xml3 = simplexml_load_file($request_url3, NULL, LIBXML_NOCDATA);
  24. // Extract relevant data
  25. $forecast1 = $xml2->xpath("/forecasts/forecast");
  26. $forecast2 = $xml2->xpath("/forecasts/forecast/day");
  27. $current = $xml1->xpath("/observation");
  28. $name = $xml3->xpath("/location");
  29. // Record date & time
  30. date_default_timezone_set($name[0]->zoneInfo) ;
  31. $fecha = $xml1->xpath("/weatherdata/weather/current");
  32. $dia= date('d', time() + 0);
  33. $mes= date('m', time() + 0);
  34. $año= date('Y', time() + 0);
  35. $Hora= date('H:i',time() + 0);
  36. // If the cache is active, record output so we can store it
  37. if ($cache_connected) ob_start();
  38. // Generate the output
  39. ?>
  40. {"weatherinfo":{
  41. "sktq" : "<?= $forecast1[0]->maxTemp ?>\u00b0/<?= $forecast1[0]->minTemp ?>",
  42. "temp1" : "<?= $current[0]->temp ?>\u00b0",
  43. "temp2" : "<?= $forecast1[1]->maxTemp ?>\u00b0/<?= $forecast1[1]->minTemp ?>\u00b0",
  44. "temp3" : "<?= $forecast1[2]->maxTemp ?>\u00b0/<?= $forecast1[2]->minTemp ?>\u00b0",
  45. "temp4" : "<?= $forecast1[3]->maxTemp ?>\u00b0/<?= $forecast1[3]->minTemp ?>\u00b0",
  46. "temp5" : "<?= $forecast1[4]->maxTemp ?>\u00b0/<?= $forecast1[4]->minTemp ?>\u00b0",
  47. "temp6" : "<?= $forecast1[5]->maxTemp ?>\u00b0/<?= $forecast1[5]->minTemp ?>\u00b0",
  48. "weather1" : "<?= $current[0]->text ?>",
  49. "weather2" : "<?= $forecast2[1]->phrase ?>",
  50. "weather3" : "<?= $forecast2[2]->phrase ?>",
  51. "weather4" : "<?= $forecast2[3]->phrase ?>",
  52. "weather5" : "<?= $forecast2[4]->phrase ?>",
  53. "weather6" : "<?= $forecast2[5]->phrase ?>",
  54. "wind1" : "<?= $current[0]->wSpeed ?> kph <?= $current[0]->wDirText ?>",
  55. "wind2" : "<?= $forecast2[0]->wSpeed ?> kph <?= $forecast2[0]->wDirText ?>",
  56. "wind3" : "<?= $forecast2[1]->wSpeed ?> kph <?= $forecast2[1]->wDirText ?>",
  57. "wind4" : "<?= $forecast2[2]->wSpeed ?> kph <?= $forecast2[2]->wDirText ?>",
  58. "wind5" : "<?= $forecast2[3]->wSpeed ?> kph <?= $forecast2[3]->wDirText ?>",
  59. "wind6" : "<?= $forecast2[4]->wSpeed ?> kph <?= $forecast2[4]->wDirText ?>",
  60. "pressure" : "<?= $current[0]->pressureDescription?>",
  61. "uvText" : "<?= $current[0]->uvText?>",
  62. "humid" : "<?= $current[0]->humid?>%",
  63. "visibility": "<?= $current[0]->visibility?> km",
  64. "cityid" : "<?php echo $_GET['city'];?>",
  65. "city_en" : "<?= $name[0]->name ?>",
  66. "city" : "<?= $name[0]->name ?>, <?= $name[0]['countryCode'] ?>",
  67. "date_y" : "<?php echo"".$año;?>\u5e74<?php echo"".$mes;?>\u6708<?php echo"".$dia;?>\u65e5",
  68. "sktime" : "<?php echo"".$Hora;?>",
  69. "pubtime" : "<?php echo"".$año;?>-<?php echo"".$mes;?>-<?php echo"".$dia;?> <?php echo"".$Hora;?>"}}
  70. <?php
  71. if($cache_connected) {
  72. // Store the output
  73. $cache_data = ob_get_flush();
  74. $cache->set($cache_key, $cache_data, 0, 10);
  75. $cache->close();
  76. }
  77. }
  78. ?>

comments powered by Disqus