为什么我不能使用Illuminate\Support\Facades\Http类在laravel中使用Guzzle获取数据?

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

我是一个新的消费api的用户。https:/jsonplaceholder.typicode.composts。 在Laravel中,虽然我已经导入了Laravel文档中使用的Http类,但是却出现了类未导入的错误.

这是我的控制器 ApiController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;

class ApiController extends Controller
{



public function fetch()
{
    $response = Http::get('https://jsonplaceholder.typicode.com/posts');
    dd($response);
}


}

这是我收到的错误

"Class 'Illuminate\Support\Facades\Http' not found"

我已经安装了安装Guzzle包。请问我做错了什么。

php laravel guzzle
1个回答
0
投票

你似乎是用Laravel 7的方式来使用Guzzle. 把你的Controller改成这样就可以在Laravel 5.8中使用了.

<?php

namespace App\Http\Controllers;

use GuzzleHttp\Client;

class ApiController extends Controller
{
    public function fetch()
    {
        $client = new Client;
        $request = $client->get('https://jsonplaceholder.typicode.com/posts');
        $response = $request->getBody();

        dd($response);
    }
}

编辑: 要得到请求的内容, 可以用 dd($response->getContents()); 而不是 dd($response);

© www.soinside.com 2019 - 2024. All rights reserved.