【发布时间】:2013-12-24 08:37:33
【问题描述】:
所以,这是我当前正在运行的 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,但我不明白为什么我不能得到我想要的单条推文。为什么这么多?我需要的只是实际的文本。
【问题讨论】:
-
我可能弄错了,但这个 json 对我来说看起来像是一条推文,你可以做的是尝试 dev.twitter.com/console 的每个请求并检查你的 json 的格式。
-
这只是我贴出来的一小部分信息,实际输出太大,放在这里。之后持续了很长时间。