◄ Older Entries | RSS

Receiving and Sending Text Messages With PHP and IMAP

0


I've had a lot of requests to turn some of my Twitter apps into mobile services. Most companies, like Twitter itself, use Short Codes (like 40404) to accept and send txt/sms messages. Short codes are very expensive (the cheapest solution I found started at $500 per month for a randomized short code of up to 8 digits). I knew there had to be a "poor man's" way around buying a short code so I made one using PHP and IMAP.

I've only had around 200 people use it thus far but it seems to be working nicely. It's rough and can use a lot more work, but that's up to you.
You need to setup an email address for mobile users to send their messages to (I suggest m@yourdomain.com for simplicity).

 
<?php
$mail=@imap_open("HOST","USERNAME","PASSWORD") or die("Can't connect: " . imap_last_error());
 
$mails=imap_num_msg($mail);
if($mails==0) { die('No new texts.'); } else {
	echo "There are $mails new texts";
	for($i=1;$i<=$mails;$i++) {
		$chead=imap_headerinfo($mail,$i);
		$mid=ltrim($chead->Msgno);
		/* Attempt to remove some of the garb headers and such that
		some providers and phones send with messages */
		$tweet = substr(imap_body($mail,$mid), 0, 144);
		list($tweet, $trash) = split('[=]', $tweet);
		list($tweet, $trash) = split('Sent from', $tweet);
 
		//Now that we have the message from the user's phone, send out a return message.
		$from1 = $chead->from; //1 due to the probability of having a $from var already defined in your script.
		foreach ($from1 as $id => $object) {
			$toaddress = $object->mailbox . "@" . $object->host;
		}
 
		//Send the text message
		echo "Message Received: $tweet  Send to $toaddress";
		mail("$toaddress", "Your message receipt!", "Your tweet has been received.");
		echo "
<hr />";
	}
}
 
imap_close($mail);
?>
 

The only hitch is that some phones don't allow users to input symbols, or sometimes even letteres, into the to box for a text message. There's a way around that, too (if your users only think to do it). They simply need to add your email address as a contact prior to creating the message. It's the matter of simply selecting a contact to send the message to, then.

Another way around this is to send the message to the provider's SMS email gateway (I don't know all, but AT&T's is 36245). Then, in the body of the message, type "m@yourdomain.com MESSAGE HERE". The provider will automatically strip the email address from the start of the txt and send the message, minus the email address at the beginning, to said email address.

It's the long way around and I'm sure there is a simpler solution. This is my solution and it works fine for what I need it to do.

Read More


Push RSS feed to your Twitter feed

1


A friend needed to push/send items from an RSS feed to his Twitter account.

Here's the code in a nice .zip file. Very simple and rough, but it gets the job done nicely. Feel free to add to/modify.
http://kevinsmithdesigns.com/blog/wp-content/uploads/KSDtwitter.zip

Read More


Getting some press

1


My latest project, SecretTweet.com, has been getting noticed by a few major names in the world of news.

LA Times:
http://latimesblogs.latimes.com/jacketcopy/2009/02/like-postsecret.html

New York Times:
http://bits.blogs.nytimes.com/2009/02/27/anonymous-confessions-go-online-with-secrettweet/

And it was on Fox News, too:
YouTube Preview Image

Read More


An interview with Shouts & Hollers

0


Shouts & Hollers is an online "ezine" of sorts that is published by Charleston, WV, area bloggers.  Bill Gardner got in contact with me to answer a few questions regarding a recent sideproject of mine.  Read it here.

Thanks Bill!

Read More


Masking PHP via .htaccess

1


What we're doing: turning the .php extension into .whatever
Why we're doing this: security, seo, or because you want to (then you'll have to answer why for yourself)
How to do this:

In your .htaccess file, we're going to tell apache to parse your choice of a single or multiple .whatevers as php.
So if we wanted to parse .html files as php, we would do something like this:

AddType application/x-httpd-php .html

And if we wanted to parse .bunnyboyfoofoo files as .php:

AddType application/x-httpd-php .bunnyboyfoofoo

And if we wanted to parse .bunnyboyfoofoo, .html, .htm, .py, .kevinsmith, .google files through php:

AddType application/x-httpd-php .bunnyboyfoofoo .html .htm .py .kevinsmith .google

Simple and fun to play with if you're that type of person.

