How to Tweet your Knol Updates (Page 3)

A Tutorial

Wouldn't it be super, if after you update a knol article, you could tweet your followers about it? Well now you can.

Authors

Written 2010 by Will Johnson Jr, you can email me at fft2001@aol.com
or post your comments for public view far below.
Creative Commons Attribution 3.0 License

<-- Back to How to Tweet your Knol Updates (Page 2)
--> Forward to How to Tweet your Knol Updates (Page 4)



Ok so far we've figured out that we can have a clickable image, that launches a PHP script, which reports some arguments we pass to it, as well as the referring URL, without needing to pass that URL.

Now what?  You've lost me totally.  Aren't we supposed to be updating Twitter already!  Get on with it!

OK OK don't push me.  I'm trying to do this slowly so we can all learn several things at once, not just one.


Since we have total control over what our local PHP script does, we can update our Twitter status using cURL.
Here is the generic code to do that:


<?php
$twitter_api_url = "http://twitter.com/statuses/update.xml";
$twitter_data = "status=I'm testing the ability to update twitter using PHP!";
$twitter_user = $username;
$twitter_password = $password;
$ch = curl_init($twitter_api_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $twitter_data);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERPWD, "{$twitter_user}:{$twitter_password}");
$twitter_data = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpcode != 200) {
echo "<strong>Don't Panic!</strong> Something went wrong, and the tweet wasn't posted correctly.";
}
echo "We're done";
?>

Don't worry about all the monkey business.  Like I said, you don't have to learn PHP to cut and paste a script.  We're going to need to change the $twitter_data variable to be the tweet we actually want to use.  Also, each of you will need to set the $username and $password to whatever your own Twitter login settings are. Just as if you're logging into Twitter manually.  You need to tell PHP how to log you in.


Do not worry that this data might be readable by some schmo, browsing to the page and trying to View Source.  PHP is pre-processed by the server, so View Source will only show them the result of the PHP actions, not the underlying PHP code itself.

So what we'd like to pass to Twitter now is something like this:

$twitter.data = "status=I just updated my article <a href = \"".getenv("HTTP_REFERER")."\">How to Tweet your Knol Updates</a>";

The \" here is to force that following double-quote mark to be taken literally in the string, instead of seen as an operator.  Typically a double-quote mark in a string, would mean either start the string, or end the string.  Here we want PHP to see it literally.

Now remember we have to pass that Title in, as there seems to be no other way to get it from Knol to the PHP script in order to update Twitter!  If you know of a way, tell me!

So like I said, we can do that right in the URL like this:

<a href="http://www.penguins.com/twitmyknol.php?Prefix=I_just_updated_my_article&Title=How_to_Tweet_your_Knol_Updates"><img src="http://s.twimg.com/a/1265999168/images/default_profile_1_normal.png"></a>

Each time we post this code to a Knol article, we just have to remember to change the Title in the code, to whatever the title of that Knol page is.

Now I don't really know if you can pass an href like I'm trying to do above, directly to Twitter!  I've never tried it until right now!  I swear!  Try it and let me know if this all works.


<-- Back to How to Tweet your Knol Updates (Page 2)
--> Forward to How to Tweet your Knol Updates (Page 4)