#!/usr/bin/perl use strict; use warnings; use DBI; use CGI; use JSON::XS; # Naively simple web service!? my $cgi = new CGI; my $auth_code = "XD9920399fldkkxzmfoJfkYYUS2"; my $lookup = $cgi->param("lookup"); my $client_key = $cgi->param("client_key"); my $print; # Expand this later to return unique error for each possible omission if ( (! defined $lookup) || (! defined $client_key) || ($client_key ne $auth_code) ) { $print = encode_json {error => 'You must provide a lookup and valid client key'} } else{ my $dbh = new_dbh(); my $sth = $dbh->prepare("SELECT title,next_episode FROM watcher WHERE userid = ?"); $sth->execute($lookup); $print = encode_json $sth->fetchrow_hashref(); } print "Content-type: text/html\n\n"; # Send back the Json string print "$print"; sub new_dbh { my $db_driver = "mysql"; my $db_name = "shows"; my $db_host = "localhost"; my $db_port = "3306"; my $db_user = "root"; my $db_passwd = "xxxxxx"; my $dbh= DBI->connect("DBI:$db_driver:dbname=$db_name;host=$db_host;port=$db_port", $db_user, $db_passwd,{RaiseError => 1, mysql_enable_utf8 => 1, }); $dbh->{TraceLevel} = 2; # disable when live return $dbh; }