Huligan
Huligan

Reputation: 429

What to use for HTTP requests in Laravel?

I need to do HTTP request in controller Laravel for getting data by URL. The remote URL returns JSON data format. What to use for HTTP requests in Laravel except standart PHP Curl?

Upvotes: 1

Views: 849

Answers (1)

Space
Space

Reputation: 2042

Guzzle is a popular cross-framework for making HTTP calls to external services. Laravel already has this included as a dependency for its mail integration services (Sparkpost, Mailgun, Mandrill).

Edit composer.json and in the require section add "guzzlehttp/guzzle": "~6" after the laravel/framework line.

composer update

At the top of your controller add use GuzzleHttp\Client;

Then within a method you can use it like this:

$client = new \GuzzleHttp\Client();
$res = $client->request('GET', 'https://api.github.com/user', [
    'auth' => ['user', 'pass']
]);
echo $res->getStatusCode();
// 200
echo $res->getHeaderLine('content-type');
// 'application/json; charset=utf8'
echo $res->getBody();
// {"type":"User"...'

There is also a a Laracast on using Guzzle with Laravel.

Upvotes: 1

Related Questions