P粉8422150062023-08-27 13:29:24
將$client
當作此類的全域變數。
然後在建構函數中設定值:
public $client public function __construct() { $this->client = new Client(['base_uri' => 'https://jsonplaceholder.typicode.com/']); }
愉快的編碼...
P粉6210339282023-08-27 10:48:41
你的$client變數的作用範圍僅限於建構函式內部。如果你想在其他地方存取它,你需要將它賦值給某種類別屬性;
private $client; public function __construct() { $this->client = new Client(['base_uri' => 'https://jsonplaceholder.typicode.com/']); } public function getInfo(Request $request) { try { $response = $this->client->request('GET', 'posts'); //... }