Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

first commit

  • Loading branch information...
commit 0d0d6082e0727138cfb41646f6798dd434966723 0 parents
jtvapi authored
1  .gitignore
@@ -0,0 +1 @@
+.DS_STORE
164 jtv_client.inc.php
@@ -0,0 +1,164 @@
+<?php
+include('jtv_constants.inc.php');
+
+/*
+
+Requires the OAuth PECL extension. Install it with pecl install oauth. See http://pecl.php.net/package/oauth for more info.
+
+How to use:
+
+ How to be authenticated as a user:
+ 1. Put your oauth keys in jtv_constants.inc.php
+ 2. Call start_user_authentication, which will recirect the user to Justin.tv to log in. $redirect should be a page on your site that implements step 3.
+ 3. Create a page that calls recieve_user_authentication, which will return true on success.
+ 4. Do authenticated requests with the get and post functions.
+
+
+ See the test directory for an example usage.
+
+
+*/
+
+class BaseJtvClient {
+
+ //user functions
+
+ public static function start_user_authorization($redirect){
+ trigger_error("Method not implemented");
+ }
+
+ public static function recieve_user_authorization(){
+ trigger_error("Method not implemented");
+ }
+
+ public static function get($path, $data=null, $prefix=JTV_API_PREFIX) {
+ trigger_error("Method not implemented");
+ }
+
+ public static function post($path, $data, $prefix=JTV_API_PREFIX) {
+ trigger_error("Method not implemented");
+ }
+
+ //access functions
+ public static function is_authorized(){
+ global $_SESSION;
+ return $_SESSION['JTV_AUTHORIZED'];
+ }
+
+ protected static function get_access_token(){
+ global $_SESSION;
+ return $_SESSION['JTV_ACCESS_TOKEN'];
+ }
+
+ protected static function set_access_token($token){
+ global $_SESSION;
+ $_SESSION['JTV_ACCESS_TOKEN'] = $token;
+ $_SESSION['JTV_AUTHORIZED'] = true;
+ }
+
+ protected static function get_access_token_secret(){
+ global $_SESSION;
+ return $_SESSION['JTV_ACCESS_TOKEN_SECRET'];
+ }
+
+ protected static function set_access_token_secret($token){
+ global $_SESSION;
+ return $_SESSION['JTV_ACCESS_TOKEN_SECRET'] = $token;
+ }
+
+ protected static function get_request_token(){
+ global $_SESSION;
+ return $_SESSION['JTV_REQUEST_TOKEN'];
+ }
+
+ protected static function set_request_token($token){
+ global $_SESSION;
+ $_SESSION['JTV_REQUEST_TOKEN'] = $token;
+ }
+
+ protected static function get_request_token_secret(){
+ global $_SESSION;
+ return $_SESSION['JTV_REQUEST_TOKEN_SECRET'];
+ }
+
+ protected static function set_request_token_secret($token) {
+ global $_SESSION;
+ $_SESSION['JTV_REQUEST_TOKEN_SECRET'] = $token;
+ }
+
+
+ //helper functions
+ private static function make_oauth(){
+ trigger_error("Method not implemented");
+ }
+
+ protected static function array_to_get_vars($arr) {
+ if (!$arr) {
+ return '';
+ }
+ $pieces = array();
+ foreach ($arr as $k => $v) {
+ array_push($pieces, urlencode($k).'='.urlencode($v));
+ }
+ return implode('&', $pieces);
+ }
+}
+
+class JtvClient extends BaseJtvClient {
+
+ public static function start_user_authorization($redirect){
+ $oauth = self::make_oauth();
+ $req_token = $oauth->getRequestToken(JTV_REQUEST_TOKEN_URL);
+ if (!empty($req_token)) {
+ self::set_request_token($req_token['oauth_token']);
+ self::set_request_token_secret($req_token['oauth_token_secret']);
+ header('Location: '.JTV_AUTHORIZE_URL.'?'.self::array_to_get_vars(array('oauth_token' => $req_token['oauth_token'], 'oauth_callback' => $redirect)));
+ return true;
+ }
+ return false;
+ }
+
+ public static function recieve_user_authorization(){
+ $oauth = self::make_oauth();
+ $oauth->setToken(self::get_request_token(), self::get_request_token_secret());
+ $access_token = $oauth->getAccessToken(JTV_ACCESS_TOKEN_URL, null, $_GET['oauth_token']);
+ if (!empty($access_token)) {
+ self::set_access_token($access_token['oauth_token']);
+ self::set_access_token_secret($access_token['oauth_token_secret']);
+ return true;
+ }
+ return $access_token;
+ }
+
+ public static function get($path, $data=null, $prefix=JTV_API_PREFIX) {
+ $url = JTV_HOST.$prefix.$path.".json?".self::array_to_get_vars($data);
+ if (JTV_DEBUG == true) print_r($url);
+ $oauth = self::make_oauth();
+ $result = $oauth->fetch($url, null, OAUTH_HTTP_METHOD_GET);
+ if ($result) {
+ return json_decode($oauth->getLastResponse(), true);
+ }
+ return $result;
+ }
+
+ public static function post($path, $data, $prefix=JTV_API_PREFIX) {
+ $url = JTV_HOST.$prefix.$path.".json";
+ $oauth = self::make_oauth();
+ $result = $oauth->fetch($url, $data, OAUTH_HTTP_METHOD_POST);
+ if ($result) {
+ return json_decode($oauth->getLastResponse(), true);
+ }
+ return $result;
+ }
+
+ private static function make_oauth(){
+ $oauth = new OAuth(JTV_CONSUMER_KEY, JTV_CONSUMER_SECRET, OAUTH_SIG_METHOD_HMACSHA1);
+ if (JTV_DEBUG == true) $oauth->enableDebug();
+ if (self::is_authorized()) {
+ $oauth->setToken(self::get_access_token(), self::get_access_token_secret());
+ }
+ return $oauth;
+ }
+}
+
+?>
15 jtv_constants.inc.php
@@ -0,0 +1,15 @@
+<?php
+//keys
+define("JTV_CONSUMER_KEY", "");
+define("JTV_CONSUMER_SECRET", "");
+
+//urls
+define("JTV_HOST", "http://api.justin.tv:80");
+define("JTV_API_PREFIX", "/api");
+define("JTV_REQUEST_TOKEN_URL", "http://api.justin.tv/oauth/request_token");
+define("JTV_AUTHORIZE_URL", "http://api.justin.tv/oauth/authorize");
+define("JTV_ACCESS_TOKEN_URL", "http://api.justin.tv/oauth/access_token");
+
+//other
+define("JTV_DEBUG" , false);
+?>
16 readme.markdown
@@ -0,0 +1,16 @@
+Justin.tv PHP Client Library
+============================
+
+How to use:
+-----------
+
+How to be authenticated as a user:
+
+ 1. Put your oauth keys in jtv_constants.inc.php
+ 2. Call start_user_authentication, which will recirect the user to Justin.tv to log in. $redirect should be a page on your site that implements step 3.
+ 3. Create a page that calls recieve_user_authentication, which will return true on success.
+ 4. Do authenticated requests with the get and post functions.
+
+
+ See the test directory for an example usage.
+
BIN  test/.DS_Store
Binary file not shown
24 test/index.php
@@ -0,0 +1,24 @@
+<?php session_start(); ?>
+<html>
+ <?php
+ include('jtv_client.inc.php');
+ ?>
+ <head>
+ <title>jtv test</title>
+ </head>
+ <body>
+ <?php
+ if (JtvClient::is_authorized()) {
+ $user_info = JtvClient::get('/account/whoami');
+ ?>
+ Hello, <?php print($user_info['login']); ?>!<br/>
+ <img src="<?php print($user_info['image_url_medium']); ?>"/>
+ <?php
+ } else {
+ ?>
+ <a href="/login.php">Log in</a>
+ <?php
+ }
+ ?>
+ </body>
+</html>
5 test/login.php
@@ -0,0 +1,5 @@
+<?php
+ session_start();
+ include('jtv_client.inc.php');
+ JtvClient::start_user_authorization('http://'.$_SERVER['SERVER_NAME'].':'.$_SERVER['SERVER_PORT'].'/login_leg3.php');
+?>
10 test/login_leg3.php
@@ -0,0 +1,10 @@
+<?php
+session_start();
+include('jtv_client.inc.php');
+if(JtvClient::recieve_user_authorization() == true) {
+ header('Location: /');
+} else {
+ print('Login error.');
+}
+
+?>
Please sign in to comment.
Something went wrong with that request. Please try again.