P粉8422150062023-08-27 13:29:24
Configure $client
as a global variable of this class.
Then set the value in the constructor:
public $client public function __construct() { $this->client = new Client(['base_uri' => 'https://jsonplaceholder.typicode.com/']); }
Happy coding...
P粉6210339282023-08-27 10:48:41
The scope of your $client variable is limited to inside the constructor. If you want to access it elsewhere, you need to assign it to some kind of class attribute;
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'); //... }