Closed KJThaDon closed 10 years ago
@KJThaDon If you just want to show a count of followers, you should look at the /user/info call for the API. It has follower counts for all of your blogs.
It also looks like you're trying to access a fully oauthed route, without having your access tokens. You'll need to do the 3-legged OAuth dance, get your access tokens and then you'll be able to access all of your private information.
@KJThaDon here's an example of the working follower counts (hope this helps):
<?php
$client = new Tumblr\API\Client('consumer key', 'consumer secret');
$client->setToken('token', 'secret');
$blogs = $client->getUserInfo()->user->blogs;
foreach ($blogs as $blog) {
echo "{$blog->name} has {$blog->followers} follower(s)\n";
}
You can get your token and secrets through implementing OAuth (as @codingjester mentions), or grab them off of https://api.tumblr.com/console/
Thanks! re-open with any more questions!
Thanks guys. I had actually found a different file which I was able to get the followers with, just by including the php file.. I have never used "autoloader" or whatever that is, so I was probably doing something wrong. However the way I am doing it below will not show the avatar profile url..Any way I can get it?
this is the code and tumblr.php file
----- index.php
<?php include ('tumblr.php'); $tumblr_id = 'tumblr'; //blog name/id $tumblr_consumer_key = 'xxx'; $tumblr_consumer_secret = 'xxx'; $tumblr_access_token = 'xxx'; $tumblr_access_token_secret = 'xxx'; $tumblr = new Tumblr( $tumblr_consumer_key , $tumblr_consumer_secret , $tumblr_access_token , $tumblr_access_token_secret ); $tumblr_followers = $tumblr->followers(''.$tumblr_id.'.tumblr.com'); $tumblr_count = $tumblr_followers->response->total_users; echo $tumblr_count;
//var_dump($tumblr_followers); ?>
As you can see the above code will work, when including the php below. If you uncomment the var dump you will not see an avatar url in there. Am I doing something wrong, or does this api not support the profile avatar?
Thanks
------- tumblr.php
<?php if( !class_exists( 'TumblrClientException' ) ) { /**
if( !class_exists( 'TumblrClient') ) { /**
The main API class. */ class Tumblr { protected $apiKey; protected $oauth;
protected $host = 'http://api.tumblr.com/v2';
protected $methods = array( 'blogInfo' => array( 'uri' => '/blog/%s/info', 'auth' => 'api_key', 'method' => 'get' ), 'followers' => array( 'uri' => '/blog/%s/followers', 'auth' => 'oauth', 'method' => 'get' ), 'posts' => array( 'uri' => '/blog/%s/posts', 'auth' => 'api_key', 'method' => 'get' ), 'queue' => array( 'uri' => '/blog/%s/posts/queue', 'auth' => 'oauth', 'method' => 'get' ), 'drafts' => array( 'uri' => '/blog/%s/posts/draft', 'auth' => 'oauth', 'method' => 'get' ), 'submissions' => array( 'uri' => '/blog/%s/posts/submission', 'auth' => 'oauth', 'method' => 'get' ), 'createPost' => array( 'uri' => '/blog/%s/post', 'auth' => 'oauth', 'method' => 'post' ), 'editPost' => array( 'uri' => '/blog/%s/post/edit', 'auth' => 'oauth', 'method' => 'post' ), 'reblogPost' => array( 'uri' => '/blog/%s/post/reblog', 'auth' => 'oauth', 'method' => 'post' ), 'deletePost' => array( 'uri' => '/blog/%s/post/delete', 'auth' => 'oauth', 'method' => 'post' ), 'dashboard' => array( 'uri' => '/user/dashboard', 'auth' => 'oauth', 'method' => 'get' ), 'likes' => array( 'uri' => '/user/likes', 'auth' => 'oauth', 'method' => 'get' ), 'following' => array( 'uri' => '/user/following', 'auth' => 'oauth', 'method' => 'get' ), 'follow' => array( 'uri' => '/user/follow', 'auth' => 'oauth', 'method' => 'post' ), 'unfollow' => array( 'uri' => '/user/unfollow', 'auth' => 'oauth', 'method' => 'get' ), 'userInfo' => array( 'uri' => '/user/info', 'auth' => 'oauth', 'method' => 'get' ) );
/**
public function __construct( $consumer_key , $consumer_secret , $oauth_token = NULL , $oauth_token_secret = NULL ) { $this->sha1_method = new OAuthSignatureMethod_HMAC_SHA1();
$this->consumer = new OAuthConsumer( $consumer_key , $consumer_secret ); if ( !empty( $oauth_token ) && !empty( $oauth_token_secret ) ) { $this->token = new OAuthConsumer( $oauth_token , $oauth_token_secret ); } else { $this->token = NULL; }
}
/**
@param array $args */ public function __call($method, $args) { // If method does not exist in the API, throw an exception. if (!array_key_exists($method, $this->methods)) { throw new TumblrException("Unknown method [".$method."]"); }
// Get method info. $info = $this->methods[$method];
// If this method needs an argument, check for validness. Only one // argument is accepted, and it should be a string. if (strstr($info['uri'], '%s')) { // Check if there are arguments at all, and check its validness. if (count($args) !== 1 || !is_string($args[0])) { throw new TumblrException("This method only accepts one argument, and it should be a string."); } } else { // Check for arguments. if (count($args) > 0) { throw new TumblrException("This method does not accept arguments."); } }
// Handle it according to the method. if ('post' === $info['method']) { return $this->handlePostMethod($info, $args); } else { return $this->handleGetMethod($info, $args); } }
/**
/**
/**
/**
/**
/**
@param array $args */ protected function handlePostMethod($info, $args) { // Handle authentication.
// Post data to the API, wait for response.
// Return response. }
/**
// Get data.
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($curl);
curl_close($curl);
} else { $url = $this->prepareUrl($info['uri'], !empty($args) ? $args[0] : false);
// Get data.
try
{
$response = @$this->oAuthRequest( $url , 'GET' );
if ( $response ) {
return @json_decode( $response );
}
return $response;
}
catch (OAuthException $ex)
{
throw new TumblrClientException(
'The Tumblr API responded with an authentication error: ' . $ex->getMessage()
);
}
}
$responseData = json_decode($response);
// Check for errors. if (200 !== $responseData->meta->status) { throw new TumblrClientExceptionException( $responseData->meta->msg, $responseData->meta->status ); }
return json_decode($response); }
/**
/**
@param string $blog */ protected function prepareUrl($uri, $blog) { $return = $this->getHost();
// Add blogname. $return .= str_replace('%s', $blog, $uri);
return $return; }
/**
@param string $blog */ protected function prepareApiKeyUrl($uri, $blog) { $return = $this->prepareUrl($uri, $blog);
// Add api key. $apiKey = $this->getApiKey(); if (strlen($apiKey) == 0) { return new TumblrException('No API key given'); }
$return .= '?api_key='.$apiKey;
return $return; }
/**
Format and sign an OAuth / API request */ function oAuthRequest( $url , $method ) {
$request = OAuthRequest::from_consumer_and_token( $this->consumer , $this->token , $method , $url , array() ); $request->sign_request( $this->sha1_method , $this->consumer , $this->token ); switch ( $method ) { case 'GET': return $this->http( $request->to_url() , 'GET' ); default: return $this->http( $request->get_normalized_http_url() , $method , $request->to_postdata() ); }
}
/**
@return API results _/ function http( $url , $method , $postfields = NULL ) { $this->http_info = array( ); $ci = curlinit(); / Curl settings */ curl_setopt( $ci , CURLOPT_USERAGENT , $this->useragent ); curl_setopt( $ci , CURLOPT_CONNECTTIMEOUT , $this->connecttimeout ); curl_setopt( $ci , CURLOPT_TIMEOUT , $this->timeout ); curl_setopt( $ci , CURLOPT_RETURNTRANSFER , TRUE ); curl_setopt( $ci , CURLOPT_HTTPHEADER , array( 'Expect:' ) ); curl_setopt( $ci , CURLOPT_SSL_VERIFYPEER , $this->ssl_verifypeer ); curl_setopt( $ci , CURLOPT_HEADERFUNCTION , array( $this , 'getHeader' ) ); curl_setopt( $ci , CURLOPT_HEADER , FALSE );
switch ( $method ) { case 'POST': curl_setopt( $ci , CURLOPT_POST , TRUE ); if ( !empty( $postfields ) ) { curl_setopt( $ci , CURLOPT_POSTFIELDS , $postfields ); } break; case 'DELETE': curl_setopt( $ci , CURLOPT_CUSTOMREQUEST , 'DELETE' ); if ( !empty( $postfields ) ) { $url = "{$url}?{$postfields}"; } }
curl_setopt( $ci , CURLOPT_URL , $url ); $response = curl_exec( $ci ); $this->http_code = curl_getinfo( $ci , CURLINFO_HTTP_CODE ); $this->http_info = array_merge( $this->http_info , curl_getinfo( $ci ) ); $this->url = $url; curl_close( $ci ); return $response;
}
/**
Get the header info to store. */ function getHeader( $ch , $header ) { $i = strpos( $header , ':' ); if ( !empty( $i ) ) { $key = strreplace( '-' , '' , strtolower( substr( $header , 0 , $i ) ) ); $value = trim( substr( $header , $i + 2 ) ); $this->http_header[$key] = $value; } return strlen( $header );
}
} }
Avatar urls can be gathered from the avatar route on the API. The Client gives you a function called getBlogAvatar to help you with this.
$client->getBlogAvatar('codingjester.tumblr.com', 512); # Gets the avatar for codingjester size 512.
Can you please help me out.. I am not wanting to load all these files onto my shared hosting account. All I want to do is display a simple follower count on my web page. Just a plain text number.. There must be an easier way then loading all these
I have tried a few different things in php, but of course none of them worked..
<?php $tumblr_link = 'http://api.tumblr.com/v2/blog/ngx2gaming.tumblr.com/followers'; $tumblr_API = array( 'consumer_key' => "KEYHERE", 'consumer_secret' => "KEYHERE" ); $tumblr_followers = $tumblr_API-> get( $tumblr_link, array('limit'=>50)); echo $tumblr_followers; ?>