Jun 22 2009

Update Twitter using Command Line, Javascript, Or PHP.

Everyone seems to be all about Twitter so here’s some simple examples of how to update your Twitter status from a command line prompt, web server or simple html web site. These three examples require curl so install it if you don’t already have it. For these examples I’ll be using my Twitter user name ‘codytaylor1234’. My password is not ‘mypassword’ so make sure you put in your own information.

The easiest way to update your Twitter account is to just call curl from the command line with this command.


curl --basic --user "codytaylor1234:mypassword" --data-ascii 
"status=This Twitter update brought to you by curl on the command line" 
"http://twitter.com/statuses/update.json"


To update your Twitter status with PHP you are going to want to do the same sort of thing but with a bit more typing.

<?php
$username = 'codytaylor1234';
$password = 'mypassword';

$update = 'This Twitter update is from a php script using curl';

$url = 'http://twitter.com/statuses/update.json';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "$url");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "status=".$update);
curl_setopt($ch, CURLOPT_USERPWD, $username.":".$password);
$result = curl_exec($ch);
curl_close($ch);

if($result)
    echo 'success';

?>;

Since a cross-domain request in Javascript isn’t really an option we have to create a proxy using PHP in order to authenticate the user on the Twitter API. If anyone knows an easy way authenticate a Twitter user using only javascript I’d love to hear it. Anyway if we replace a small amount of code in the above example and put it in a file then we can use a simple ajax request to update our Twitter status. So the new PHP file would be:


<?php
$username = $_POST['username'];
$password = $_POST['password'];

$update = $_POST['update'];

$url = 'http://twitter.com/statuses/update.json';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "$url");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "status=".$update);
curl_setopt($ch, CURLOPT_USERPWD, $username.":".$password);
$result = curl_exec($ch);
curl_close($ch);

?>

For this example I called that php file ‘twitter-update.php’. Now that we have our simple proxy we can update our twitter status with a simple html form and a little ajax. I used the prototype framework for my javascript.


<script src="includes/prototype.js" type="text/javascript">
<script type='text/javascript'>
function update_twitter()
{
  var param_string = "username="+$('username').value+"&password="+
        $('password').value+"&update="+$('update').value;
  alert(param_string);
  var options = {
    method: "post",
    parameters: param_string,
    onSuccess: function (xhr, Json) {
      alert("Response received successfully.");
    },
    onFailure: function (xhr, Json) {
      alert("Request was unsuccessful.");
    }
  };
  
  var oRequest = new Ajax.Request("twitter-update.php", options);
}

</script>

Obviously this is for example purposes only and if you’re actually using it for production then you should edit it a lot. Now for the last little bit here’s the simple html form that starts it all.


User Name : <input type='text' id='username' value='codytaylor1234'><br>
Password  : <input type='password' id='password' value='mypassword'><br>
New Status : <input type='text' id='update' 
value='Twitter Update from html/javascript/php'><br>
<input type='button' value="Update Twitter" onclick='update_twitter();'>

Share

Jun 20 2009

Get Integer Bit Parity In PHP

I was forced to use php to generate a cyclic redundancy check (CRC) byte earlier this week. When generating this CRC I needed to check the bit parity of a byte. At first I thought that there would be an example that I could just cut and paste into my code or a library including the function somewhere. I guess that most PHP users don’t really have to work with bits and bytes very often because I couldn’t find an example of doing this. Granted I didn’t really search that hard because writing it was pretty simple. But hopefully the next guy searching for this actually finds this example.

Most of the time we don’t really have to think about types in php. But if you’re dealing with this kind of thing then you usually want to know what you’re working with. This php function example will only work with integers and will return a value of -1 if the first parameter is not a number. The function returns 1 if the number of bits set in the number pased in is odd. Otherwise it will return 0. The second parameter defaults to 4 because that is the number of bytes in a normal php integer. In php if a number becomes larger than an integer can handle, which is called an overflow, it becomes a float. The default integer size is platform dependant but it’s usually 4 bytes. It can be set using the constant PHP_INT_SIZE if you need to change it.


<?php

function odd_parity($something,$number_of_bytes_to_check=4,$double_check=0)
{
    //test to make sure it's a number
    if(!is_numeric($something)) {return -1;}  

    $setbits = 0;

    //get the number of bits that are set
    for($i=0;$i<$number_of_bytes_to_check*8;$i++)
    {
        if($something & (1<<$i)) $setbits++;
    }

    //check if the number of bits set is even or odd   
    $result =  $setbits % 2;
   
    //spit out the results to double check it's working 
    if($double_check)
    echo   "<br>Parity of : ".$something." is ".
    $result." In Binary : ".decbin((int)$something).
    " Checking the right ".($number_of_bytes_to_check*8).
    " Bits";

    return $result;
}

//will return 0 because it's defaulting to 4 bytes
echo odd_parity(0x0100010001);  

