I have a JSON based REST API implemented using PHP that will be called from my mobile app. I have a website deployed on the same server where the REST API existed.
What is the best way to use the existing functionality, is it by sending a cURL request to my API or just by including actual model classes source code and calling those functions?
If they're logically part of the same application, you might consider implementing the functionality of the REST API as a helper function. Then, you can include that function (and whatever model classes it needs) and get the code re-use without the overhead of creating another request.
Related
I am currently working on a API Wrapper for an internal API. I have found a lot of answers when searching for how to test an API, but not an wrapper.
The problem is, that the API (of course) need authentication and is an internal API developed by another team.
The tests should be independent from the API itself and not really call teh API.
The tests need to work without a connection to the internal API, but I know, what valid requests from the class would look like, given a certain input.
My first thought was a solution, where I call an function of the API wrapper class and checking if the (curl) requests the class makes are equal to a predefined value. E.g. checking the JSON body of one or more POST requests is the same as I would expect.
How would you recommend implementing such a test?
Thanks a lot in advance for any help :)
I would like to design few different REST API, eg:
mobile.api.com - for mobile
client.api.com - for client side
admin.api.com - for admin side
However, these API might need to separate out to different server and using the reuse some code logic, since it is a same app, but process the output according different platforms.
My question is, how to make these different API to use the same code logic without duplicate the code if possible?
So far the solution I think of (hmm, not so suitable) is create a common API (where the same code logic sits here), each API will curl to this API (assume all server in same network)
EG:
if request a user details (where it is a common for each these API), mobile.api.com or admin.api.com will curl to common.api.com to get the user details, like common.api.com/users/userA , then process the output.
I hope there is a better architecture or solutions for this.
Thanks
I think just get one framework for example Yii
just creating module applications
all basic login will be in default module and we will have main models controller and others and modules will extends this classes
and in .htaccess file we will write some redirections
it just my openion. it have other variants also . . .
I'm asking this question because some of the websites is visited seems to be using a RESTful API to access the data even if it's on the website...
For example: my website will have 6 pages and 5 of them use the DB. But, I will also have a REST api for my partners...
So, the question is:
On my website, is it better to access directly the DB via mysqli_query or to use a RESTful API with Ajax calls to load data?
Just a note: I'll be using Zend Framework 2 for my RESTful API except if someone has a better option... I know Node.js and PHP... I'm able to write it in Ruby or something if it's better for me... Need a opinion on that...
Use the RESTful API.
The specification of REST is that we use the HTTP methods, which he calls verbs.(GET, POST, PUT, DELETE).
A direct request would be limiting it, or you would be using at most two method (verbs) - GET and POST.
For that you have to do this:
GET /user/frederick/edit
GET /user/frederick/update
GET /user/frederick/delete
GET /user/new
And with a RESTful API:
GET /user/frederick/
POST /user/new/
PUT /user/frederick/
DELETE /user/frederick/
The advantage of using your own API is that you don't have to write duplicate code. For example, you might have generate_for_rest and generate_for_server functions that do the same thing and just emit data in different formats. It's a good idea to reuse your own APIs as much as you can.
That said, I do find it a bit unusual that a website would communicate to itself with its own RESTful API. That requires an HTTP request (though it should be extremely fast) and conversion of the data twice. Instead it would make more sense to have an API that generates the data that you need and a facade that converts that data into formats for it to be used.
For example you could have a function get_all_users. Internally you can use get_all_users to get the results as php data structures that you can use immediately. In your controller that responds to HTTP requests you may do a JSON conversion, but you shouldn't be doing any duplicate work to get the data for either internal or external use.
I am creating an applicaton in PHP with MVC based framework. I am exposing a web API for the mobile apps.
The same mobile interface is to be created in web application also. So:
For creating the web interface, can I use the API created for mobile app with HTTP request?
OR
Model methods are exposed, so can we use the model instead of calling and connecting with the API in same server?
What is the best practice?
It depends , if it is your internal product, and you are the only user of API, it is better to expose function base API. eg: addUser, deleteUser.
If you want some one else to use the API and create some application (with different logic depending on their own need) give them model based access. As far as I know functional API's are generally created and exposed. Because this kinds of API saves the number of webservice call and hides the logic underneath it.
edit
if you mean calling the webservice internally ... Ideally design should be such that ..their should be a delegate method that consists of core logic, then that core logic function can be called from your api as well as controller. Calling webservice internally will be a unnecessasary performance overhead. Though you can do that in small size application, but is not at all good solution.
If your model layer holds business logic, though It should be Ideally a seperate layer ...then you should consider using model.
This is a Homework task. It involves creating a simple DB and making CURL calls to the server to get results from the DB, accordingly I have a DBClass file with the required methods. I understand what REST architecture is in general, but I am kind of unable to put the pieces together. Here's what I have so far:
Model.class.php -> this is the Database class that instantiates connections to the DB and has methods that execute DB queries and return the result.
Simulator.php -> helper class, simulates HTTP requests (POST or GET only) to the localhost so my curl call is made to 'http://localhost/app/index.php'
index.php -> here is where I receive the CURL requests, in effect, I decode the HTTP requests to understand the request method, parameters, URI as such.
At this point, I am lost. Understandably a RESTful API request essentially is of the kind server/getMeMyBananas and getMeMyBananas is a DB method that looks for bananas for the user and returns the ID's. I am confused how this maps into the index.php file and the missing pieces.
Suggestions and links to awesome resources are most welcome. I am not interested in security or creating a state of the art Web service.
getMeMyBananas is an example of an RPC route
In REST, the four main HTTP verbs GET, POST, PUT and DELETE are the verbs to act upon a noun (a resource).
REST is not a standard. It's a loose recommendation on how to form an API for a remote system using HTTP as its foundation.
There's nothing to say that you can't design RPC-like routes in a REST API. People do it all of the time. It's just that you mainly use the verbs to create (POST), retrieve (GET), update (PUT) or delete (DELETE). That makes the acronym CRUD. So, with REST, you are able to cover a lot of scenarios in information exchange simply by sticking to CRUD.
So, you can start by designing your URLs (routes) to resemble nouns (resources) and build a switch case in PHP to switch on the HTTP verb. Keep in mind, there's nothing stopping from and there's nothing wrong with having RPC-like routes. In fact, you can't handle all cases with the simple REST CRUD scenario, so you'll have to handle cases that don't fit into that scenario with RPC-like routes.
See:
http://dojotoolkit.org/reference-guide/1.8/quickstart/rest.html
Later, if you are interested in a built out API in PHP, I built an API infrastructure and open sourced it. I'm not sure if it'll help you, but here it is:
https://github.com/homer6/blank_altumo
You can map any url to any path you want! For example, when using Apache you can use ModRewrite to turn http://ex.com/rest/bananas into http://ex.com/index.php?p1=rest&p2=bananas
From there you can now you can fetch your request parameters with the global variable get for example: $_GET["p1"].
I would suggest you to perform isset() test on those.
After that when you've got the data, I'd suggest to package it in JSON so almost any client can read it.
That's basically how I'd do it! If you've got more questions go ahead :)