Accessing Miso API: a sample PHP application
Miso recently introduced the ability to access their data through API which is really great, I’m already using it in production on Italiansubs.net.
The documentation (that you should definitely read) is well written and easy to follow if you have any experience in working with json API and OAuth, but if you haven’t, and you don’t know Ruby (the programming language used for their sample application) it’s possible to find it tricky.
So, here’s the sample PHP application:
First, install the PECL OAuth library
pecl install oauth
And add the extension to your php.ini file
extension=oauth.so
The first file is the configuration file (misoconfig.php)
define('OAUTH_CONSUMER_KEY',"yourconsumerkey");
define('OAUTH_CONSUMER_SECRET',"yourconsumersecret");
define('OAUTH_REQUEST_TOKEN_API', 'https://gomiso.com/oauth/request_token');
define('OAUTH_ACCESS_TOKEN_API', 'https://gomiso.com/oauth/access_token');
define('OAUTH_AUTHORIZE_API', 'https://gomiso.com/oauth/authorize');
define('OAUTH_TMP_DIR', function_exists('sys_get_temp_dir') ? sys_get_temp_dir() : realpath($_ENV["TMP"]));
define('MISO_USER_AGENT', 'youruseragent');
The next file requests a token, saves it in a tmp file and redirects the user to Miso for the authentication, if the user allows your application, he will be redirected to the callback url
require 'misoconfig.php';
$req_url = OAUTH_REQUEST_TOKEN_API;
try {
$o = new OAuth(OAUTH_CONSUMER_KEY,OAUTH_CONSUMER_SECRET,
OAUTH_SIG_METHOD_HMACSHA1,OAUTH_AUTH_TYPE_FORM);
$o->enableDebug();
$resp = $o->getRequestToken($req_url, "http://www.yourwebsite.com/callback.php");
} catch(OAuthException $E) {
echo "Response: ". $E->lastResponse . "\n";
}
file_put_contents("/tmp/last_request",$resp['oauth_token']."\n".
$resp['oauth_token_secret']."\n");
print_r ($resp);
header("Location: ".OAUTH_AUTHORIZE_API."?oauth_token=".$resp['oauth_token']);
The last file (callback.php) lets us acquire the access token by using the previous request token and the oauth verifier:
require 'misoconfig.php';
$acc_url = OAUTH_ACCESS_TOKEN_API;
list($token,$secret) = file('/tmp/last_request');
$token = trim($token);
$secret = trim($secret);
$verifier = $_GET['oauth_verifier'];
try {
$o = new OAuth(OAUTH_CONSUMER_KEY,OAUTH_CONSUMER_SECRET,
OAUTH_SIG_METHOD_HMACSHA1,OAUTH_AUTH_TYPE_URI);
$o->enableDebug();
$o->setToken($token,$secret);
$resp = $o->getAccessToken($acc_url, NULL, $verifier);
file_put_contents("/tmp/access_token.txt",serialize($resp));
} catch(OAuthException $E) {
echo "Response: ". $E->lastResponse . "\n";
}
print_r($resp);
We’re done! We can now access Miso’s API using the access token, you can test it with something like this:
try {
$oauth = new OAuth(OAUTH_CONSUMER_KEY,OAUTH_CONSUMER_SECRET,OAUTH_SIG_METHOD_HMACSHA1,OAUTH_AUTH_TYPE_AUTHORIZATION);
$oauth->setToken($oauth_token,$oauth_token_secret);
$oauth->fetch('http://gomiso.com/api/oauth/v1/users/show.json', null, OAUTH_HTTP_METHOD_GET, array('User-Agent'=>''.MISO_USER_AGENT.''));
$response_info = $oauth->getLastResponseInfo();
header("Content-Type: {$response_info["content_type"]}");
echo $oauth->getLastResponse();
} catch(OAuthException $E) {
echo "Exception caught!\n";
echo "Response: ". $E->lastResponse . "\n";
}
Please note that this is just an example that should let you understand how to access Miso API, it won’t work out of the box just by uploading this code snippets into your FTP.
1 Comment
Leave a comment
Menu
Cloud Tag
Categories
- Announcements (1)
- Benchmarks (2)
- How-To (5)
- News (1)
- Projects (1)
- Tips (5)
Archive
- June 2011 (1)
- May 2011 (1)
- January 2011 (1)
- October 2010 (1)
- August 2010 (4)
- July 2010 (1)




Great tutorial, the only issues I’m having using HTTP_OAUTH (my host doesn’t support regular Oauth), is that I’m clueless how to make request after I get my access token.. Any idea? I would be eternally grateful!
———————-
<?php
// START A SESSION SO WE CAN ACCESS VARIABLES SHARED WITH US BY INDEX.PHP
session_start();
// INCLUDE ANY SETTINGS AS WELL
require_once("misoconfig.php");
// AS ANY CLASS REFERENCES
require_once("HTTP/OAuth/Consumer.php");
// BY REACHING THIS FAR, WE'VE BEEN AUTHENTICATED. LET'S GRAB THE ACCESS TOKEN AND SECRET AND SAVE THEM .
$consumer = new HTTP_OAuth_Consumer(‘key’, ‘secret’, $_SESSION['token'],$_SESSION['token_secret']);
$consumer->getAccessToken(OAUTH_ACCESS_TOKEN_API, $_GET['oauth_verifier']);
$_SESSION['token'] = $consumer->getToken();
$_SESSION['token_secret'] = $consumer->getTokenSecret();
?>