0

すべて、Tumblrからいくつかの投稿を取得するための次のコードがあります。

$baseHostname = "name.tumblr.com";
$tumblrConsumerKey = "asfd"; # use your own consumer key here
$humblr = new Humblr($baseHostname, $tumblrConsumerKey);

$post = $humblr->getPosts(array('limit' => 1));
print_r($post);

これはうまく機能し、次のような結果が得られます。

Array ( 
    [0] => stdClass Object ( 
        [blog_name] => name 
        [id] => 43993 
        [post_url] => http://name.tumblr.com/post/43993/
        [slug] => slug 
        [type] => video 
        [date] => 2013-02-25 18:00:25 GMT 
        [timestamp] => 1361815225 
        [state] => published 
        [format] => html )

次のような値をいくつか表示してみます。

echo "The blog name is: ".$post->blog_name;
echo $post->id;

ただし、空白です。これらの値を表示するにはどうすればよいですか?

ありがとう

4

2 に答える 2

1

まず、エラー報告をオンにします。

// error reporting for development environment
error_reporting(-1);
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);

@Zlatan が指摘しているように、これは stdClass の配列です。

エラー報告を有効にすると、次のコードで「Notice: Trying to get property of non-object in ...」というエラー通知が表示されます。

echo "The blog name is: ".$post->blog_name;
echo $post->id;

非オブジェクトにアクセスしようとしているからです。

配列インデックスを介してオブジェクトにアクセスすることで修正できます。

echo "The blog name is: ".$post[0]->blog_name;
echo $post[0]->id;

仮定$posts

Array
(
    [0] => stdClass Object
        (
            [blog_name] => blog1
            [id] => 10234
            [post_url] => http://name.tumblr.com/post/43993/
            [slug] => slug
            [type] => video1
            [date] => 2013-02-25 18:00:25 GMT
            [timestamp] => 1361815225
            [state] => published
            [format] => html
        )

    [1] => stdClass Object
        (
            [blog_name] => blog2
            [id] => 20234
            [post_url] => http://name.tumblr.com/post/43993/
            [slug] => slug1
            [type] => video
            [date] => 2013-02-25 18:00:25 GMT
            [timestamp] => 1361815225
            [state] => published
            [format] => html
        )

)

配列インデックスによるオブジェクトへのアクセス:

echo "The blog name is: ".$post[0]->blog_name;
echo $post[0]->id;
echo "The blog name is: ".$post[1]->blog_name;
echo $post[1]->id;

// prints
// The blog name is: blog1
// 10234
// The blog name is: blog2
// 20234

投稿をループしたい場合:

foreach ($posts as $post) {
    echo "The blog name is: ".$post->blog_name;
    echo $post->id;
}

// prints
// The blog name is: blog1
// 10234
// The blog name is: blog2
// 20234

資力

于 2013-02-25T18:56:40.087 に答える
1

私が見るように、それは配列なので、試すことができます:

echo $post[0]->blog_name;
于 2013-02-25T18:17:50.520 に答える