tumblr API 在 php 中获得帖子

tumblr API getting posts in php

我在使用 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'));

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

Web API 通常将请求的数据作为 JSON 文件传送。不常见但可能以 XML、HTML 文本或 propietar 格式提供。

过程是一样的,你必须解码传递的数据,分配给一个变量,并根据需要循环数据。

Tumbler 回复是一个 JSON 格式的文件。

使用json_decode.

如果有人需要帮助在外部网站上获取博客 post 以及一些样式,我会 post 进行此操作。我正在使用 Bootstrap 4 alpha 6(是的,alpha...我知道)。我还通过一个简单的计数器手动限制页面上的 posts。您可以通过在 api 调用(google it)上传递限制选项来做同样的事情。

我也在使用作曲家、Carbon 和 Tumblr API PHP。如果您不知道这些是什么,请查看 packagist.org 以开始使用 composer 并安装 php 软件包。我确信这件事可以重构,也许我会在完成后编辑我的 post,但现在我认为这可能对某些人有用。只需编辑它以适应。

哦,这也会引入 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>";
?>