class Person { private $name; private $age; public function __construct($name, $age){ $this->name = $name; $this->age = $age; } } // Create a new Person object $person = new Person("John", 30);
use GuzzleHttp\Client; class MyApi { private $client; public function __construct($baseUrl, $apiKey){ $this->client = new Client([ 'base_uri' => $baseUrl, 'headers' => [ 'Authorization' => 'Bearer ' . $apiKey, 'Accept' => 'application/json' ] ]); } public function getUser($id){ $response = $this->client->get('/users/' . $id); return json_decode($response->getBody()); } } // Create a new MyApi object $api = new MyApi("https://api.example.com", "abc123"); // Get user with id 123 $user = $api->getUser(123);This code creates a MyApi object that uses the GuzzleHttp package to make HTTP requests to an API. The __construct method sets up the client object with the base URL and API key. The getUser method uses the client to get a user with a specific ID from the API. The package/library used in this example is GuzzleHttp.