What's the gogo my mofo?

Tutorial – Let Your Readers SMS You

March 8th, 2009 Filed under: Web | Tags: , ,

This is how I made it possible for readers of my blog to text message / SMS my phone for free from my wordpress blog. It’s a bit of a hack going through twitter and twe2, but it works. If you wanted to you could just have it so that readers can send you a message on Twitter instead.

First you’ll need two twitter accounts. One that you usually use and one for your website. We are going to be using the Twitter API to send a direct message to your normal twitter account. Twitter doesn’t allow you to send a direct message to yourself, so you will need to have a second account for your site which we will use to send the message from.

Now if you are setting this up on a wordpress site you are going to have to install the Exec-PHP plugin. This plugin just lets you post php into a normal wordpress text widget or a wordpress post/page. We will be using a bit of PHP to send the message to Twitter.

Use the HTML tab!

Use the HTML tab!

Now that that is out of the way we can get down to business. You will need to post this code where you would like the “Send me a text message” form to be displayed. I put mine one a separate wordpress page. NB. If you are putting it onto a page or post in wordpress remember to enter it into the ‘HTML’ editor not the ‘Visual’ WYSIWYG editor. If you even click on the ‘Visual’ editor tab once the code has been entered it will change the code and make it useless, causing you to start again.

Here is the code:

<?php
if (isset($_POST['submit'])) {

$twitter_status = $_POST['twitter_stat'];

if (strlen($twitter_status) > 0) {

// Set username and password etc
$username = ‘enter_site_twitter_username‘;
$password = ‘enter_site_twitter_password‘;
$sendto = ‘enter_your_twitter_username ‘;
$direct = ‘d ‘;
$message = $direct . $sendto . $twitter_status;
// The twitter API address
$url = ‘http://twitter.com/statuses/update.xml’;
// Alternative JSON version
// $url = ‘http://twitter.com/statuses/update.json’;
// Set up and execute the curl process
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, “$url”);
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_POST, 1);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, “status=$message”);
curl_setopt($curl_handle, CURLOPT_USERPWD, “$username:$password”);
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);
// check for success or failure
if (empty($buffer)) {
echo ‘message’;
} else {
echo ‘Thanks for the message.’;
}

} else
echo “<p>Error: I won’t send blank messages!</p>”;
}
?>

<h2>Send me a message:</h2>

<p><strong>What’s so important?</strong></p>
<form action=”<?php echo $_SERVER['PHP_SELF']; ?>” method=”post”>
<input name=”twitter_stat” type=”text” id=”twitter_stat” size=”40″ maxlength=”120″ />
<input type=”submit” name=”submit” id=”submit” value=”Send”/>
</form>

I have highlighted the text that you need to change red. We will walk through those changes now. enter_site_twitter_username will need to be changed to your sites twitter username (remember you have 2, one for your site and one for you).

enter_site_twitter_password will be changed to your sites twitter password. The password is needed because the code rest of the code uses it to log on to twitter and send a message. Don’t worry no one will be able to see the password.

enter_your_twitter_username will be changed to your real twitter username. This will be added to the start of the message your readers write so that it gets sent to your twitter account. Please remember to leave the empty space after your username. eg. ‘whataboutki ‘ = good       ‘whataboutki’ = bad

<?php echo $_SERVER['PHP_SELF']; ?> this should be able to be left just the way it is if you are putting the code into a wordpress text widget. If you are putting it into a wordpress post or page then this will have to be replaced with the url of the post or page eg. http://www.whataboutki.com/text-sms

Should look like this.

Should look like this.

Once you have made the changes you should have a form that looks like this one your site. If you test it out it should send the message you type as a direct message to your normal twitter account from your sites twitter account. Yeah!

Now you just need to get twitter to send that message to your phone. If you are in america then twitter can do this for you. Charges might apply. if you aren’t then you can set up a twe2 account. Twe2 will text your twitter direct messages and replies to your phone (most countries) free of charge. It uses another service Wadja to do this, so every message will have ‘> FREE SMS WWW.WADJA.COM‘ at the end.

Hope this helps some people out there. I know this stuff can be tricky and trust me I don’t really know what I’m doing either. Just playing around, it’s the best way to learn. Anywho if you know of a better way to do something I use in this tutorial or need something explained a bit better just leave a comment.

Also, I understand that this opens you up to the risk of spam. I hope to add a Captcha type thing to it at some stage.


Add Your Own Comment