How to seperate base URL and endpoint in laravel http client api call?

Viewed 1672

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


3 Answers

Yes its possible. It has a method called baseUrl()

$http=Http::baseUrl("https://stream.twitter.com/")
             ->post('statuses/2',["status"=>"success"]);

Suppose if you are looking for new Client then configuration looks like below

 $client = new Client([
         'base_uri'        => 'http://www.fake.com/1.0/',
         'timeout'         => 0,
         'proxy'           => '192.168.16.1:10'
    ]);

Maybe you want something like this?

$client = Http::baseUrl("https://stream.twitter.com/");
$response2 = $client->post('statuses/2',["status"=>"success"]);
$response3 = $client->post('statuses/3',["status"=>"success"]);

Guzzle uses guzzle/psr7 to do this. You can too (I think any PSR7 implementation would do not just Guzzle's since PSR7 is a standard):

use GuzzleHttp\Psr7\Uri;

$uri = new Uri('https://stream.twitter.com/');
$uriWithPath = $uri->withPath('statuses/2');

Keep in mind that Uri is an immutable class therefore all mutating operations create a new instance of Uri with the additional property you need.

Related