php-laravelHow do I make a request in Laravel using PHP?
Making a request in Laravel using PHP is easy and straightforward. Here is an example of a GET request:
<?php
$client = new \GuzzleHttp\Client();
$response = $client->request('GET', 'https://example.com/');
echo $response->getStatusCode(); // 200
echo $response->getHeaderLine('content-type'); // 'application/json; charset=utf8'
echo $response->getBody(); // '{"id": 1420053, "name": "guzzle", ...}
In the example above, we create a new GuzzleHttp\Client
object, which is used to make the request. We then call the request
method, passing in the HTTP verb (GET) and the URL to which we want to make the request. We then get the response status code, header line and body and echo them out.
Code explanation
$client = new \GuzzleHttp\Client();
- Creates a newGuzzleHttp\Client
object which will be used to make the request.$response = $client->request('GET', 'https://example.com/');
- Calls therequest
method on the$client
object, passing in the HTTP verb (GET) and the URL to which we want to make the request.echo $response->getStatusCode();
- Echoes out the response status code.echo $response->getHeaderLine('content-type');
- Echoes out the response header line.echo $response->getBody();
- Echoes out the response body.
For more information, please refer to the Laravel Documentation and Guzzle Documentation.
More of Php Laravel
- How do I set up a .gitlab-ci.yml file for a Laravel project using PHP?
- How can I use the @yield directive in PHP Laravel?
- How can I configure Nginx to work with Laravel on a PHP server?
- How can I generate a PDF from HTML using Laravel and PHP?
- How can I use PHP Laravel's ZipArchive library to create a zip file?
- ¿Cómo configurar PHP y Laravel desde cero?
- How do I configure Xdebug in the php.ini file for a Laravel project?
- How can I convert JSON data to XML using PHP Laravel?
- How do I set up a Laravel project with XAMPP on a Windows machine?
- How can I use PHP XLSXWriter with Laravel?
See more codes...