Using Bit.ly PHP API

Posted on August 15th, 2010 at 8:27 am

URL shortening services became really popular after Twitter went mainstream. The leader in URL shortening services is definitely Bit.ly. In this tutorial i will briefly describe how to use it’s API to autmoatically sorten an URL address.

Signup for Bit.ly API key

Before we can do anything we need to signup for bit.ly api key, you can do it here http://bit.ly/a/sign_up, once you’re done you can find your key at http://bit.ly/a/your_api_key

Hint if you don’t feel like signing up you can use Bit.ly demo api key and password, but keep in mind that it shouldn’t be used in production. Login:bitlyapidemo, ApiKey=R_0da49e0a9118ff35f52f629d2d71bf07

Shortening URL with PHP and Bit.ly

Bit.ly has simple RESTful API so it’s easy to use it, in fact the whole script can be written in just one function.

function bitly_shorten($url)
{
    $query = array(
        "version" => "2.0.1",
        "longUrl" => $url,
        "login" => API_LOGIN, // replace with your login
        "apiKey" => API_KEY // replace with your api key
    );
 
    $query = http_build_query($query);
 
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "http://api.bit.ly/shorten?".$query);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 
    $response = curl_exec($ch);
    curl_close($ch);
 
    $response = json_decode($response);
 
    if($response->errorCode == 0 && $response->statusCode == "OK") {
        return $response->results->{$url}->shortUrl;
    } else {
        return null;
    }
}

That’s it just copy and paste this code into your project.

Twitter, PHP oAuth: update status

Posted on June 7th, 2010 at 9:15 pm

Recently Twitter announced they are going disable basic authentication mechanism. Basic auth can be used until June 30th 2010, so there is only less then one month left (assuming you are reading this on June 2010). I admit that for a long time i neglected the OAuth Twitter authorization, it seemed complicated and time consuming in comparison basic auth.

However, when i found out they are shutting down the basic api, i had no other choice then to learn how it works. What it turned out is that with the right tools it’s pretty easy to do. Especially if you are (like me) looking to setup application that will be using only single user. For example to automatically update your status. Read the rest of this entry »

Custom WordPress comments list template

Posted on April 26th, 2010 at 9:44 pm

It’s counter intuitive but, there is no such thing as comments list template, ie the template file where you can define each comment look and feel. Of course (like with almost anything in WordPress) it’s possible to change comments list HTML, but it requires a bit different approach.

First in your theme functions.php file you need to define new function (you can call it anyway you want):

<?php
function mytheme_comment($comment, $args, $depth) {
   $GLOBALS['comment'] = $comment; ?>
   <li <?php comment_class(); ?> id="li-comment-<?php comment_ID() ?>">
     <div id="comment-<?php comment_ID(); ?>">
      <div class="comment-author vcard">
         <?php echo get_avatar($comment,$size='48',$default='<path_to_url>' ); ?>
 
         <?php printf(__('<cite class="fn">%s</cite> <span class="says">says:</span>'), get_comment_author_link()) ?>
      </div>
      <?php if ($comment->comment_approved == '0') : ?>
         <em><?php _e('Your comment is awaiting moderation.') ?></em>
         <br />
      <?php endif; ?>
 
      <div class="comment-meta commentmetadata">
          <a href="<?php echo htmlspecialchars( get_comment_link( $comment->comment_ID ) ) ?>">
              <?php printf(__('%1$s at %2$s'), get_comment_date(),  get_comment_time()) ?>
          </a>
          <?php edit_comment_link(__('(Edit)'),'  ','') ?>
      </div>
 
      <?php comment_text() ?>
 
      <div class="reply">
         <?php comment_reply_link(array_merge( $args, array('depth' => $depth, 'max_depth' => $args['max_depth']))) ?>
      </div>
     </div>
<?php
        }
?>

This code was taken from WordPress Codex, at the same time it’s a default design of the single comment, you can make in this function any changes you want.

In order to implement new “comment theme” you need to configure it. Open comments.php file and find code similar to this:

<ol class="commentlist">
<?php wp_list_comments(); ?>
</ol>

wp_list_comments() function is responsible for displaying comments list. To use our custom function we defined earlier to render comments we need to pass apprioprate argument to wp_list_comments()

<?php
wp_list_comments("callback=mytheme_comment");
?>

From now on each comment will be rendered using mytheme_comment() function. wp_list_comments() accepts more actually more arguments, however i don’t want to duplicate code from Codex here, if you are interested go to WordPress Codex page for more information.

PHP plugin autoload

Posted on May 17th, 2009 at 4:02 pm

Somewhere in the past i already covered autoloading on this blog but lately while developing my WordPress Job Board Plugin i decided to add an autoloader (there are over 30 libraries right now so it makes at least some sense), besides if we are given some cool tools why not to use them, right? Read the rest of this entry »

XInclude and XPointer – Advanced XML Part 2

Posted on December 4th, 2008 at 9:55 pm

This is the second part Advanced XML and PHP series. If you haven’t read first part you can find it here: Introduction to XPath in PHP. You may be not interested right now but unfortunately topic of todays article (XPointer and XInclude) are extensions of XPath, so it would be very helpful if you would go thru it right now, i assure you that XPath is very interesting technology and knowing it may benefit somewhere in the future. Read the rest of this entry »

PHP AutoLoad Best Practices

Posted on November 13th, 2008 at 6:36 pm

Few days ago my friend Mark, added on my blog interesting comment regarding autoloading objects in PHP. While i think it is great to have quality comments like each of Mark’s comment, autoloading is a topic that requires a separate post.

What is autoloading? Every time you want to use a new class in your PHP project, first you need to include this class (using include or require language construct, that’s right this are not functions). However if you have __autoload function defined, inclusion will handle itself. Read the rest of this entry »

PHP string to hex and hex to string functions

Posted on November 4th, 2008 at 9:45 pm

In my opinion SQL injection is an major issue when it comes to web application development. Typically programmers deal with it by escaping strings, while this is probably the best way to get it handled i want to show you different yet effective approach to this problem – converting string to hex value. Read the rest of this entry »

PHP redirect header location

Posted on October 4th, 2008 at 8:32 am

Ok finally i managed to write a new article after about a month ( actually i do not even want to think when i wrote last good article for you). Anyway today short article on using headers to redirect vistors to other pages on your site or external site.

To do this you need only one function
header($header, $replace, $http_response_code);

First argument $header is a header itself. We want to use header to redirect visitors so in our case we will use something like “Location: URL” where URL is obviously an url of a page to which you want to redirect visitor. It can be full url (http://some-random-url.com/pages/page.html) or relative URL (pages/page.html) Read the rest of this entry »

Detecting search engine bots with PHP

Posted on September 7th, 2008 at 5:12 pm

I have to admit i am writing this post because i will publish soon big article about cURL extension and show you how to use it to create bots which would be even able to login to web pages and do some actions there, actually nothing illegal, but still i fill the need to tell you how to detect “good” (search engine bots) and “bad” (scrappers) bots and how to protect from them. Read the rest of this entry »

Clone keyword in PHP

Posted on July 30th, 2008 at 5:26 pm

A lot of people do not read PHP manual. That is a fact, they will look for answers to their questions on forums, books, blogs (like this one for example) and so on, while most of the answers they are looking for, are already there … in PHP manual. Or maybe it is me who is weired, because i like to browse PHP.net to see “what’s new”, well can’t really tell. Read the rest of this entry »