Post twitter tweets from php with tinyurl links

Author: admin / Category: Programming Secrets

My boss just asked me to look for a script, capable of adding content to a twitter page from a site’s database. Very important, the twitter post needs to contain links to the website posts. And because they need to be as short as possible, i used the tinyurl API to dynamically create the short version of the urls. Here is a very simple code how to post to twitter some content:

require(‘twitterAPI.php’);

$twitter_username =’YourTwitterUsername’;
$twitter_psw =’YourTwitterPassword’;

$url = ‘YourLink’;
$tweet = ‘YourTweet’;
$url = TinyURL($url);
$twitter_status=postToTwitter($twitter_username, $twitter_psw, $tweet.’ : ‘.$url);

function TinyURL($u){
return file_get_contents(‘http://tinyurl.com/api-create.php?url=’.$u);
}

You can download the TwitterApi from twitterAPI.php

Sort multi-dimensional array in PHP

Author: admin / Category: Programming Secrets

How to sort multi-dimensional arrays using PHP? Sorting one dimensional array is pretty easy, but to do this with two or multi-dimensional arrays, is a little different. Here is how to do it:

There’s a given 2 dimensional array:

$array[] = array(“id”=>1,”title”=>”Monkey”);
$array[] = array(“id”=>2,”title”=>”Alien”);
$array[] = array(“id”=>3,”title”=>”Stargate”);
$array[] = array(“id”=>4,”title”=>”New”);

We use the usort function of PHP:

usort($array, “cmp”);

… where cmp is the following function:

function cmp($a, $b){
return strcmp($a["title"], $b["title"]);
}

After sorting the $a array, it will look like this:

$array[0] = array(“id”=>2,”title”=>”Alien”);
$array[1] = array(“id”=>1,”title”=>”Monkey”);
$array[2] = array(“id”=>4,”title”=>”New”);
$array[3] = array(“id”=>3,”title”=>”Stargate”);

Now, the array is sorted by ‘title’ in ascending order. To sort in in descending order, just change the order of the compared elements in the cmp function:

return strcmp($b["title"], $a["title"]);

Create Excel file from PHP with tables using headers

Author: admin / Category: Programming Secrets

Did You ever needed to create pure and simple Excel spreadsheets from php? Are you tired with complicated classes, large and nonsense code? Here is a tiny code sample, showing how easy it  is to create Excel files directly from a php file. I won’t comment the lines, as the code is obvious…

$file_name = “myfile.xls”;

header(‘Pragma: public’);
header(‘Expires: 0′);
header(‘Cache-Control: must-revalidate, post-check=0, pre-check=0′);
header(‘Cache-Control: public’);
header(‘Content-Description: File Transfer’);
header(‘Content-Type: text/xls; name=”‘ . $file_name . ‘”‘);
header(‘Content-Disposition: attachment; filename=”‘ . $file_name . ‘”‘);
header(‘Content-Transfer-Encoding: binary’);

$content = ‘<table>
<tr>
<td>Header1 (A)</td>
<td>Header2 (B)</td>
<td>Header3 (C)</td>
</tr>
<tr>
<td>Content 1A</td>
<td>Content 1B</td>
<td>Content 1C</td>
</tr>
<tr>
<td>Content 2A</td>
<td>&nbsp;</td>
<td>Content 2C</td>
</tr>
</table>’;

echo $content;

Connect to strato.de with imap php functions

Author: admin / Category: Programming Secrets

Connecting to strato.de with imap isn’t easy at all, as there’s little documentations on the Internet, and they are mainly in dutch. So I will present in the following lines how you can access your e-mails from a php file.

$server = ‘{imap.strato.de:143}’;
$connection = imap_open($server, “youremail@yourdomain.nl”, “yourpassword”);

print_r(imap_errors());

$mailboxes = imap_list($connection, $server, ‘*’);

foreach($mailboxes as $mailbox)
$shortname = str_replace($server, ”, $mailbox);

imap_reopen($connection, $server.’INBOX’);

$count = imap_num_msg($connection);

for($i = 1; $i <= $count; $i++) {
$header = imap_headerinfo($connection, $i);
$raw_body = imap_body($connection, $i);
}

$server – the connection parameteres, differs from server to server. For exemple:

  • Google: {imap.gmail.com:993/ssl/novalidate-cert}
  • Google Apps: {imap.googlemail.com:993/ssl/novalidate-cert}

$connection: this is the IMAP stream

$mailboxes: will contain all the mailboxes you have (Inbox, Trash, Sent + persobal mailboxes)

$shortname: will contain just the name of the mailboxes, without their server address

imap-reopen: you can make a new stream, connecting to a given mailbox (INBOX in my case)

$count: how many mails do you have in the selected mailbox

- the last for cycle runs through the mailbox, and for every e-mail gets it’s header information ($header) and the mail’s content ($raw_body)


Powered by wordpress        .
.