我有這樣的錯誤:
傳遞給 Symfony\Component\HttpFoundation\Request::__construct() 的參數(shù) 1 必須是給定的數(shù)組、字符串類型,在 C:\xampp\htdocs\satusehat2\app\Http\Controllers\PasienController 中調(diào)用.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 = '';
您可以使用 Symfony\Component\HttpFoundation\Request::create()
來代替,它隱式調(diào)用請求工廠并返回 Request
對象。
$request = Request::create(uri: 'my-api-address', content: $body, server: $headers)
PS:不需要顯式指定method
參數(shù),因為'GET'
是默認值。