用于twitter的PHP脚本返回太多数据

时间:2013-12-06 20:48:18

标签: php json parsing twitter

所以,这是我正在运行的当前php脚本。

<?php
session_start();
require_once("twitteroauth.php"); 
$consumerkey = "XXXXXXXXXXXXXXXXXXXXX";
$consumersecret = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
$accesstoken = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
$accesstokensecret = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";

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);

$hashtag_list = array(
    "breakingbad" => "breakingbad",
    "espn" => "espn",
    "gameofthrones" => "gameofthrones",
);
$notweets = 1;

foreach ($hashtag_list as &$hashtag) {
    $tweets = $connection->get("https://api.twitter.com/1.1/search/tweets.json?q=".$hashtag."&result_type=popular&count=".$notweets);

    echo json_encode($tweets);
}
?>

问题是,当我在终端中运行此脚本时,我收到了大量的信息。最重要的是,我没有收到我要求的每个节目的一条推文,而是每个主题标签得到大约12条推文。任何人都可以告诉我为什么我得到这么多的数据。也是为什么Twitter制作1.1并决定以数字方式打击开发人员。

如果您对我要回到这里的内容感到好奇,那么返回的数据类型非常少:

 "statuses":[{"metadata":{"result_type":"popular","iso_language_code":"en"},"created_at":"Thu Dec 05 21:06:49 +0000
  2013","id":408704034040995840,"id_str":"408704034040995840","text":"Let's take it all the
  way back. See the latest #BreakingBad news: http:\/\/t.co\/dINizWJhqh #tbt 
  http:\/\/t.co\/tfYirPuMNv","source":"<a href=\"http:\/\/www.socialflow.com\" 
        rel=\"nofollow\">SocialFlow<\/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":25399731,"id_str":"25399731","name":"Breaking Bad","screen_name":"BreakingBad_AMC","location":"Albuquerque, New Mexico ","description":"For more information, go to http:\/\/t.co\/RvmSIdNAJH. #BreakingBad  http:\/\/t.co\/SuRyGtdteo","url":"http:\/\/t.co\/mr1UzInsNt","entities":{"url":{"urls":[{"url":"http:\/\/t.co\/mr1UzInsNt","expanded_url":"http:\/\/www.amc.com","display_url":"amc.com","indices":[0,22]}]},"description":{"urls":
    [{"url":"http:\/\/t.co\/RvmSIdNAJH","expanded_url":"http:\/\/amc.com","display_url":"amc.com","indices":[28,50]},

 {"url":"http:\/\/t.co\/SuRyGtdteo","expanded_url":"http:\/\/breakingbadamc.tumblr.com\/","display_url":"breakingbadamc.tumblr.com","indices":
    [66,88]}]}},"protected":false,"followers_count":769339,"friends_count":2051,"listed_count":3379,"created_at":"Thu Mar 19 22:32:21 +0000 
    2009","favourites_count":1462,"utc_offset":-18000,"time_zone":"Quito","geo_enabled":true,"verified":true,"statuses_count":3594,"lang":"en","contributors_enabled":false,"is_translator":
    false,"profile_background_color":"000000","profile_background_image_url":"http:\/\/a0.twimg.
    com\/profile_background_images\/378800000085590192\/f6daf83a1f1a3061158eea710e43c246.png",
    "profile_background_image_url_https":"https:\/\/si0.twimg.com\/profile_background_images\/378800000085590192\/f6daf83a1f1a3061158eea710e43c246.png","profile_background_tile":false,
    "profile_image_url
    ":"http:\/\/pbs.twimg.com\/profile_images\/3764660477\/e0df076073170905b32e62bdb75dc04d_normal.jpeg","profile_image_url_https"

现在约有5%的数据返回。现在,这不是一个大问题,因为我可以解析JSON,但我不明白为什么我不能得到我想要的单个推文。为什么这么多?我只需要实际文本。

2 个答案:

答案 0 :(得分:1)

您可以尝试双引号字符串查询参数,q参数和result_type参数。

我尝试https://dev.twitter.com/console并且没有引号返回的推文数量是15,这是调用返回的默认推文数量,这意味着不考虑count参数,我尝试的查询是:{ {1}}。

在你的情况下它应该是这样的:

https://api.twitter.com/1.1/search/tweets.json?q="espn"&count=1&result_type="popular"

答案 1 :(得分:0)

问题是可以在这里使用json_encode() - 你通过twitters API接收JSON数据然后再次重新编码JSON,这可能是奇怪格式化的原因。

除此之外, /search/tweets.json 上的API(请注意 s )旨在为您提供一组推文(https://dev.twitter.com/docs/api/1.1/get/search/tweets )。但是,我找不到提供搜索单个推文的方法。

映入眼帘, 罗宾海勒。