1

I'm currently creating an API for my website with Lumen (http://api.example.com). The API handles CRUD: like changing/reading user data, updating articles, adding comments, user auth and so on.

The API is necessary because I'm calling it from my mobile app. But I want my web application (http://www.example.com), based on Laravel, to communicate with the API too. That way I don't have to write the same code for the API and the webapp. But how can I do an internal request from the webapp to the API?

I'm aware of the Dingo package but that works on a single domain only. I can use Dingo on my Laravel framework, but I want to create the API with Lumen...

Jordy
  • 4,719
  • 11
  • 47
  • 81

2 Answers2

0

Check this, it allow you to make call to another URL (your API) in PHP with everything you need, and it's well documented.

jeanj
  • 2,106
  • 13
  • 22
  • Is it a recommended approach to use Guzzle to interact with your own API? – Jordy Jul 22 '16 at 14:59
  • Owned, or public API, yes it's a common way to do in PHP. – jeanj Jul 22 '16 at 15:01
  • Thank you. One question about that: I don't see anyone around the web using that. They all use Guzzle for access to third party API's only. Is there a better way to interact with your own API? Am I overlooking something? I'm very sure a lot of other websites should have the same problem as I have.. – Jordy Jul 22 '16 at 15:20
  • What's the difference between a third party API and yours? – jeanj Jul 22 '16 at 15:21
  • A third party API is something like the Google API that is authenticating users. Or the Github api. My own API is handling my own code, the CRUD actions, like every website has. But because I want to do that from the mobile app too, it should be accessible as an API. So for example: adding a comment to a news-item should be done by the same code for the webapp and the mobile app. Hope you understand. – Jordy Jul 22 '16 at 15:24
  • Yes like third party API, yours is about managing datas, a lot of API have CRUD actions. I don't understand what can be the problem since anyway your API will be public and availiable – jeanj Jul 22 '16 at 15:26
  • Yeah, but perhaps the logic behind it is wrong and there should be a better solution. I don't know ;) (because the web app is always sending one more http request to that api now when using Guzzle) – Jordy Jul 22 '16 at 15:29
  • The way you should do this is with a JS framework like angular or vuejs to handle the api since you don't need laravel anymore because you already have your backend – jeanj Jul 22 '16 at 15:31
  • I think you don't understand it completely :) My webapp is a Laravel website. I have a mobile app too. When a user is adding a comment, a write query should be fired. That can be one on the web app, but when you do that on mobile the request is handled by the api. Now: that means I have two places with the same code. One is the webapp, one is the api. But I want in 1 place, so the webapp should communicate with the API. But when I use Guzzle, every time a new http request is fired to the api.. – Jordy Jul 22 '16 at 15:37
  • I understand it completely, when you have an api with all the logic there is no point to have another php since there is no logic to do anymore. So you have two ways continue with your laravel webapp but query the api with whatever can do a HTTP request or rebuild your webapp with a JS framework that will just handle the front and call your api as backend, then you will only have your api to maintain as backend. – jeanj Jul 22 '16 at 15:42
  • Ahh, I understand. Thank you! :) – Jordy Jul 22 '16 at 15:51
  • So the answer to my original question is: it is a wrong approach. I could better make a JS framework that interacts with my API. And use Guzzle only if I make a Laravel backend that interacts with a third party API? – Jordy Jul 22 '16 at 15:54
  • That's the way I did this. Guzzle will only be useful if in your api you need to interact with other API – jeanj Jul 22 '16 at 15:55
0

Using APIfy to generate your API

PhuLuong
  • 527
  • 4
  • 11