首页 > 解决方案 > 将 api 数据保存到 db laravel 时出现非法字符串偏移“标题”

问题描述

我正在尝试将数据从 api 保存到我的数据库我有一个函数来获取

public function index()
    {
        $client = new Client();
        $uri = 'http://www.omdbapi.com/?t=tree&apikey=';
        $header = ['headers' => ['X-Auth-Token' => 'My-Token']];
        $res = $client->get($uri, $header);
        $data = json_decode($res->getBody()->getContents(), true);
  return $data;
    }

然后一个函数保存在我的数据库中

    public function store(Request $request)
    {
        $movies = $this->index();
//        dd($this->index());

        collect($movies);
//        dd($movies);

             foreach($movies as $movie) {
//                dd($movie);
                Movie::create([
                    'title' => $movie['Title'],
                     'year' =>$movie['Year'],
                    'type' =>$movie['Type'],
                    'cover_photo' => $movie['Poster'],
                ]);

            }

    }

尝试保存时出现上述错误

标签: laravelapiguzzle

解决方案


推荐阅读