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'); //... }