tumblr / tumblr.php

Tumblr API v2 PHP Client
Apache License 2.0
407 stars 115 forks source link

Simple follower count #29

Closed KJThaDon closed 10 years ago

KJThaDon commented 10 years ago

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; ?>

codingjester commented 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.

seejohnrun commented 10 years ago

@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!

KJThaDon commented 10 years ago

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') ) { /**

codingjester commented 10 years ago

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.