Like in guzzle, I want to seperate base URL and end point
use Guzzle\Http\Client;
use Guzzle\Stream\PhpStreamRequestFactory;
$client = new Client('https://stream.twitter.com/');//base url
$request = $client->post('statuses/2',123);//end point
Yes, it's possible. It has a method called baseUrl()
.
Laravel provides an expressive, minimal API around the Guzzle HTTP client, allowing you to quickly make outgoing HTTP requests to communicate with other web applications.
$response = Http::baseUrl("https://reqres.in/api")->get('users');
Another way is to create macros .Define the macro within the boot method of your application's App\Providers\AppServiceProvider class:
<?php
namespace App\Providers;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Http::macro('reqres', function () {
return Http::baseUrl('https://reqres.in/api');
});
}
}
and now you can reuse this method like below.
$response = Http::reqres()->get('users');
For post method
$response = Http::reqres()->post('users',[
"name"=> "morpheus",
"job"=> "leader"
]);
There are lots of built-in methods, which you can refer to here HTTP Client
Dont forget to import facades
use Illuminate\Support\Facades\Http;
If you want to use Guzzle then
$client = new GuzzleHttp\Client(['base_uri' => 'https://reqres.in/api/']);
$response = $client->request('GET', 'users');