Laravel &引用;无法将stdClass类型的对象用作数组;使用Twitter API搜索推文时出错
我目前正在做一个项目,我试图搜索tweet,然后将它们保存在数据库中。对于这个项目,我使用了Laravel和 下面是代码:Laravel &引用;无法将stdClass类型的对象用作数组;使用Twitter API搜索推文时出错,laravel,twitter,oauth,Laravel,Twitter,Oauth,我目前正在做一个项目,我试图搜索tweet,然后将它们保存在数据库中。对于这个项目,我使用了Laravel和 下面是代码: class TwitterController extends BaseController { public function test() { $connection = new TwitterOAuth('', '', '', ''); $content = $connection->get('ac
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)"
因为
$tweets
是一个具有status
属性的对象,所以需要像这样对其进行迭代:
foreach ($tweets->statuses as $tweet)
请突出显示第27行。我猜是这一个`foreach($tweets as$tweet){`因此错误表明$tweets不是一个数组,所以可能是循环错误请显示
dd($tweets);
在foreach
clause@AlexeyMezenindd($tweets)结果为:{#167▼ +“状态”:数组:15[▶] +“搜索元数据”:{#495▶} }@TimoGüntner您需要扩展列表并将其发布到您的问题中。在这种情况下,我们可以帮助您。或者,如果对象不是太大,可以制作一个屏幕截图。@AlexeyMezenin哇,对不起,忘记了。这是一个扩展了大部分内容的粘贴箱:谢谢,这很有效。但现在,我在尝试添加时,tweet的html出现了另一个问题将其插入DB:SQLSTATE[22007]:无效的日期时间格式:1366不正确的字符串值:“\xF0\x9F\x98\x81…”用于第1Glad行的“html”列。您应该询问另一个问题并发布与此新日期时间格式错误相关的所有代码。