PHP


16
Nov 09

PHP Class To Interface With Many URL Shortening Services

With the explosion of Twitter and such services, the need to have long URLs shortened has drastically increased.  Naturally, many services to shorten URLs have sprung up as well.

Most of these services (that are worth using), have some sort of API that will allow developers to send a long URL to them, have it shortened and then returned.  This type of service allows other developers to write applications that have the ability to shorten URLs right inside of them.

Recently, I had to write a PHP class to work with URL shorteners.  So, I made it support multiple shortening services and have decided to share it with you.

The class is called URLShortener.class.php and it supports the following shortening services:

  • j.mp (bit.ly)
  • tr.im
  • TinyURL
  • is.gd
  • u.nu
  • Linkyy
  • Your own custom shortening service

Here are some examples of how to use the class in your own applications:

<?php
	 /* Basic: */
	 $s = new URLShortener('tr.im');
	 echo $s->shorten("http://brandontreb.com");
 
	 /* Using API Key and Login */
	 $s = new URLShortener('j.mp');
	 $s->login = 'brandontreb';
	 $s->APIKey = 'R_2a413ebd15254a72b500ec2ce83f982d';
	 echo $s->shorten("http://brandontreb.com");
 
	 /* Custom URL */
	 // Just make sure you put %s and it will be 
	 // replaced with the long URL
	 $s = new URLShortener('custom',
		'http://b1t.me/api/shorten.xml/%s');
	 echo $s->shorten("http://brandontreb.com",false);
?>

You may download my URLShortener PHP class here.

Feel free to leave any questions or comments in the comments section.


12
Nov 09

Money You Might Be Missing Out On – LinkShare API Integration

logo

As you may know, I am the developer of the site FreshApps.com.  One thing we had been doing to make some extra money is to use Linkshare to be an affiliate for Apple.

If you don’t know, Linkshare is a service that allows you to become an affiliate of thousands of companies.  You simply select one of the companies products, get the linkshare link, and put the link on your site.  Now, every time someone clicks that link and makes a purchase, you will get a percentage of the revenue.

The Problem

The only problem was, we have thousands of apps and converting the links manually seemed like such a daunting process. So, as you can imagine we converted around 10 links and never looked at it again.

Well, earlier today, the designer of the site JJ Mancini, asked me to check and see if linkshare had an API.  I checked it out and sure enough, they did and it was no more complex than interfacing with a URL shortening service.  So, I wrote the script and within minutes, all of our downloads links were converted into something that can now make us some revenue.

The Solution

Now that I have created the script, I figured I would pretty it up and share it with you.  Keep in mind, the script is stupid easy, so if I am insulting your intelligence by showing it, I apologize.

<?php
	/* linkshare.php */
 
	// Your linkshare API token
	$token = "89705XXXXd11ab28ae548bXXXX4ad6475279faXXXX65da0ec8ed77XXXXeb067";
	// Apples Merchant ID
	$mid   = "13508";
 
	$linkShareLinks = array();	
 
	// I assume links in an array of links to the app store
	foreach($links as $link) {
		$linkShareURL = "http://feed.linksynergy.com/createcustomlink.shtml?".
			"token=$token&mid=$mid&murl=$link&mt=8&buylink=yes";
		$linkShareReturn = file_get_contents($linkShareURL);
 
		if(stristr($linkShareReturn, "click.linksynergy.com")) {
			array_push($linkShareLinks,$linkShareReturn);
		} 
	}
 
	print_r($linkShareLinks);
?>

And that’s it! The variable $linkShareLinks will now contain all of the App Store links converted to your account’s linkshare url. If you have any site with that contains ads for apps in the app store (review site, developer blog, etc…), you would be crazy not to integrate with linkshare.

Give it a try, and feel free to ask questions in the comments section.


10
Aug 09

Displaying Your FeedBurner Subscriber Count Anywhere – PHP Coding Tutorial

feedburner-logo1

If you are a serious blogger (and I’m sure you are), you probably track your RSS subscribers with FeedBurner.  If you don’t, you should be.  One thing that has always bugged me about FeedBurner is if you ever wanted to display the number of subscribers on your blog, you were stuck using their image.  The image looks like this:

I’m sure you have seen this logo everywhere.  Well, not too many people know it, but FeedBurner actually has a very simple API that allows you to just get the subscriber count so you can display it however you would like.

So rather than being limited to this boring icon, you can display your live subscriber count anywhere on your blog.  If you are feeling adventurous, you could even super impose it on to your own custom image using the GD library (tutorial to come if sufficient interest, post in the comments if you would like to see it).

Let’s get started…

1. Activate the Awareness API Inside Of Feedburner

Log in to FeedBurner.  Click the Publicize tab and the click Awareness API.  Finally, click Activate.  The service is now enabled.

awareness_api

2. Write The PHP Code To Interface With FeedBurner

There is a lot of data that you could potentially get from FeedBurner, but the code below will just show you how to get your subscriber count.

All you need to do is make a simple GET request using CURL to the URL https://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=brandontreb . Of course replacing brandontreb with your feed’s name.

    <?php
     
    $url	= "https://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=brandontreb";
    $ch 	= curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    $data = curl_exec($ch); 
    curl_close($ch);		
    if ($data) {
    	echo $data;
    	preg_match('/circulation=\"([0-9]+)\"/',$data, $matches);
    	if ($matches[1] != 0) {
    		$subscriberCount = $matches[1];
    	}
    }
     
    echo "Join the other $subscriberCount people and subscriber to my RSS feed."
     
    ?>

    This code is pretty straight forward with a little trickiness to parse the XML. We first make a CURL connection to the URL. Just replace brandontreb with the title of your RSS feed inside of FeedBurner.

    Next we print the data. Note: You won’t see the data in your browser unless you do a view source. Since it is XML, your browser will treat it like HTML and not display it. So, we do a preg_match for the element circulation and get it’s value. (Pretty sick right?). The value of our subscriber count will be at index 1 of the matches array.

    Then, we just print the subscriber count and voila!

    Now, you are no longer a slave to that generic FeedBurner subscriber count icon. Be sure to check out their API for other cool things that you can do with their web services. If you have any questions, feel free to leave them in the comments section of this post. Happy Coding!