使用Twitter API搜索推文时,“无法使用stdClass类型的对象作为数组”错误

问题描述 投票:1回答:1

我目前正在开发一个项目,我正在尝试搜索推文,然后将它们保存在数据库中。对于这个项目,我正在使用Laravel和TwitterOAuth

这是代码:

class TwitterController extends BaseController
{
public function test() {
    $connection = new TwitterOAuth('',
        '',
        '',
        '');
    $content = $connection->get('account/verify_credentials');

    $tweets = $connection->get('search/tweets',
        ['q' => "cryptocurrency"]);

    foreach($tweets as $tweet) {



        $tweet_id = $tweet[0]->id;
        $oembed_j = file_get_contents('https://publish.twitter.com/oembed?url=https://twitter.com/x/status/' . $tweet_id);
        $oembed = json_decode($oembed_j);

        $twitter = new Twitter;
        $twitter->url = $oembed->url;
        $twitter->author_name = $oembed->author_name;
        $twitter->author_url = $oembed->author_url;
        $twitter->html = $oembed->html;
        $twitter->width = $oembed->width;
        $twitter->height = $oembed->height;
        $twitter->type = $oembed->type;
        $twitter->cache_age = $oembed->cache_age;
        $twitter->provider_name = $oembed->provider_name;
        $twitter->provider_url = $oembed->provider_url;
        $twitter->version = $oembed->version;
        $twitter->save();
    }
}

}

和错误:

"(1/1) FatalErrorException
Cannot use object of type stdClass as array

in TwitterController.php (line 27)"
laravel twitter oauth
1个回答
1
投票

因为$tweets是一个具有statuses属性的对象,所以你需要像这样迭代它:

foreach ($tweets->statuses as $tweet)
© www.soinside.com 2019 - 2024. All rights reserved.