use GuzzleHttp\Client; $client = new Client(['base_uri' => 'https://example.com']); $response = $client->request('GET', '/api/data'); $body = $response->getBody()->getContents(); echo $body;
use Symfony\Component\HttpClient\HttpClient; $client = HttpClient::create(); $response = $client->request('GET', 'https://example.com/api/data'); $body = $response->getContent(); echo $body;
$ch = curl_init('https://example.com/api/data'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $body = curl_exec($ch); curl_close($ch); echo $body;In this example, we are using the cURL extension to send a GET request to a server API that returns data. The response body is obtained by using the `curl_exec` function to execute the request and store the data in the `$body` variable. Finally, we close the cURL session and print the body contents to the client. In conclusion, response body manipulation in PHP can be achieved using different package libraries like GuzzleHttp, Symfony HttpClient, or using the cURL extension. These examples illustrate how to use these libraries to retrieve data from a server API and print it to the client.