Magento 2, Magento Development, Customization, Extension Development and Integration, Optimization, SEO and Responsive Design

Magento 2, Magento Development, Customization, Extension Development and Integration, Optimization, SEO and Responsive Design

Tumblr Api Example Using Oauth PHP Script

After a long research to get Tumblr user information using Oauth, Only this solution is working for me. So i am sharing here it will help someone. You can use your Tumblr account to login with authentication to get Tumblr user information. Using this example you can get user information like blog title, blog name, number of posts, blog url, Likes etc. Let's see step by step.
Step1: Get an OAuth key: register an application

Step2: Create index.php and add following code.

<?php
// This script is a simple example of how to send a user off to authentication using Tumblr's OAuth
// Start a session.  This is necessary to hold on to  a few keys the callback script will also need
session_start();

// Include the TumblrOAuth library
require_once('tumblroauth/tumblroauth.php');

// Define the needed keys
$consumer_key = "YOUR CONSUMER KEY";
$consumer_secret = "YOUR CONSUMER SECRET KEY";

// The callback URL is the script that gets called after the user authenticates with tumblr
// In this example, it would be the included callback.php you need to change with your url
$callback_url = "http://www.domain.com/tumblr/callback.php";

$tum_oauth = new TumblrOAuth($consumer_key, $consumer_secret);

$request_token = $tum_oauth->getRequestToken($callback_url);

// Store the request token and Request Token Secret as out callback.php script will need this
$_SESSION['request_token'] = $token = $request_token['oauth_token'];
$_SESSION['request_token_secret'] = $request_token['oauth_token_secret'];

// Check the HTTP Code.  It should be a 200 (OK), if it's anything else then something didn't work.
switch ($tum_oauth->http_code) {
  case 200:
    // Ask Tumblr to give us a special address to their login page
    $url = $tum_oauth->getAuthorizeURL($token);
 
 // Redirect the user to the login URL given to us by Tumblr
    header('Location: ' . $url);
 
    break;
default:
    // Give an error message
    echo 'Could not connect to Tumblr. Refresh the page or try again later.';
}
exit();
?>

Note: You need to change callback url in index.php file.
$callback_url = "http://www.domain.com/tumblr/callback.php";

Step3: Now create a callback  file

<?php
// Start a session, load the library
session_start();
require_once('tumblroauth/tumblroauth.php');

// Define the needed keys
$consumer_key = "YOUR CONSUMER KEY";
$consumer_secret = "YOUR CONSUMER SECRET KEY";

// Create instance of TumblrOAuth.
// It'll need our Consumer Key and Secret as well as our Request Token and Secret
$tum_oauth = new TumblrOAuth($consumer_key, $consumer_secret, $_SESSION['request_token'], $_SESSION['request_token_secret']);

// Ok, let's get an Access Token. We'll need to pass along our oauth_verifier which was given to us in the URL. 
$access_token = $tum_oauth->getAccessToken($_REQUEST['oauth_verifier']);

// We're done with the Request Token and Secret so let's remove those.
unset($_SESSION['request_token']);
unset($_SESSION['request_token_secret']);

// Make sure nothing went wrong.
if (200 == $tum_oauth->http_code) {
  // good to go
} else {
  die('Unable to authenticate');
}

//echo "oauth_token: ".$access_token['oauth_token']."<br>oauth_token_secret: ".$access_token['oauth_token_secret']."<br><br>"; //print the access token and secret for later use

$tum_oauth = new TumblrOAuth($consumer_key, $consumer_secret, $access_token['oauth_token'], $access_token['oauth_token_secret']);

// Make an API call with the TumblrOAuth instance.  There's also a post and delete method too.
$userinfo = $tum_oauth->get('http://api.tumblr.com/v2/user/info');

// You don't actuall have to pass a full URL,  TukmblrOAuth will complete the URL for you.
// This will also work: $userinfo = $tum_oauth->get('user/info');

// Check for an error.
if (200 == $tum_oauth->http_code) {
  // good to go
} else {
  die('Unable to get info');
}

$userInfos = $userinfo->response->user->blogs;
foreach($userInfos as $userdata)
{
    echo $userdata->title."<br>";
    echo $userdata->name."<br>";
    echo $userdata->url."<br>";
}
?>

Thanks for visiting my blog. Feel free to share your feedback.

Please support us, Like us on Facebook.

0 comments:

Post a Comment

 

Copyright @ 2017 HKBlog.