0

jsonから特定のアイテムを取得するのを手伝ってくれる人はいますか?

これが私のコードです

<?php
    require_once("../twitter/twitteroauth.php"); //Path to twitteroauth library
    $twitteruser = "SokSovat";
    $notweets = 1;
    $consumerkey = "XXXXX";
    $consumersecret = "XXXXXX";
    $accesstoken = "XXXXXXX";
    $accesstokensecret = "XXXXXXX";

    function getConnectionWithAccessToken($cons_key, $cons_secret, $oauth_token, $oauth_token_secret) {
        $connection = new TwitterOAuth($cons_key, $cons_secret, $oauth_token, $oauth_token_secret);
        return $connection;
    }

    $connection = getConnectionWithAccessToken($consumerkey, $consumersecret, $accesstoken, $accesstokensecret);

    $tweets = $connection->get("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=".$twitteruser."&count=".$notweets);

    echo json_encode($tweets);

?>

出力

[
   {
      "created_at":"Thu Aug 15 06:20:15 +0000 2013",
      "id":3.6789347939897e+17,
      "id_str":"367893479398965248",
      "text":"\u3050\u308b\u306a\u3073\u98df\u5e02\u5834\u3067\u7523\u5730\u76f4\u9001\u30fb\u7279\u7523\u54c1\u3001\u53b3\u9078\u30bb\u30ec\u30af\u30c8\u3057\u305f\u30b0\u30eb\u30e1\u3092\u304a\u53d6\u308a\u5bc4\u305b\uff01 http:\/\/t.co\/IQRCSt2sFC\n[PR]",
      "source":"Tweet Button<\/a>",
      "truncated":false,
      "in_reply_to_status_id":null,
      "in_reply_to_status_id_str":null,
      "in_reply_to_user_id":null,
      "in_reply_to_user_id_str":null,
      "in_reply_to_screen_name":null,
      "user":{
         "id":963434528,
         "id_str":"963434528",
         "name":"Sok Sovat",
         "screen_name":"SokSovat",
         "location":"Cambodia",
         "description":"It is better to have a piece of bread with a happy heart than to have a lot of money with sadness..!!",
         "url":"http:\/\/t.co\/2QnB2KpT",
         "entities":{
            "url":{
               "urls":[
                  {
                     "url":"http:\/\/t.co\/2QnB2KpT",
                     "expanded_url":"http:\/\/soksovat.net63.net",
                     "display_url":"soksovat.net63.net",
                     "indices":[
                        0,
                        20
                     ]
                  }
               ]
            },
            "description":{
               "urls":[

               ]
            }
         },
         "protected":false,
         "followers_count":10,
         "friends_count":26,
         "listed_count":0,
         "created_at":"Thu Nov 22 01:47:04 +0000 2012",
         "favourites_count":0,
         "utc_offset":null,
         "time_zone":null,
         "geo_enabled":false,
         "verified":false,
         "statuses_count":174,
         "lang":"en",
         "contributors_enabled":false,
         "is_translator":false,
         "profile_background_color":"C0DEED",
         "profile_background_image_url":"http:\/\/a0.twimg.com\/images\/themes\/theme1\/bg.png",
         "profile_background_image_url_https":"https:\/\/si0.twimg.com\/images\/themes\/theme1\/bg.png",
         "profile_background_tile":false,
         "profile_image_url":"http:\/\/a0.twimg.com\/profile_images\/2879880469\/060f9351577d0ab019fa5b1036210931_normal.jpeg",
         "profile_image_url_https":"https:\/\/si0.twimg.com\/profile_images\/2879880469\/060f9351577d0ab019fa5b1036210931_normal.jpeg",
         "profile_link_color":"0084B4",
         "profile_sidebar_border_color":"C0DEED",
         "profile_sidebar_fill_color":"DDEEF6",
         "profile_text_color":"333333",
         "profile_use_background_image":true,
         "default_profile":true,
         "default_profile_image":false,
         "following":null,
         "follow_request_sent":false,
         "notifications":null
      },
      "geo":null,
      "coordinates":null,
      "place":null,
      "contributors":null,
      "retweet_count":0,
      "favorite_count":0,
      "entities":{
         "hashtags":[

         ],
         "symbols":[

         ],
         "urls":[
            {
               "url":"http:\/\/t.co\/IQRCSt2sFC",
               "expanded_url":"http:\/\/bit.ly\/150qrue",
               "display_url":"bit.ly\/150qrue",
               "indices":[
                  36,
                  58
               ]
            }
         ],
         "user_mentions":[

         ]
      },
      "favorited":false,
      "retweeted":false,
      "possibly_sensitive":false,
      "lang":"ja"
   }
]

私が欲しいもの

実際、私は取得したいだけです

"text":"\u3050\u308b\u306a\u3073\u98df\u5e02\u5834\u3067\u7523\u5730\u76f4\u9001\u30fb\u7279\u7523\u54c1\u3001\u53b3\u9078\u30bb\u30ec\u30af\u30c8\u3057\u305f\u30b0\u30eb\u30e1\u3092\u304a\u53d6\u308a\u5bc4\u305b\uff01 http:\/\/t.co\/IQRCSt2sFC\n[PR]"

テキスト内のコンテンツのみを表示するにはどうすればよいですか?

4

3 に答える 3

3

使用する

echo json_encode($tweets[0]['text']);
于 2013-08-16T04:07:57.627 に答える
0
// decode json result to access each key in the array
$la_data = json_decode($tweets);

$lo_tweet = reset($la_data);

if (!is_object($lo_tweet))
    exit;

echo $lo_tweet->text;

これは昨日私のために働いた

于 2013-08-16T04:17:37.893 に答える
0

$tweets = $connection->get(" https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name= ".$twitteruser."&count=".$notweets);

objects の配列を返すので、json_encode の前に簡単に取得できます。

echo $tweets[0]->text;

json_encode の後に同じことが必要な場合は、次のようにします。

あなたのコード

echo $json=json_encode($tweets); // 出力を変数に割り当てます

$arr=json_decode($json);

echo $arr[0]->text;

于 2013-08-16T04:18:56.693 に答える