从twitter响应中向json对象添加元素

时间:2014-03-05 15:11:53

标签: php json twitter

我试图向一个arraay添加一个属性,但我不断提出这些错误:

警告:json_decode()期望参数1为字符串,第25行的C:\ xampp \ htdocs \ 4th \ twitdb2 \ get_tweets.php中给出的对象

警告:在第27行的C:\ xampp \ htdocs \ 4th \ twitdb2 \ get_tweets.php中为foreach()提供的参数无效 空

我想广告is_selected到数组的每个元素的末尾,但我无法理解它。任何帮助都会非常感激。谢谢

  <?php
    session_start();
    require_once("twitteroauth.php"); //Path to twitteroauth library

    $search = "xxxxx";
    $geocode = "xxxxx";
    $notweets = xxxxxx;
    $consumerkey = "xxxxxxx";
    $consumersecret = "xxxxxxxx";
    $accesstoken = "xxxxx-xxxxxx";
    $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);

    $search = str_replace("#", "%23", $search);
    $tweets = $connection->get("https://api.twitter.com/1.1/search/tweets.json?q=".$search."&geocode=".$geocode."&count=".$notweets);


    $result = json_decode($tweets, true);

    foreach($result as $key => $val)
    {
       $result[$key]['is_selected'] = 0;
    }
      echo json_encode($result);





    ?>

var dump

object(stdClass)#5 (2) { ["statuses"]=> array(13) { [0]=> object(stdClass)#6 (23) { ["metadata"]=> object(stdClass)#7 (2) { ["result_type"]=> string(6) "recent" ["iso_language_code"]=> string(2) "nl" } ["created_at"]=> string(30) "Wed Mar 05 11:11:46 +0000 2014" ["id"]=> float(4.411691911448E+17) ["id_str"]=> string(18) "441169191144796160" ["text"]=> string(29) "iPhone charger is broken 😤" ["source"]=> string(82) "Twitter for iPhone" ["truncated"]=> bool(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"]=> object(stdClass)#8 (40) { ["id"]=> int(31950251) ["id_str"]=> string(8) "31950251" ["name"]=> string(17) "Rebecca McDermott" ["screen_name"]=> string(10) "rebeccamcd" ["location"]=> string(0) "" ["description"]=> string(0) "" ["url"]=> NULL ["entities"]=> object(stdClass)#9 (1) { ["description"]=> object(stdClass)#10 (1) { ["urls"]=> array(0) { } } } ["protected"]=> bool(false) ["followers_count"]=> int(53) ["friends_count"]=> int(143) ["listed_count"]=> int(1) ["created_at"]=> string(30) "Thu Apr 16 16:26:14 +0000 2009" ["favourites_count"]=> int(516) ["utc_offset"]=> int(0) ["time_zone"]=> string(6) "Dublin" ["geo_enabled"]=> bool(true) ["verified"]=> bool(false) ["statuses_count"]=> int(1312) ["lang"]=> string(2) "en" ["contributors_enabled"]=> bool(false) ["is_translator"]=> bool(false) ["is_translation_enabled"]=> bool(false) ["profile_background_color"]=> string(6) "ACDED6" ["profile_background_image_url"]=> string(49) "http://abs.twimg.com/images/themes/theme18/bg.gif" ["profile_background_image_url_https"]=> string(50) "https://abs.twimg.com/images/themes/theme18/bg.gif" ["profile_background_tile"]=> bool(false) ["profile_image_url"]=> string(75) "http://pbs.twimg.com/profile_images/432285869324922880/QQiT0W7B_normal.jpeg" ["profile_image_url_https"]=> string(76) "https://pbs.twimg.com/profile_images/432285869324922880/QQiT0W7B_normal.jpeg" ["profile_banner_url"]=> string(57) "https://pbs.twimg.com/profile_banners/31950251/1379612844" ["profile_link_color"]=> string(6) "038543" ["profile_sidebar_border_color"]=> string(6) "EEEEEE" ["profile_sidebar_fill_color"]=> string(6) "F6F6F6" ["profile_text_color"]=> string(6) "333333" ["profile_use_background_image"]=> bool(true) ["default_profile"]=> bool(false) ["default_profile_image"]=> bool(false) ["following"]=> bool(false) ["follow_request_sent"]=> bool(false) ["notifications"]=> bool(false) } ["geo"]=> object(stdClass)#11 (2) { ["type"]=> string(5) "Point" ["coordinates"]=> array(2) { [0]=> float(54.98074354) [1]=> float(-7.30069342) } } ["coordinates"]=> object(stdClass)#12 (2) { ["type"]=> string(5) "Point" ["coordinates"]=> array(2) { [0]=> float(-7.30069342) [1]=> float(54.98074354) } } ["place"]=> object(stdClass)#13 (10) { ["id"]=> string(16) "f2ea0703dc6c7579" ["url"]=> string(56) "https://api.twitter.com/1.1/geo/id/f2ea0703dc6c7579.json" ["place_type"]=> string(4) "city" ["name"]=> string(5) "Derry" ["full_name"]=> string(12) "Derry, Derry" ["country_code"]=> string(2) "GB" ["country"]=> string(14) "United Kingdom" ["contained_within"]=> array(0) { } ["bounding_box"]=> object(stdClass)#14 (2) { ["type"]=> string(7) "Polygon" ["coordinates"]=> array(1) { [0]=> array(4) { [0]=> array(2) { [0]=> float(-7.4070791) [1]=> float(54.8110645) } [1]=> array(2) { [0]=> float(-7.0069043) [1]=> float(54.8110645) } [2]=> array(2) { [0]=> float(-7.0069043) [1]=> float(55.0668188) } [3]=> array(2) { [0]=> float(-7.4070791) [1]=> float(55.0668188) } } } } ["attributes"]=> object(stdClass)#15 (0) { } } ["contributors"]=> NULL ["retweet_count"]=> int(0) ["favorite_count"]=> int(1) ["entities"]=> object(stdClass)#16 (4) { ["hashtags"]=> array(0) { } ["symbols"]=> array(0) { } ["urls"]=> array(0) { } ["user_mentions"]=> array(0) { } } 

1 个答案:

答案 0 :(得分:0)

这应该可以解决问题:

$tweets = $connection->get("https://api.twitter.com/1.1/search/tweets.json?q=".$search."&geocode=".$geocode."&count=".$notweets);

for($i = 0; $i < count($tweets->statuses); $i++) {
    $tweets->statuses[$i]['is_selected'] = 0;
}
echo json_encode($result);
相关问题