首页  >  问答  >  正文

参数 1 传递给 Symfony\Component\HttpFoundation\Request::__construct()

我有这样的错误:

传递给 Symfony\Component\HttpFoundation\Request::__construct() 的参数 1 必须是给定的数组、字符串类型,在 C:\xampp\htdocs\satusehat2\app\Http\Controllers\PasienController 中调用.php 第 68 行。

这是我的功能

public function curl_postman() {
        $client = new Client();
        $headers = [
          'Content-Type' => 'application/json',
          'Authorization' => 'My Bearer token'
        ];
        $body = '';
        $request = new Request('GET', 'my-api-address', $headers, $body);
        $res = $client->sendAsync($request)->wait();
        echo $res->getBody();
        
    }

第 68 行是

$body = '';

P粉378890106P粉378890106210 天前230

全部回复(1)我来回复

  • P粉124070451

    P粉1240704512024-02-27 10:02:10

    您可以使用 Symfony\Component\HttpFoundation\Request::create() 来代替,它隐式调用请求工厂并返回 Request 对象。

    $request = Request::create(uri: 'my-api-address', content: $body, server: $headers)

    PS:不需要显式指定method参数,因为'GET'是默认值。

    回复
    0
  • 取消回复