?>

Share

May 14 2009

Iphone iwebkit Example

Create Custom Iphone or Ipod Touch Web Applications Using iWebKit

For the impatient people like myself, heres what it looks like. You want to point your iphone browser to this location and hit the ‘+’ button at the bottom and select add to the homescreen. This will get rid of the navigation bar on in safari and make the iwebkit example look like a native iphone app.

Here are the steps to take in order to get this working.
Get the frame work: iWebKit framework (I used 4.5.3 for this guide)
Extract the iwebkit and take a look at the manual. Iwebkit comes with a few examples of how to use the key framework elements.We’re going to make a very rudimentary application for the purpose of iphone iwebkit example. Ajax integration with iwebkit will come later.

First you will need to create a directory on your web server and extract the iwebkit files into it.
The webkit contains a few examples but most of them didn’t show up properly for me when I used the Iphone.
Most webapps are built with just really basic html. The custom elements that iwebkit provides are called by normal divs with certain ids.

First start with an extremely basic html skeleton page :

<html>
<head>
<meta content=”yes” name=”apple-mobile-web-app-capable” />
<meta content=”text/html; charset=iso-8859-1″ http-equiv=”Content-Type” />
<meta content=”minimum-scale=1.0, width=device-width, maximum-scale=0.6667, user-scalable=no” name=”viewport” />
<title>iphone iwebkit example</title>
</head>
<body>

</body>
</html>

Hopefully no explanations are needed there. Notice the meta tags? So now we need to start adding the iphone javascript and css.
To get started with iwebkit for the iphone or iphone touch you only need to include two files into this basic html page but you will need to take all four folders from the Framework folder and put them into the directory where you want to host your web app.
Add these two lines to the head portion of the skeleton html file outlined above.

<link href=”css/style.css” rel=”stylesheet” type=”text/css” />
<script src=”javascript/functions.js” type=”text/javascript”></script>

Now to start putting the application together using iwebkits basic elements.

Top Bar
This section will outline the options for the navigation bar at the top of the page on the iphone or ipod touch.
Using iwebkit for the top bar we will normally put navagation buttons and a title like this.

<div id=’topbar’>
<div id=”leftnav”>
<a href=”index.html”><img alt=”home” src=”images/home.png”/></a>
</div>
<div id=’title’>
iphone iwebkit example
</div>
<div id=”rightnav”>
<a href=”index.html”>Next</a>
</div>
</div>

There are four div elements there that make up this top bar. The first is the bar itself and it encompasses the other three elements.
The second is the leftnav div. This specifies that the elements within this div are to appear at the left side of the bar (Big surprise).
The third is the title. The fourth is the rightnav which will appear on the right side. You’re probably going to want to put links and pictures in the leftnav and rightnav divs.
Instead of the rightnav div it is possible to put a rectangle button instead of the arrow button just by changing rightnav to rightbutton in the id.

Content
This section is for basically anything you want to put in your iwebkit page. I’m going to demonstrate a simple iphone-looking list here:

<div id=”content”>
<span class=”graytitle”>Iphone iwebkit example</span>
<ul class=”pageitem”>
<li class=”textbox”>
<p>Some Text for the iwebkit example</p>
<p>More text for the Iphone</p>
</li>
<li class=”textbox”>
<span class=”header”>Iphone iwebkit</span>
<p>Iphone Iphone</p><p>WebKit WebKit</p>
</li>
<li class=”menu”>
<a href=”index.php”>
<img alt=”Description” src=”thumbs/basics.png” />
<span class=”name”>Iphone iwebkit Example</span>
<span class=”comment”>Comment about iwebkit</span>
<span class=”arrow”></span>
</a>
</li>
<li class=”store”>
<a class=”noeffect” href=”index.php”>
<span class=”image” style=”background-image: url(‘image.jpg’)”></span>
<span class=”name”>Iphone Song</span>
<span class=”comment”>iWebKit Comment</span>
<img alt=”rating” class=”stars” src=”images/4stars.png” />
<span class=”starcomment”>13 Reviews</span>
<span class=”arrow”></span>
</a>
</li>

</ul>
</div>

There are a lot of options when using iwebkit. Most are pretty self explanatory, like the <ul> class and the <span> and <li> classes, but who wants to remember all that.
So go check the source of some feature or look that you want on your iwebkit page at <a href=’http://m.iwebkit.net/index.html’>this demo</a>.

The first issue I had when looking at my iwebkit example on the iphone was that the example iwebkit application wasn’t fullscreen in safari.
The only way that I’ve found so far to make this web application look like a native iphone application is to go to the page in safari and then hit the ‘+’ button at the bottom and select add to the homescreen.
This will get rid of the navigation bar on the iphone and make the iwebkit example look like a native iphone app.

This example and the full source for this iwebkit iphone example  can be looked at here.

Share