2016-05-01 17 views
0

tumblr-apiを使ってブログから投稿を呼び出すのが難しいです。私はapi consoleを使用して、この変数を取得するコードを取得しますが、変数を取得してhtmlに出力する方法は理解できません。tumblr APIがPHPで投稿を取得しています

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

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

これは私が知る限り変数を取得します。 誰かがエコーステートメントの変数(ブログ、このうちの2つだけ)を呼び出す方法を知っていますか、または少なくともこの後にコードで書式設定されたテキストを取得するには? This pageにはドキュメントがありますが、共有しているものを使用する方法がわかりません。

+0

必要に応じて配列をループします.json、オブジェクト、配列を展開しますか? – datelligence

答えて

0

ウェブAPIが要求されたデータをJSONファイルとして配信するのは、通常はそうです。通常はそうではありませんが、XML、HTML TEXT、またはプロタイプの形式で提供することが可能です。

プロセスは同じです。配信されたデータをデコードし、変数に割り当て、必要に応じてデータをループする必要があります。

タンブラーレスポンスはJSON形式のファイルです。

json_decodeを使用してください。

+0

あなたは例を挙げることができます、私は十分に簡単にそれを把握するためにPHPについて知りません、申し訳ありません!あなたの言うことがうまくいくはずです。 – tecksup

0

私は誰かがスタイリングと一緒に外部サイトのブログ投稿を得るのを助ける必要がある場合にこれを投稿しています。私はブートストラップ4アルファ6(ええ、アルファ...私は知っている)を使用しています。また、簡単なカウンターでページの投稿を手動で制限しています。あなたは、APIの呼び出し(Googleそれ)の制限オプションに渡すと同じことをすることができます。

私はComposer、Carbon、Tumblr API PHPも使用しています。コンパイラを使用してPHPパッケージをインストールするには、それらが何であるかわからない場合packagist.org。私はこのことがリファクタリングできると確信しています、そして、私はそれが完了したら私の投稿を編集するかもしれませんが、今はこれが誰かにとって役に立つかもしれないと思います。これに合わせて編集してください。

ああ、これはTwitterフィードでも同様です。お互いの隣にちょうど2列。

<?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>"; 
?> 
関連する問題