Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5 REST client CRUD

Is there a way in Laravel 5 to do a CRUD using REST? I have a REST API already using CodeIgniter and I want my Laravel application to communicate with it.

So let's say I have this url to get all gender: http://api.local/api/api_gender/gender

In my controller, it seems I can do something like this:

$results = json_decode(file_get_contents('http://api.local/api/api_gender/gender/'));

But I dont know if this is the right way to do it.

Now, how could I do it if I want to add a new gender in Laravel? In CI, I could simply use Phil's rest library and just use $this->rest->put('http://api.local/api/api_gender/gender/', $parameters)

like image 509
basagabi Avatar asked Mar 01 '15 12:03

basagabi


1 Answers

I would use Guzzle, a very popular, flexible HTTP client for PHP. As far as I know, it is one of the most used packages to make HTTP requests in PHP.

$client = new GuzzleHttp\Client();

$client->put('http://api.local/api/api_gender/gender/', ['json' => ['foo' => 'bar']]);
// or
$client->put('http://api.local/api/api_gender/gender/', ['query' => ['foo' => 'bar']]);
like image 177
Jeroen Noten Avatar answered Oct 13 '22 21:10

Jeroen Noten