【问题标题】:Accessing tweets from home timeline in Twitter using oAuth in PHP?使用 PHP 中的 oAuth 从 Twitter 中的家庭时间线访问推文?
【发布时间】:2011-05-31 22:16:16
【问题描述】:

我正在尝试打印用户时间轴中的第一条推文。

$hometimeline = $twitterObj->get_statusesHome_timeline();
$stream=$hometimeline->responseText;
$user=$stream[0]->user;
$tweet=$user->text;
echo $tweet;

这不会打印任何东西。我在这里做错了什么?

如果我回显$hometimeline->responseText,它会显示在屏幕上
[
{"in_reply_to_status_id_str":null, "coordinates":null, "geo":null, "user":{"profile_use_background_image":true,"text":"我回来了",....},"id_str": "121212"},

{ ... 以此类推} ]

【问题讨论】:

    标签: php twitter twitter-oauth


    【解决方案1】:

    你得到一个 json 字符串作为响应。使用json_decode 解析响应,您将能够将其作为数组或标准对象进行管理

    在与响应交互之前执行此操作

    $stream = json_decode( $hometimeline->responseText, true);
    

    编辑

    请记住,您始终可以使用print_r 进行调试

    【讨论】:

    • 我认为我不应该对此进行解码。我应该能够直接访问 JSON 值,不应该。就像 $twitterInfo->screen_name 会直接给我来自 JSON 响应的屏幕名称。
    • 没有。你至少需要运行一次json_decode。您将响应用作对象或数组的事实取决于您是否将true 作为json_decode 的第二个参数
    【解决方案2】:

    试试这个:

    $hometimeline = $twitterObj->get_statusesHome_timeline();
    $stream = json_decode( $hometimeline->responseText );
    $user=$stream[0]->user;
    $tweet=$user->text;
    echo $tweet;
    

    json_decode 将您收到的 JSON 字符串转换为 PHP 对象。

    【讨论】:

    • $stream = ... 行之后执行print_r( $stream ); exit; 看看会发生什么。
    • Array ( [0] => stdClass Object ( [in_reply_to_status_id_str] => [coordinates] => [geo] => [user] => stdClass Object ( [is_translator] => [default_profile] => [profile_use_background_image] => 1 [protected] => [follow_request_sent] => [statuses_count] => 14439 [friends_count] => 69 [profile_background_color] => ffffff ...... 之类的东西我也尝试过使用 $stream[0]["user"]["text"] 但这会导致错误
    • 您可以看到 $stream[0]->user 是一个 StdClass 类型的对象,并且它具有各种属性(is_translatorstatuses_countfriends_count 等),因此解码工作正常,但您还没有包含足够的内容来查看用户是否有 text 属性。如果$stream[0]->user->text 没有值,那么要么你的属性名称错误,要么这个特定实例的值实际上是空的。
    • 啊,让我在几分钟内完成这个
    • aargh....你是对的,文本并不是用户对象的一部分,我所要做的就是 $stream[0]->text ... 很抱歉
    猜你喜欢
    • 1970-01-01
    • 2013-04-16
    • 1970-01-01
    • 1970-01-01
    • 2011-10-04
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多