PHP script for getting latest Tweets of a user

There are so many different ways to get tweets to display on your blog or web page, unfortunately most of these use JavaScript which means that the tweets displayed are not made available to search engines 🙁

 

Don’t worry; you can also display your latest Twitter tweets using PHP. You can achieve by following very simple functions.

 

function getLastestTweets fetches latest tweets

It require 2 parameters
$userid = User Id of the twitter account
$x         = number of tweets to be fetched

[php]
<?php

function getLastestTweets($userid,$x){
$url = "http://twitter.com/statuses/user_timeline/$userid.xml?count=$x&quot";
$xml = simplexml_load_file($url) or die(‘could not connect’);
echo ‘<ul>’;
foreach($xml->status as $status){
$text = twitterify( $status->text );
echo ‘<li>’.utf8_decode($text).'</li>’;

$time = dateDiffForTweet(date("Y-m-d H:i:s"),date("Y-m-d H:i:s",strtotime($status->created_at)));
if(!sizeof($time)){ $time[0]= ‘1 second’; }
echo " ".$time[0]." ago";
}
echo ‘</ul>’;
}

?>
[/php]

function twitterify automatically converts hashtags (like #scriptarticle) to tag links, @links to author links (like @mahesh), and autolinks URIs (like http://scriptarticle.com ) to links.

[php]
<?php

function twitterify($ret) {
$ret = preg_replace("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t< ]*)#", "\\1<a href=\"\\2\" >\\2</a>", $ret);
$ret = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r< ]*)#", "\\1<a href=\"http://\\2\" >\\2</a>", $ret);
$ret = preg_replace("/@(\w+)/", "<a href=\"http://www.twitter.com/\\1\" >@\\1</a>", $ret);
$ret = preg_replace("/#(\w+)/", "<a href=\"http://search.twitter.com/search?q=\\1\" >#\\1</a>", $ret);
return $ret;
}

?>
[/php]

function dateDiffForTweet is used for showing time difference between tweet posted and current, like 2 mins ago,2 days ago

[php]
<?php

function dateDiffForTweet($time1, $time2, $precision = 6) {
// If not numeric then convert texts to unix timestamps
if (!is_int($time1)) {
$time1 = strtotime($time1);
}
if (!is_int($time2)) {
$time2 = strtotime($time2);
}

// If time1 is bigger than time2
// Then swap time1 and time2
if ($time1 > $time2) {
$ttime = $time1;
$time1 = $time2;
$time2 = $ttime;
}

// Set up intervals and diffs arrays
$intervals = array(‘year’,’month’,’day’,’hour’,’minute’,’second’);
$diffs = array();

// Loop thru all intervals
foreach ($intervals as $interval) {
// Set default diff to 0
$diffs[$interval] = 0;
// Create temp time from time1 and interval
$ttime = strtotime("+1 " . $interval, $time1);
// Loop until temp time is smaller than time2
while ($time2 >= $ttime) {
$time1 = $ttime;
$diffs[$interval]++;
// Create new temp time from time1 and interval
$ttime = strtotime("+1 " . $interval, $time1);
}
}

$count = 0;
$times = array();
// Loop thru all diffs
foreach ($diffs as $interval => $value) {
// Break if we have needed precission
if ($count >= $precision) {
break;
}
// Add value and interval
// if value is bigger than 0
if ($value > 0) {
// Add s if value is not 1
if ($value != 1) {
$interval .= "s";
}
// Add value and interval to times array
$times[] = $value . " " . $interval;
$count++;
}
}

return $times;
// Return string with times
//return $times;
// return implode(", ", $times);
}

?>
[/php]

Here is the way; you can display your tweets

[php]
<?php

//my userid scriptarticle
getLastestTweets(‘scriptarticle’,5);

?>
[/php]

If you face any issue to implement the code just left a comments below the post.

Remove index.php from CodeIgniter URL Path using .htaccess

If you are using Codeigniter (PHP Framework) you are noticed that by default index.php will be included with your URL.

 

But you can easily remove index.php from your URL so that your URL should be like : http://yourdomainname.com/controller/action/var1/var2

 

To do this just follows the following steps:

 

1. Open config.php from system/application/config directory in your project and replace $config[‘index_page’] = ‘index.php’ by $config[‘index_page’] = ”

 

2. Create a ‘.htaccess’ file in project root directory, open the file using your favourite text editor, write down the following script and save it.

[sourcecode language=”plain”]
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|images|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
[/sourcecode]

 

3. In some case the default setting for uri_protocol does not work properly. To solve this problem just replace

 

$config[‘uri_protocol’] = ‘AUTO’ by $config[‘uri_protocol’] = ‘REQUEST_URI’ from system/application/config/config.php

 

It works for me. The URL now without the index.php segment.I implemented all of the configuration above on Windows using Xampp.

Hope it will work fine for you also, if not left a comment below the post.

Move WordPress Blog to New Domain/Location or in another folder/directory

You usually need to move your blog (WordPress) if you are switching your web host as we generally work on localhost.You must also need to ‘move’ your website if you are transferring it to a new domain, new folder, or creating an alternate version on a local server.

After uploading file and updating wp-config.php.We have found that there are lots of URL in the database, It is very hard to update these rows one by one with new URL.
Don’t worry. It is very simple to go

Just 4 steps to do.

1. Query to Update WordPress Settings

UPDATE wp_options SET option_value = replace(option_value, ‘http://www.old-web-url.com’, ‘http://www.new-web-url.com’) WHERE option_name = ‘home’ OR option_name = ‘siteurl’;

2. Query to Update Permalinks

UPDATE wp_posts SET guid = replace (guid, ‘http://www.old-web-url.com’,’http://www.new-web-url.com’);

3. Query to Update Any Links Embedded In Content

UPDATE wp_posts SET post_content = replace (post_content, ‘http://www.old-web-url.com’, ‘http://www.new-web-url.com’);

4. Query to Post Metas

UPDATE wp_postmeta SET meta_value = replace (meta_value,’http://www.old-web-url.com’,’http://www.new-web-url.com’);

Sometimes, you have omitted the www from your domain when cross-linking posts or referencing images, so you should also run these above query without the ‘www’ for your old URL.

After following these steps, hope you will found your blog or website fully functional.
If it is still not functional, make a comment below this article with explanation what issue you are facing.