-2

I want to create API methods which you can access form another platform. Just see the following diagram. api-data-flow-diagram

In the above image you can see how to fetch data from Yii2 app using another application, I wanna use same procedure when I going to push data into Yii2 application from the another application. That's means Yii2 just provides api methods to clint to push/pull data.

Give me some suggestion?

uglypointer
  • 377
  • 1
  • 14
Supravat Mondal
  • 2,439
  • 1
  • 18
  • 31

2 Answers2

1

Yii2 provides ready to use RESTful Web Service API. http://www.yiiframework.com/doc-2.0/guide-rest-quick-start.html

SiZE
  • 1,966
  • 1
  • 11
  • 21
1

First of all, you will need separate module for API.

If you use Advanced template (with backend, frontend), you'll need to create api directory in root level of your project. If your project is big, and API will changes in future and your API is for many clients, you should make versioning API. Create directory "modules" in api directory, then create directory "v1" in "modules". In future, if you'll need make a huge changes, create second directory, like "v2".

If you use Basic template, just create modules directory, then create API module.

steppefox
  • 1,701
  • 2
  • 14
  • 19
  • steppefox, i have already implemented it using the following tutorial. [Setup RESTful API in Yii2](http://budiirawan.com/setup-restful-api-yii2/), its working but how it call from another application? – Supravat Mondal Aug 13 '15 at 06:46
  • Simply. API it's just a list of urls. For example, if you have RESTful API with Countries controller, you can send request to: http://api.yoursite.com/v1/countries to get list of countries. Response of API usually send in JSON or XML format. Of course your web server must properly resolve url `api.yoursite.com`. – steppefox Aug 13 '15 at 07:39
  • Let me know how to post data when I create a country?? send me some sample code. – Supravat Mondal Aug 13 '15 at 07:46
  • Sorry, i can't give you code example. You need just to send POST request via CURL (or whatever) to api.yoursite.com/v1/countries, that's all – steppefox Aug 13 '15 at 07:54
  • Client don't use `CURL` for api call, just client will call api method with his data array. then it will push in my yii2 application. – Supravat Mondal Aug 13 '15 at 08:07
  • 1
    RESTful API doesn't works like that. You MUST send REQUESTS to specific urls. With CURL or whatever library. If you want just call methods of remote API, you mean SOAP, but it's not RESTful API, it's SOAP. For example, if your client is a PHP web-site, you can use Guzzle package to make requests. – steppefox Aug 13 '15 at 08:10
  • steppefox, you are right. I appreciate for your helpless. – Supravat Mondal Aug 13 '15 at 09:33