<?php

	function runCurl($url, $params = null){
		$session = curl_init();
	
		curl_setopt($session, CURLOPT_HEADER, false);
		curl_setopt($session, CURLOPT_POST, true);
		curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($session, CURLOPT_URL, $url);
		if($params != null) {
			curl_setopt($session, CURLOPT_POSTFIELDS, $params);
		}
		$response = curl_exec($session);
		curl_close($session);		
		return $response;
	}
	

	$dlApiEndpoint = "http://www.gumband.com/send/send.php";
	$rssUrl = 'http://www.topix.net/rss/news/weird';	
	$outgoing_keyword = 'DISCUSS';
	$keyphrase = 'YOUR KEY PHRASE';

	/* incoming parameters */
	$keyword = $_GET['keyword'];
	$carrier = $_GET['network'];
	$handset = $_GET['smsfrom'];
	$smsmsg = $_GET['smsmsg'];
	

	/* get the rss feed */
	$xml = simplexml_load_file($rssUrl, 'SimpleXMLElement', LIBXML_NOCDATA);
	
	/* pick a random news item */
	$items = $xml->xpath('/rss/channel/item');
	$which = rand(0, sizeof($items)-1);
	
	/* create the message from title and description */
	$title = $items[$which]->xpath('title');
	$description = $items[$which]->xpath('description');
	$description = strip_tags($description[0]);
	$msg = $title[0] . ' - ' . substr($description, 0, 150 - strlen($title[0])+3);

	$checksum = md5($outgoing_keyword . $msg . $keyphrase);

	/* send the message */
	$curlResp = runCurl($dlApiEndpoint, array(
		'keyword' => $outgoing_keyword,
		'message' => $msg,
		'carrier' => $carrier,
		'handset' => $handset,
		'checksum' => $checksum
	));
	
	
?>