【问题标题】:PHP script for twitter returning too much data用于 twitter 的 PHP 脚本返回太多数据
【发布时间】: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 的格式。
  • 这只是我贴出来的一小部分信息,实际输出太大,放在这里。之后持续了很长时间。

标签: php json parsing twitter


【解决方案1】:

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

我试过https://dev.twitter.com/console,不带引号返回的推文数是15,这是调用返回的默认推文数,这意味着不考虑count参数,我试过的查询是:https://api.twitter.com/1.1/search/tweets.json?q="espn"&amp;count=1&amp;result_type="popular" .

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

$tweets = $connection-&gt;get('https://api.twitter.com/1.1/search/tweets.json?q="'.$hashtag.'"&amp;count='.$notweets.'&amp;result_type="popular"');

【讨论】:

  • 嘿,我的代码末尾确实有"&amp;count=".$notweets,并且$notweets 设置为1,所以我在查询中确实将它设置为1。所以,每个主题标签都应该重新调整 1。但我认为你是正确的,它返回 15 而不是我想的 12。
【解决方案2】:

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

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

问候, 罗宾·海勒。

【讨论】:

    猜你喜欢
    • 2020-10-24
    • 1970-01-01
    • 1970-01-01
    • 2011-04-15
    • 1970-01-01
    • 2023-02-10
    • 1970-01-01
    • 2011-10-23
    • 1970-01-01
    相关资源
    最近更新 更多