Tumblr API 如何在 php 中访问以下数组中的各个值

发布于 2024-12-17 04:15:27 字数 918 浏览 4 评论 0原文

我正在使用 tumblr API 和以下代码:

$var = xhttp::toQueryArray($response['body']);
print_r($var);

这会在屏幕上打印以下内容:

Array ( [{"meta":{"status":200,"msg":"OK"},"response":{"user":{"name":"lukebream","likes":0,"following":8,"default_post_format":"html","blogs":[{"name":"lukebream","url":"http:\/\/lukebream.tumblr.com\/","followers":5,"primary":true,"title":"Untitled","admin":true,"queue":0,"ask":false,"tweet":"N"}]}}}] => )

如何访问各个元素并将它们分配给变量?

这是我完成的内容:

$tumblr->set_token($_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);
$data = array();
$data['post'] = array();        
$response = $tumblr->fetch('http://api.tumblr.com/v2/user/info', $data);    

if($response['successful']) {
    echo $response['json']['response']['url'];
} else {
    echo "api call failed. {$response[body]}<br><br>";
}

I'm using the tumblr API and the following code:

$var = xhttp::toQueryArray($response['body']);
print_r($var);

This print on the screen the following:

Array ( [{"meta":{"status":200,"msg":"OK"},"response":{"user":{"name":"lukebream","likes":0,"following":8,"default_post_format":"html","blogs":[{"name":"lukebream","url":"http:\/\/lukebream.tumblr.com\/","followers":5,"primary":true,"title":"Untitled","admin":true,"queue":0,"ask":false,"tweet":"N"}]}}}] => )

How can I access the individual elements and assign them to variables?

Here is what I have finished with:

$tumblr->set_token($_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);
$data = array();
$data['post'] = array();        
$response = $tumblr->fetch('http://api.tumblr.com/v2/user/info', $data);    

if($response['successful']) {
    echo $response['json']['response']['url'];
} else {
    echo "api call failed. {$response[body]}<br><br>";
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你不是我要的菜∠ 2024-12-24 04:15:27

它称为 JSON,您可以使用 json_decode()

使用示例:

//I used file_get_contents() to keep things simple
$jsonData = file_get_contents("http://api.tumblr.com/v2/blog/lukebream.tumblr.com/info?api_key=<api_key_here>"); 

$jsonData

{
    "meta":{
        "status":200,
        "msg":"OK"
        },
    "response":{
        "blog":{
            "title":"Untitled",
            "posts":61,
            "name":"lukebream",
            "url":"http:\/\/lukebream.tumblr.com\/",
            "updated":1321830278,
            "description":"",
            "ask":false,
            "likes":0
            }
        }
}

后面 包含 :经过json_decode(),我们得到一个 PHP 对象,因此 :

$obj = json_decode($jsonData);

将返回 :

stdClass Object
(
    [meta] => stdClass Object
        (
            [status] => 200
            [msg] => OK
        )
    [response] => stdClass Object
        (
            [blog] => stdClass Object
                (
                    [title] => Untitled
                    [posts] => 61
                    [name] => lukebream
                    [url] => http://lukebream.tumblr.com/
                    [updated] => 1321830278
                    [description] => 
                    [ask] => 
                    [likes] => 0
                )
        )
)

然后您可以像访问任何其他对象一样访问数据。

It's called JSON, you can parse it using json_decode()

Usage Example :

//I used file_get_contents() to keep things simple
$jsonData = file_get_contents("http://api.tumblr.com/v2/blog/lukebream.tumblr.com/info?api_key=<api_key_here>"); 

The $jsonData contains :

{
    "meta":{
        "status":200,
        "msg":"OK"
        },
    "response":{
        "blog":{
            "title":"Untitled",
            "posts":61,
            "name":"lukebream",
            "url":"http:\/\/lukebream.tumblr.com\/",
            "updated":1321830278,
            "description":"",
            "ask":false,
            "likes":0
            }
        }
}

after it goes through json_decode(), we get a PHP object, so :

$obj = json_decode($jsonData);

will return :

stdClass Object
(
    [meta] => stdClass Object
        (
            [status] => 200
            [msg] => OK
        )
    [response] => stdClass Object
        (
            [blog] => stdClass Object
                (
                    [title] => Untitled
                    [posts] => 61
                    [name] => lukebream
                    [url] => http://lukebream.tumblr.com/
                    [updated] => 1321830278
                    [description] => 
                    [ask] => 
                    [likes] => 0
                )
        )
)

Then you can access the data like with any other object.

没有你我更好 2024-12-24 04:15:27

您还可以使用 json_decode($str, TRUE):这将返回一个 ARRAY 而不是一个对象,更容易使用!

You can also use json_decode($str, TRUE): this will return an ARRAY instead of an object, much easier to play with!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文