Create your own Instagram app with PHP

NOTICE:¬†Thanks to everyone interested in this. Sadly, I haven’t had the time that I would like to dedicate to this blog lately. I invite you to head directly to the Github Project Issue Tracker where a more fluid discussion can take place and a bigger community can participate and help with your particular issues.

 

  1. Register your application Instagram to get your OAuth credentials.
  2. Make sure to set your callback URL as a web address that is accessible anywhere from tje Internet, something like http://yourdoma.in/callback.php
  3. Download the latest version of the code in zip or tar.gz format.
  4. Unpack the code and modify the credentials stored in the $config variable of example.php to the ones obtained in step 1
  5. Point your browser to http://yourdomain/example.php. If the user is already authenticated, she must be presented with a recent popular photos screen. Otherwise, the authentication page will pop up.

This is the only part of the code that you should modify in example.php:

/**
* Configuration params, make sure to write exactly the ones
* instagram provide you at http://instagr.am/developer/
*/
$config = array(
'client_id' => '',
'client_secret' => '',
'grant_type' => 'authorization_code',
'redirect_uri' => 'http://your.call/back/url.php',
);

If you have any question, please use the Github Project Issue Tracker.

Instagram API implementation in PHP

NOTICE: Thanks to everyone interested in this. Sadly, I haven’t had the time that I would like to dedicate to this blog lately. I invite you to head directly to the Github Project Issue Tracker where a more fluid discussion can take place and a bigger community can participate and help with your particular issues.

 

Instagram recently released an official API. Initially, they had implementations in Python and Ruby, so I decided to build mine in PHP with a little help from Zend Framework.

In order to use the official API, you have to sign up first for a client key at http://instagr.am/developer/. You will need to register a new client or use the data from an existing one, just keep in mind these are the important data that you need before you can interact with the API:

Client Id: let’s call it a public key. It is necessary so the API knows your application. This is public available information, for the purpose of the example provided with my implementation we are going to query Instagram’s service directly using the client key as part of the URL.

Client secret: this is the private key that once paired with the client id is exchanged with Instagram’s service. The client secret needs to be private, but you have to make it available in some form inside your code so it can be sent and to get the authentication token back.

Callback url: maybe on of the most important aspects in this example. Make sure the callback URL is exactly the same where you are going to put the script that acts as an entry point for the Instagram callback. For this particular example, this script is going to be called “instagram.php”, meaning that you’ll have to point Instagram to something like http://example.com/instagram.php.

So this is as easy as one, two, three.

1. Grab the code from GitHub in you preferred format: zip or tar.gz and Unpack it in the same folder you told Instagram your callback was going to live.

2. The example we create in this post, authenticates and displays the most popular photos according to the data sent back by the API. The implementation provides access to all public methods, so you can create you own stuff. Amongst others, you can search for media, perform ‘follow’ actions or search media by location.

3. Just make sure to modify the code, so you can put your own client properties in the $config array at the beginning of the file:

/**
* Configuration params, make sure to write exactly the ones
* instagram provide you at http://instagr.am/developer/
*/
$config = array(
'site_url' => 'https://api.instagram.com/oauth/access_token',
'client_id' => '', // Your client id
'client_secret' => '', // Your client secret
'grant_type' => 'authorization_code',
'redirect_uri' => '', // The redirect URI you provided when signed up for the service
);

And now, the code of the instagram.php script:

require_once 'Instagram.php';

/**
* Configuration params, make sure to write exactly the ones
* instagram provide you at http://instagr.am/developer/
*/
$config = array(
'site_url' => 'https://api.instagram.com/oauth/access_token',
'client_id' => '', // Your client id
'client_secret' => '', // Your client secret
'grant_type' => 'authorization_code',
'redirect_uri' => '', // The redirect URI you provided when signed up for the service
);

// Instantiate the API handler object
$instagram = new Instagram($config);
$popular = $instagram->getPopularMedia();

// After getting the response, let's iterate the payload
echo "
<ul>\n";
$response = json_decode($popular, true);
foreach ($response['data'] as $data) {
$link = $data['link'];
$caption = $data['caption']['text'];
$author = $data['caption']['from']['username'];
$thumbnail = $data['images']['thumbnail']['url'];
?>
	<li><a href="<?= $link ?>"><img title="<?= $caption ?>" src="<?= $thumbnail ?>" border="0" alt="" width="150" height="150" align="absmiddle" /></a> by <!--?= $author ?--></li>
<!--?<br /--> }
echo "</ul>
\n";

