Categories
PHP

Updating Twitter status using PHP CLI

Here’s the source file :

<?php
$username = $argv[1];
$password = $argv[2];
$message = $argv[3];
$tweetUrl = “http://{$username}:{$password}@www.twitter.com/statuses/update.xml”;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $tweetUrl);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, “status={$message}”);
$result = curl_exec($curl);
?>

How to Use ?
Save the file as “twitter.php”.

php twitter.php twitter_username twitter_password “Status Update !!”

NB: If you are posting a multi word status ( which has space ), don’t forget to wrap the sentence inside double quotes. Otherwise the first word of the sentence will be posted alone. The codes require cURL extension loaded.

Categories
PHP Python

It’s all about printf() and String Formatting

The printf() function was most probably introduced in C. The function, as most of the programmers know, prints formatted string according to a given format. PHP, being derived from C, has the same function with some added features. But in Python, the function is totally excluded 🙁

Python developers believe that there should be two seperate procedures to print a formatted string – format a string and then print it. So python has a different mechanism of doing the task.

Lets see some language specific examples:

C:

PHP:

Python: (Python 3)

Here, we format a string using the % (Modulo sign) or string formatting operator or the interpolation operator. If there was a single argument required for the formatting, we would simply pass it after the second modulo. Example:

s = “%s is a string ” % “maSnun”

But if we need to pass more than one argument to the formatting operator, we will have to pass a tuple of the arguments (like I have done in the first example). A tuple is an ordered collection which cannot be modified once it has been created. In other words, it’s rather like a read only array.

More About Tuples
You set up a tuple with round brackets (not
the square brackets you use to set up a list),
but you then access the individual members of
either with square brackets. That means that
you can use common code to process an ordered
collection, whether it’s a list or a tuple

demo = (1, 3, 6, 10, 15, 21, “lots”)

Categories
Python

Python: Encoding and Decoding Strings as Bytes

It’s simple!

Python IDE Entries:

It’s quite interesting. Last night, when I was using urlopen to access the TinyURL API, I found that the data was being returned as bytes. I had to decode it.