Member Sign In
Not a member?

A Wired.com user account lets you create, edit and comment on Webmonkey articles. You will also be able to contribute to the Wired How-To Wiki and comment on news stories at Wired.com.


It's fast and free.

Sign in with OpenID
Sign In
Webmonkey is a property of Wired Digital.
processing...
Join Webmonkey

Please send me occasional e-mail updates about new features and special offers from Wired/Webmonkey.
Yes No

Please send occasional e-mail offers from Wired/Webmonkey affiliated web sites and publications, and carefully selected companies.
Yes No

I understand and agree that registration on or use of this site constitutes agreement to Webmonkey's User Agreement and Privacy Policy.
Webmonkey is a property of Wired Digital.
processing...

Retrieve Sign In

Please enter your e-mail address or username below. Your username and password will be sent to the e-mail address you provided us.

or
Webmonkey is a property of Wired Digital.
processing...

Welcome to Webmonkey

A private profile page has been created for you.
As a member of Webmonkey, you can now:
  • edit articles
  • add to the code library
  • design and write a tutorial
  • comment on any Webmonkey article
Close
Webmonkey is a property of Wired Digital.

Sign In Information Sent

An e-mail has been sent to the e-mail address registered in this account.
If you cannot find it in your in-box, please check your bulk or junk folders.
Sign In
Webmonkey is a property of Wired Digital.

Sunrise Sunset SMS

/skill level/
/viewed/
0 Times

This is the code we used to create an SMS notification app that returns Sunrise/Sunset information to any user that requests it. The request is sent to the service from the users' phone via a shortcode. The user submits a city and a state, a ZIP code, or an exact address. We hit up a couple of public APIs to determine what the sunrise and sunset times are for that location, then wrap those times in a text message and mail it back.

Here is the full tutorial on Webmonkey: Build an SMS Notification App


The code

<?
/*** REMEMBER to use your own API key ***/
$apikey = "yourkeyhere";

// Get lat/long from Google HTTP Geocoder
$city = $_GET["city"];
$city = urlencode($city);
$geourl = "http://maps.google.com/maps/geo?q=$city&output=csv&key=$apikey";
$csvContent = get_url($geourl);
list($status, $accuracy, $lat, $long) = split(",", $csvContent);

if ($status != 200)
{
  display_output("Eek! Couldn't determine where you are based on: $city");
}
else
{
  // Get time zone from Earthtools
  // http://www.earthtools.org/webservices.htm
  $timeurl = "http://www.earthtools.org/timezone/$lat/$long";
  $xmlContent = get_url($timeurl);
  $xmlObject = simplexml_load_string($xmlContent);
  $zone = $xmlObject->offset;
  $localtime = reformat_time($xmlObject->localtime);
  $dst = dst_to_number($xmlObject->dst);
  
  // Get sunrise/sunset data from Earthtools
  // http://www.earthtools.org/webservices.htm
  $daymonth = date("j/n");
  $sunurl = "http://www.earthtools.org/sun/$lat/$long/$daymonth/$zone/$dst";
  $xmlContent = get_url($sunurl);
  $xmlObject = simplexml_load_string($xmlContent);
  $sunrise = reformat_time($xmlObject->morning->sunrise);
  $sunset = reformat_time($xmlObject->evening->sunset);
  
  // Print out sunrise/sunset msg
  display_output("Sunrise: $sunrise\nSunset: $sunset\n\nbtw, we think it's $localtime where you are. Adjust accordingly.");
}


/*** Functions used by the rest of the app go here ***/
function display_output($content)
{
  print header("Content-type: text/plain");
  print $content;
  exit;
}
function reformat_time($time, $fmt="g:i A")
{
  return date($fmt, strtotime($time));
}
function dst_to_number($dst)
{
  if ($dst == "True")
  {
    return 1;
  }
  else
  {
    return 0;
  }
}
function get_url($url)
{
  // Create cUrl object to grab XML content using passed variable $url
  $c = curl_init();
  curl_setopt($c, CURLOPT_URL, $url);
  curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
  $content = trim(curl_exec($c));
  curl_close($c);

  return $content;
}
?>

An example of the code in action

  • This page was last modified 23:09, 16 September 2008.
Edit this article
Reddit Digg
 

/related_articles/

See more related articles

Subscribe now

Special Offer For Webmonkey Users

WIRED magazine:
The first word on how technology is changing our world.

Subscribe for just $10 a year