【问题标题】:Tumblr API file_get_contentsTumblr API 文件获取内容
【发布时间】:2012-05-02 15:54:09
【问题描述】:

我正在尝试使用 Tumblr API,我相信它的 JSON。我正在尝试使用 Php 在下面的 URL 中获取“posts-total”并将其回显。我该怎么做?

http://example.tumblr.com/api/read/json?num=0

谢谢

Update:

我尝试使用

$result = json_decode(file_get_contents('http://example.tumblr.com/api/read/json?num=0'));
$print_r($result); 
echo $result[1]; 

我收到 500 错误。当我试图回显 $result 时,我什么也得不到。

【问题讨论】:

  • 我打错字了-应该是 print_r- 这可能会引发您的 500 错误。问题是 url 返回一个带有 json 的变量,而不仅仅是一个纯 json

标签: php api file-get-contents tumblr


【解决方案1】:

我遇到了类似的问题,我想我已经解决了。

var tumblr_api_read = {"tumblelog":{"title":"Ex-Sample","description":"A sample document used to provide examples of the various [and <i>varying<\/i>] HTML tags Tumblr puts out. Quote, specifically, has a lot of variants.","name":"example","timezone":"US\/Eastern","cname":false,"feeds":[]},"posts-start":0,"posts-total":"20","posts-type":false,"posts":[]};

似乎被发回的 json 有利于 javascript 而不是纯 json。因此 json_decode 函数在尝试解析它时遇到问题。看来您可以在 url 中添加 debug=1 参数并接收 PHP 可以处理的 json 字符串。

$result = json_decode(file_get_contents('http://example.tumblr.com/api/read/json?num=0&debug=1'));
$print_r($result); 
echo $result[1];

【讨论】:

    【解决方案2】:

    简单的php-

    $result = json_decode(file_get_contents('http://example.tumblr.com/api/read/json?num=0'));
    print_r($result); //will show contents return in an araay format
    echo $result[1]; //or whatever the element is
    

    【讨论】:

    • 我收到 500 错误。当我回显 $result 时,我什么也得不到。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-05
    • 1970-01-01
    • 2013-01-16
    • 2020-12-01
    • 1970-01-01
    • 2010-11-25
    相关资源
    最近更新 更多