【问题标题】:return all the array between one hour via php通过php返回一小时内的所有数组
【发布时间】:2011-09-09 00:19:05
【问题描述】:

假设我们使用搜索 api 进行一些搜索,然后它会在 json 中返回类似这样的内容。

  'results' => 
    array
      0 => 
        object(stdClass)[9]
          public 'from_user_id_str' => string '57393273' (length=8)
          public 'profile_image_url' => string 'http://a1.twimg.com/profile_images/1150792547/49146_1365401218_7951632_q_normal.jpg' (length=83)
          public 'created_at' => string 'Mon, 06 Jun 2011 04:01:36 +0000' (length=31)
          public 'from_user' => string 'primalokomotif' (length=14)
          public 'id_str' => string '77585906684526592' (length=17)
          public 'metadata' => 
            object(stdClass)[10]
              public 'result_type' => string 'recent' (length=6)
          public 'to_user_id' => null
          public 'text' => string 'RT @detikcom kober siap perkenalkan ajaran klub suami suami takut istri' (length=71)
          public 'id' => float 7.7585906684527E+16
          public 'from_user_id' => int 57393273
          public 'geo' => null
          public 'iso_language_code' => string 'id' (length=2)
          public 'to_user_id_str' => null
          public 'source' => string '<a href="http://mobile.twitter.com" rel="nofollow">Mobile Web</a>' (length=97)
      1 => 
        object(stdClass)[11]
          public 'from_user_id_str' => string '278060132' (length=9)
          public 'profile_image_url' => string 'http://a1.twimg.com/profile_images/1355377589/296218442_normal.jpg' (length=66)
          public 'created_at' => string 'Mon, 06 Jun 2011 04:01:34 +0000' (length=31)
          public 'from_user' => string 'kaka_kecil' (length=10)
          public 'id_str' => string '77585897637412864' (length=17)
          public 'metadata' => 
            object(stdClass)[12]
              public 'result_type' => string 'recent' (length=6)
          public 'to_user_id' => null
          public 'text' => string 'RT @detikcom: 'Milan Tak Pernah Inginkan Hamsik' http://de.tk/dP7Cs via @detiksport' (length=83)
          public 'id' => float 7.7585897637413E+16
          public 'from_user_id' => int 278060132
          public 'geo' => null
          public 'iso_language_code' => string 'id' (length=2)
          public 'to_user_id_str' => null
          public 'source' => string '<a href="http://ubersocial.com" rel="nofollow">ÜberSocial</a>' (length=94)
      2 => 
        object(stdClass)[13]
          public 'from_user_id_str' => string '185984810' (length=9)
          public 'profile_image_url' => string 'http://a0.twimg.com/profile_images/1368466468/a_normal.jpg' (length=58)
          public 'created_at' => string 'Mon, 06 Jun 2011 04:01:31 +0000' (length=31)
          public 'from_user' => string 'tiayeeeaah' (length=10)
          public 'id_str' => string '77585887143276544' (length=17)
          public 'metadata' => 
            object(stdClass)[14]
              public 'result_type' => string 'recent' (length=6)
          public 'to_user_id' => null
          public 'text' => string 'hmm..hmm.. -___-" RT @detikcom: Bandung Siap Perkenalkan Ajaran Klub Istri Taat Suami http://bit.ly/kCcOsG' (length=111)
          public 'id' => float 7.7585887143277E+16
          public 'from_user_id' => int 185984810
          public 'geo' => null
          public 'iso_language_code' => string 'id' (length=2)
          public 'to_user_id_str' => null
          public 'source' => string '<a href="http://seesmic.com/seesmic_desktop/sd2" rel="nofollow">Seesmic Desktop</a>' (length=115)
      3 => etc

我们如何返回日期少于一小时的数组?或过滤它?也许像做一个搜索然后返回所有具有 strtotime(created_at)

*编辑

喜欢How can I use the Twitter Search API to return all tweets that match my search query, posted only within the last five seconds? 但是这样做没有答案。

感谢关注

亚当斋月

【问题讨论】:

    标签: php json search twitter


    【解决方案1】:

    所以,您只想返回过去一小时内拥有created_at 的人吗?我喜欢尽可能使用array_filter()

    function filterWithinHour($obj)
    {
        return (strtotime($obj->created_at) > strtotime('-1 hour'));
    }
    
    $recent = array_filter($results, 'filterWithinHour');
    

    如果您需要传递某个时间(而不是假设现在),您可能只想手动循环遍历数组,而不是试图将时间参数传递给带有全局变量的回调。我讨厌这样使用全局变量。

    【讨论】:

    • @wiseguy 我们如何做类似 array_filter($results, 'this->filterWithinHour()'); ?
    • 啊我看到 array($this,"callback_function") 到目前为止它仍然有效,让我们等待另一种实现这一目标的方法。
    • @Adam 不是这样的。回调函数不是类方法;它是一个独立的函数,您可以像上面的示例一样按名称调用它。
    • @Adam 对。另一种方法是像我提到的那样手动循环它们,但它不如使用array_filter() 好。 :-) 如果你愿意,我也可以举个例子。
    • @Adam 我猜array_filter() 可能会快一点,但除非您一次运行 100,000 次,否则它的速度不足以产生巨大的影响。我们可以运行一个小测试来找出答案。 (我明天可能会这样做。)
    【解决方案2】:

    Twitter Search API 有一个 since_id 可以帮助您:

    返回 ID 更大的结果 比(即比)更近 指定的标识。有限制 可以访问的推文数量 通过 API。如果极限 自since_id以来出现了推文, since_id 将被强制为 可用的最早 ID。

    如果您每小时进行一次投票,您可以有效地获得最后一小时的帖子。如果服务关闭,您仍然不会错过任何推文,因为您可以返回几天。

    【讨论】:

    • If you poll every hour, you effectively get the posts of the last hour. 我还是不明白,是的,他们有一个自 ID,但我们怎么知道一条推文的自 ID 不到一个小时?
    • 您之前请求的最新帖子的 id 是数组中第一条推文的id(或id_str。如果您每小时轮询一次 Twitter 并使用 id在您从上一个请求中以since_id 获得的第一条推文中,您只会获得较新的推文。如果您每小时轮询一次,这意味着您会获得最后一小时的推文。
    • 啊,我明白了.. 但我们必须存储/缓存自 id 对吗?然后等待一个小时。
    • +1 好主意。如果此方法适合查询 API 的方式,这可能是首选方法。
    猜你喜欢
    • 1970-01-01
    • 2021-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-12
    相关资源
    最近更新 更多