Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
非法字符串偏移量';标题';将api数据保存到db laravel时_Laravel_Api_Guzzle - Fatal编程技术网

非法字符串偏移量';标题';将api数据保存到db laravel时

非法字符串偏移量';标题';将api数据保存到db laravel时,laravel,api,guzzle,Laravel,Api,Guzzle,我正在尝试将数据从api保存到我的数据库 我有一个函数要取 public function index() { $client = new Client(); $uri = 'http://www.omdbapi.com/?t=tree&apikey='; $header = ['headers' => ['X-Auth-Token' => 'My-Token']]; $res = $client->

我正在尝试将数据从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'],
                ]);

            }

    }
在尝试保存时,出现上述错误

您的
index()
方法仅返回单个电影的属性数组,而不是电影列表,因此如果您将
store()
方法改为存储单个电影,则该方法将起作用:

公共函数存储(请求$Request)
{
$movies=$this->index();
电影::创建([
'title'=>$movie['title'],
“导演”=>$movie[“导演”],
“发布日期”=>$movie[“发布”],
'plot'=>$movie['plot'],
“演员”=>$movie[“演员”],
'runtime'=>$movie['runtime'],
“流派”=>$movie[“流派”],
“制作”=>$movie[“制作”],
'writer'=>$movie['writer'],
“封面图片”=>$movie[“海报”],
'language'=>$movie['language'],
'rated'=>$movie['rated']
]);
返回响应('success!')
}
根据所用API的类型,将
t=tree
更改为
s=tree
将获得列表,而不是单个实例:

公共功能索引()
{
$client=新客户端();
$uri='1http://www.omdbapi.com/?s=tree&apikey=';
$header=['headers'=>['X-Auth-Token'=>'My-Token']];
$res=$client->get($uri,$header);
$data=json_decode($res->getBody()->getContents(),true);
返回$data;
}

您可以添加添加($this->index())时得到的内容吗?你能给我们看看json格式的数据吗?对于dd($this->index());