PS:  Sorry it has been so long since my last update...it's summer, I've been enjoying it.

Read More


301 Permanent Redirect PHP and HTACCESS

0


Just to compare two of the different ways to effectively do a proper redirect...

PHP:
(old.php)

<?
header ('HTTP/1.1 301 Moved Permanently');
header('Location: http://kevinsmithdesigns.com/new.php');
exit;
?>

.HTACCESS

 
Redirect 301 /old.php http://www.kevinsmithdesigns.com/new.php
 

--------------
I much prefer .htaccess so you can delete old.php and keep track of all redirects via one file (.htaccess).
.htaccess is also probably the safest way to redirect (if you're the SEO conscious type)

Read More


Javascript onfocus-onblur, show-hide table-div

2


For an example, visit SecretTweet.com and click in the textbox up top--the only textbox on the whole page. The captcha, tags field, language field, and submit button all are hidden until needed by the user.

<html>
<head>
<script type="text/JavaScript">
<!--
function showthetable(theTable)
{
      if (document.getElementById(theTable).style.display == 'none')
      {
            document.getElementById(theTable).style.display = 'block';
      }
}
//-->
</script>
</head>
 
<body>
<textarea name="whatever" cols="55" rows="2" onfocus="showthetable('TABLENAME');return true;"></textarea>
 
<div class="options">
<table border="0" cellpadding="0" cellspacing="0">
<tr id="TABLENAME" style="display: none;">
<td>
Hidden content until onfocus!  Hooray!
  </td>
</table>
</div>
 

Read More


Javascript Expand/Collapse Element

0


Note: This piece of code wasn't entirely created by me. Also, I don't claim to be knowledgeable in javascript...I'm just sharing.

function expandCollapse() {
for (var i=0; i<expandCollapse. »
arguments.length; i++) {
var element = document.getElementById »
(expandCollapse.arguments[i]);
element.style.display = (element.style. »
display == "none") ? "block" : "none";
	}
}

Which will allow you to expand and collapse a div in css. I needed this for a new addition to VisitMatewan.com that I'm working on. Handy....

Read More


Use Sticky Forms!

0


If a user inputs information that generates an error OR if he/she leaves a field blank, save them time (and increase your turnover) by using sticky forms. It's so simple and improves usability so much...

Let's say we're collecting first and last names (to keep it simple):

 
form.php
<?php
if (isset($_POST['submitted'])) {
 
		// Print the results.
		echo '
<h1>Your Name:</h1>
 
First: ' . $_POST['first'] . ' Last: ' . $_POST['last'] . '
 
';
 
	} else { // Invalid submitted values.
		echo '
<h1>Error!</h1>
 
Please enter a valid name.
 
';
	}
?>
<h2>Your Name</h2>
<form action="form.php" method="post">
 
First:
<input type="text" name="first" size="30" maxlength="40" value="<?php if (isset($_POST['first'])) echo $_POST['first']; ?>" />
 
Last:
<input type="text" name="last" size="30" maxlength="40" value="<?php if (isset($_POST['last'])) echo $_POST['last']; ?>" />
<input type="submit" name="submit" value="Submit!" />
<input type="hidden" name="submitted" value="TRUE" />
</form>
 

So, if a user inputs a first name but no last name (or last but no first), it will show whatever the user has already typed (and preventing them from having to type it again)...if the form is lengthy, this is very handy and such a time saver.

Read More


Multidimensional Arrays

0


Multidimensional arrays are basically arrays within arrays. So, let's make one.

 
<?
//Our first array is going to be for dairy products:
$dairy = array(1 =&gt; 'Milk','Cheese','Yogurt');//Our second array is going to be for animals:
$animals = array(1 =&gt; 'Cow','Duck','Horse','Chicken');
 
//Now let's pretend we wanted to combine these arrays (I should've chosen something like Months and Years or States and Territories...but I'm sticking with dairy products and farm animals ;) ....)
$combined = array('Dairy' =&gt; $dairy, 'Animals' =&gt; $animals);
 
//Idea: This is the best way to categorize interests for personalized profiles etc....
//Echoing what we've just done...
echo "I want to echo Cow!  {$combined['animals']['Cow']}";
 
?>

Of course, if you're just wanting to echo Cow, just use echo "Cow";...but if you're building things from arrays like drop down menus or if you're collecting interests, this is helpful.

Read More


◄ Older Entries | RSS