tumblr API在php中获取帖子

时间:2016-05-01 02:31:43

标签: tumblr

我很难使用tumblr-api来呼叫博客上的帖子。我正在使用api console来获取为此提供变量的代码,虽然我不明白如何获取这些变量并将它们输出到html。

    $client = new Tumblr\API\Client('PRIVATE KEY');

// Make the request
$client->getBlogPosts('tecksup.tumblr.com', array('type' => 'text', 'limit' => 6, 'filter' => 'text'));

据我所知,这得到了变量。 有人知道如何调用变量(博客,在这一个上只有两个)用于echo语句,或者至少在此之后将代码格式化为文本? This page有文档,但我不明白如何使用他们共享的内容。

2 个答案:

答案 0 :(得分:0)

Web API通常会将请求的数据作为JSON文件传递。不常见但可能是以XML,HTML TEXT或propietar格式提供。

过程是相同的,您必须解码已传送的数据,分配给变量,并根据需要循环数据。

Tumbler响应是一个JSON格式的文件。

使用json_decode

答案 1 :(得分:0)

I am posting this in the event that someone needs help getting the blog posts on an external site along with some styling. I am using Bootstrap 4 alpha 6 (yeah, alpha...I know). I'm also manually limiting the posts on the page by a simple counter. You could do the same with passing in the limit option on the api call(google it).

I am also using composer, Carbon, and the Tumblr API PHP. If you don't know what those are look at packagist.org开始使用composer并安装php包。我确信这件事可以重构,也许我会在完成后编辑我的帖子,但是现在我觉得这对某人有用。只需编辑它以适应。

哦,这也是一个Twitter提要。只有两列彼此相邻。

<?php 
 require_once('../vendor/autoload.php');
 use Carbon\Carbon;
 $config_file = file_get_contents('../config/config.json');

 $config = json_decode($config_file, true);
 $key = $config['tumblr']['key'];
 $secret = $config['tumblr']['secret'];
 $oauth_token = $config['tumblr']['oauth_token'];
 $oauth_secret = $config['tumblr']['oauth_secret'];

 $client = new Tumblr\API\Client($key, $secret, $oauth_token, $oauth_secret);
 // This may break in the future if Tumblr issues a new oauth token set, it is at that point 
 // that one would create a token callback.
 // $client->setToken($token, $tokenSecret);  

 $info = $client->getUserInfo();

 foreach ($client->getUserInfo()->user->blogs as $blog) {
  // echo $blog->name . "\n";
 }
 $posts = $client->getBlogPosts($blog->name, $options = null);

 echo "<div class='row'>
         <div class='col-md-8'>";
$i = 0;
foreach($posts->posts as $post) {
$post_date = $post->date;
$date = Carbon::parse($post_date)->toFormattedDateString();

$body = $post->body;
if ($post->type == 'text') {
    echo "
      <div class='row mb-2'>
      <div class='card'>
        <div class='card-block'>
          <h4 class='card-title text-left'>{$post->title}</h4>
          <h6 class='card-subtitle mb-2 text-muted text-left'>{$date}</h6>
          <div class='card-text text-justify'>{$body}</div>
          <div class='card-text text-right'>
            <a href='{$post->post_url}' target='_blank'>
              <i class='fa fa-external-link' aria-hidden='true'></i>
            </a>
          </div>
        </div>
      </div>
      </div>";
  } elseif ($post->type == 'photo') {
    $photo = ($post->photos[0]->alt_sizes[2]->url);
    echo "
      <div class='row mb-2'>
      <div class='card'>
        <div class=''>
          <img class='card-img-top rounded blog-img-top pt-4' src='{$photo}' alt='Card image cap'>
        </div>
        <div class='card-block'>
          <div class='card-text text-justify'>{$post->caption}</div>
            <h6 class='card-subtitle mb-2 text-muted text-left'>{$date}</h6>
          <div class='card-text text-right'>
            <a href='{$post->post_url}' target='_blank'>
              <i class='fa fa-external-link' aria-hidden='true'></i>
            </a>
          </div>
        </div>
      </div>
      </div>
    ";
  } elseif ($post->type == 'quote') {
    echo "
      <div class='row mb-2'>
        <div class='card utility-padding-1' style='width: 100%;'>
        <h6 class='card-subtitle mb-2 text-muted text-left'>{$date}</h6>
        <blockquote class='blockquote'>
          <p class='mb-0 text-left'>\"{$post->text}\"</p>
          <div class='blockquote-footer text-left'>{$post->source}</cite></div>
        </blockquote>
        <div class='card-text text-right'>
            <a href='{$post->post_url}' target='_blank'>
              <i class='fa fa-external-link' aria-hidden='true'></i>
            </a>
          </div>
       </div>
      </div>
    ";
  } elseif ($post->type == 'link') {
      $link_photo = ($post->photos[0]->original_size->url);
      echo "
      <div class='row mb-2'>
      <div class='card utility-padding-1' style='width: 100%;'>
        <div class=''>
          <h4 class='card-text text-justify'>{$post->summary}</h4>
          <h6 class='card-subtitle mb-2 text-muted text-left'>{$date}</h6>
          <img class='card-img-top rounded blog-img-top pt-4' src='{$link_photo}' alt='Card image cap'>
        </div>
        <div class='card-block'>
        <div class='card-text text-justify'>{$post->reblog->comment}</div>
          <div class='card-text text-right'>
            <a href='{$post->url}' target='_blank'class='pr-2'>
              <i class='fa fa-link' aria-hidden='true'></i>
            </a>
            <a href='{$post->post_url}' target='_blank'>
              <i class='fa fa-external-link' aria-hidden='true'></i>
            </a>
          </div>
        </div>
      </div>
      </div>
    ";
  } elseif ($post->type == 'video') {
     $video = $post->player[0]->embed_code;
      echo "
      <div class='row mb-2'>
      <div class='card'>
        <div class='embed-responsive embed-responsive-4by3'>
          {$video}
       </div>
        <div class='card-block'>
          <div class='card-text text-justify'>{$post->caption}</div>
            <h6 class='card-subtitle mb-2 text-muted text-left'>{$date}</h6>
          <div class='card-text text-right'>
            <a href='{$post->post_url}' target='_blank'>
              <i class='fa fa-external-link' aria-hidden='true'></i>
            </a>
          </div>
        </div>
      </div>
      </div>
    ";
  } else {
    echo "
      <div class='row mb-2'>
      <div class='card'>
        <div class='card-block'>
          <h4 class='card-title text-left'>Post type not recognized</h4>
          <h6 class='card-subtitle mb-2 text-muted text-left'>{$date}</h6>
          <div class='card-text text-justify'>Call your friendly neighborhood programmer</div>
          <div class='card-text text-right'>
            <a href='{$post->post_url}' target='_blank'>
              <i class='fa fa-external-link' aria-hidden='true'></i>
            </a>
          </div>
        </div>
      </div>
      </div>";
  }
  $i++;
  if($i==5) break;
}
 echo "</div>
      <div class='col-md-4'>
        <a class='twitter-timeline' href='https://twitter.com/[USERNAME]'>Tweets by [USERNAME]</a> <script async src='//platform.twitter.com/widgets.js' charset='utf-8'></script>
      </div>
    </div>";
?>