【问题标题】:fetch first five posts using tumblr API使用 tumblr API 获取前五个帖子
【发布时间】:2011-07-11 17:05:00
【问题描述】:
$feedURL = 'http://########.tumblr.com/api/read/';
$xml = simplexml_load_file($feedURL);

foreach($xml->posts->post as $post){
   $posts = (string) $post->{'photo-caption'}; 
   $img = (string) $post->{'photo-url'};
   echo "<div style='width:518px;height:300px;'><div style='width:200px;height:200px;float:left;'>".'<img style="width:200px;height:200px;" src="' . $img . '" />'."</div><div style='width:300px;float:right;'>".$posts."</div></div><br>";
}

我已使用此代码显示所有可用的帖子,但我只想显示前五个帖子

【问题讨论】:

    标签: php api tumblr


    【解决方案1】:

    根据to the tumblr API,您可以附加一个查询字符串参数num 来限制从API 调用返回的帖子数。我没有使用 Tumblr API 的个人经验,但你可以尝试这样的方法来限制阅读的帖子数量:

    $numPosts = 5;
    $feedURL = "http://########.tumblr.com/api/read/?num=$numPosts";
    $xml = simplexml_load_file($feedURL);
    // etc. etc. 
    

    【讨论】:

    【解决方案2】:

    阅读 API 文档。 http://www.tumblr.com/docs/en/api#api_read

    如果您提供?num=5,您将获得 5 个结果。

    另一方面,您可以使用以下内容:

    $posts = $xml->posts->post;
    $posts = array_slice($posts, 0, 5);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-02
      • 1970-01-01
      • 2014-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多