Finally, point you browser to the authorization URL and enjoy your coding!

https://api.instagram.com/oauth/authorize/?client_id=CLIENT_ID&redirect_uri=REDIRECT_URL.

You can get more details about manipulating the API at http://instagr.am/developer/

Two (or more) Zend Framework projects on a shared host

Shared hosting environments can be a nightmare when it comes to PHP Web Development, specially in testing stages. Yes, I know one should not be using and paying for a hosting plan if the code is still not production ready, but I have seen some cases.

I will try to show how to have the Zend Framework installed with as many projects as you want (if your server quota allows you to) in the same web space and all these projects sharing the same ZF copy.

Suppose that your home directory is /home/mauricio/. From now on, I am going to refer to it as <root>. Create the following file structure under it:

library/
  Zend/
myfirstproject/
  application/
    controllers/
    views/
      scripts/
mysecondproject/
  application/
    controllers/
    views/
      scripts/
public_html/
  myfirstproject/
  mysecondproject/

Now download, extract and upload the “Zend” directory contained in the zip or gz file into the “<root>/library” directory of the structure that you have just created. This copy of the Zend Framework will be shared by all projects. If needed, you can upgrade to a future version of ZF just by changing its content.

Keep in mind that this example is based on the fact that the public folder on the hosting environment is “<root>/public_html”. Some providers call it “www”, so… in this example our public folder will be called “public_html”, make the appropriate changes to suit your needs.

The first folders: “myfirstproject” and “mysecondproject” will contain the application files that won’t be visible to the outside world. Notice that under public_html exist another two folders under the same names, these two folder will contain the bootstrap or the entry point for the application which must be of public access and all the images, style sheets and other media that your application requires.

Your .htaccess file under <root>/public_html/myfirstproject/ should look like this:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

And the bootstrap file index.php under the same location, should look like this:

<?php
/**
* <root>/public_html/myfirstproject/index.php
*
*/

// The library folder contains Zend Framework files
define('LIBRARY_PATH',     realpath(dirname(__FILE__) . '/../../library/'));
define('APP_PATH',         realpath(dirname(__FILE__) . '/../../myfirstproject/application/'));
define('CONTROLLERS_PATH', APP_PATH . '/controllers/');

// library is appended to the include_path
set_include_path(get_include_path() . PATH_SEPARATOR . LIBRARY_PATH);
include_once 'Zend/Loader.php';
Zend_Loader::registerAutoload();

/**
* The most important step is to set the controllers directory pointing to
* <root>/myfirstproject/application/controllers
*/
$front = Zend_Controller_Front::getInstance();
$front->setControllerDirectory(CONTROLLERS_PATH);
$front->dispatch();

That was the trick, let’s finish this creating our IndexController and its view. The first one should be placed on <root>/myfirsproject/application/controllers/IndexController.php:

<?php

/**
* <root>/myfirstproject/application/controllers/IndexController.php
*
*/
class IndexController extends Zend_Controller_Action
{
  public function indexAction()
  {
  }
}

The view should be anything you like, my bet is for:

<!-- <root>/myfirstproject/application/views/scripts/index.phtml -->
<html>

<head><title>It works!</title></head>

<body>
<h2>It works!</h2>
</body>
</html>

That’s all, you can start building more views, controllers and helpers. To get your second project working just follow the same steps that you did for “myfirstproject” folder. The same way, you can create as many projects as you want, keep in mind that they all share the same Zend Framework library.

Gmail outage wasn’t so bad at all!

Bad news spread rapidly and everybody heard about the Gmail service disruption on February 24th. I was personally affected by the issue, fortunately I only manage my personal e-mail through Gmail, so my business communications wasn’t affected at all.

Besides the huge attention that this problem caught, I was really impressed by the open and detailed response that Google gave to all of its customers. One good thing about the Gfail issue was the creation of the new Google apps status dashboard, where –as its name says– people can see the status of the main Google services with very detailed information, like this PDF report about the past Gmail issue.

How I wish that many companies follow this lead…