When communicating between a Node/React web server and a PHP api server is it best practice to "proxy" all requests by the client (React) through the web server and not directly to the api server?
Im using universal (isomorphic) React rendered on the Node web server. It would also use the api server to build the initial page loads (isomorphic).
That's what I would do. Actually that's what i'm doing (with a java based backend/api). It allows you to hide the real structure of your api and also to eventually parse the result in the node server if needed. Also it allows you to adapt the request method to fit exactly your client services (if for example your api is not exactly sending the data the format and the way you want).
For me it seems to be a good practice (and I agree trying to close this question is crazy, this is a good question ...)
Related
I'm having troubles to figure out how web services handle requests and send responses, and how my site/module will handle it. So these are my questions and bellow a little about why i am asking this:
What is the right way to send a request to web service?
How a web-service will usually answer a site requesting?
How a PHP site can send a request to a web-service? Is cURL the right way?
I'm a student who are learning PHP and a lot of other things, and my job now is create a Joomla Module to show information from a web service (probably created in java or something, probably created by me, when i learn java...).
I know i will use http requests to talk with the web service, but i worry im using it wrong (making a request to the url, like the browser).
So, i did a little example site and a example API. The api uses Slim microframework to create routes, access my database and return the content in json (what means: if i access 'api.com/api/something' in my browser i see a plain white page with a lot of json). I pretend this is my web service.
The example site send a request to the API with cURL, reads the content and decode the json and do things. It seems forced to me.
I have not much support to understand web services and if i am in the right way (or far from this).
I would appreciate your help.
You are on the right track. A web service is simply a processing or storage facility intended to be accessed by some other program just like a database or fileserver service.
What is the right way to send a request to a web service
It depends. The most common implementations use SOAP or REST which define additional semantics on top of the HTTP protocol. SOAP uses a single URL as a gateway to the service and more specific selection of the functionality and the relevant data is embedded within an XML payload presented via POST. HTTP is merely a bearer for the message exchange. In REST, the HTTP part is integrated into the semantics of the transaction.
The URL identifies the location of the data (or processing function)
The payload contains only data, usually presented as POSTed JSON or XML,
further REST uses the HTTP verb (GET, POST, PUT, DELETE) to indicate the requested action
the HTTP headers are used to convey transaction meta-data.
How a web service will usually answer a request
I'm not sure what you are asking here. It should report back on the state of the request, any relevant error messages and possibly some data.
The speciifics would be unique to the API and documented.
Is cURL the right way?
While it is possible to do a lot with the HTTP wrappers functionality in PHP, libcurl offers an lot more flexibility. So, yes this it would be hard to implement a REST client without using cURL, OTOH a SOAP client is (usually) less complex at the HTTP tier but life is a lot simpler if you use a SOAP library/framework to abstract the more complex protocol.
For future questions please have one question per entry.
What is the right way to send a request to web service?
That really depends on the web service, what they require. It can be as simple as a short text string, to sending a XML formatted or JSON formatted array. You need to research the relevant web service.
How a web-service will usually answer a site requesting?
Again it depends on the web service. They are not the same. A web service will have documentation on what it expects and how it will respond.
How a PHP site can send a request to a web-service? Is cURL the right way?
Curl is a good method and is usually the method used from within PHP.
This probably seems stupid question. But I'm new to ionic apps. I wanted to clear this before development.
Can we build ionic app without API? like we normally do for our website.
Example:
If you go to MySite.com, you will see the site running in php with normal rendering. Would Ionic work same way or it has to go through API Send/Receive request for data handling?
Ionic has no direct requirement on data access. It is built on AngularJS, which is in turn built on Javascript. Best practices would generally have you accessing JSON data from a server by using a mechanism that is Angular aware such as $http, $resource, Restangular, etc.
However, you can execute any javascript based browser command (or even load something like jQuery to perform the data access). However, as soon as you go outside of Angular, then you will have to deal with the additional complexity of making sure the digest cycle is run whenever you have updated values that may be reflected on your view because of data binding.
And, just to be clear, none of this has anything to do with what's happening on the server side. When you are asking about accessing "without API", do you mean accessing HTML files vs. JSON data? Ionic is built to be a SPA (single page application) that is installed on the mobile device and doesn't require internet access to run once it's installed.
Therefore, especially if external/live data isn't required (imagine some type of calculator where you enter values and results can be calculated with just the data in the app on the handset -- without the need for a live server at all), Ionic apps don't REQUIRE access to an external server at all.
You could provide traditional hyperlinks to other html files, but at that point it would no longer be running the packaged/installed files that form the basis of the installed app on the handset and would instead be a web app that is relying on an external server for all views. And, of course, even if it does require access to data from a server, the often massive increase in speed by not round tripping the server with a new HTML page (only a relatively tiny JSON payload in most situations) makes it feel much more like a true native app.
In ionic, the rendering happens on your phone. The server simply provides data. So if you need any data from a server (usually yes) then you need an API.
Ionic is focused on building native/hybrid mobile apps rather than
mobile websites.
– http://ionicframework.com/docs/overview/#browser-support
As per definition, an API only defines the way your ionic app can interact with your php script. In principle, there are no rules on how you design this interaction. So yes, you can keep it quite traditional/old-fashioned. Don't get the concept of an API mixed up with the concepts of RESTful or SOAP APIs etc.
I've developed an android app that interact with my database by using some php scripts (one for each function of my app) that returns a json object with response data.
Now i need to build up a website too that do the same tasks of my app, but i would fix up my server code.
Should i have to maintain my app php scripts separate from website scripts (i'm planning to use some php framework to develop it), or there's a different way to do it?
No! Same script will work for all platforms.
If you follow proper protocols you will be good :)
Use Rest Console or similar tools to test your webservice on browser.
If you are able to get JSON response, then its good for all platform.
If you want to separate out the platforms and devices on server that can be handled by using user agent check at server end.
I want to code an Android client and a Windows.exe server application (possibly PHP., I am still trying to decide).
I have no problem with developing the server app, but have not yet done any Android coding. Mostly, though, I am unsure about how to communicate between client and server.
A client aapp will login to the server then at regular intervals send its location (GPS coords) to the server which will store the data in a MySql database,
A second Android app will display a historical trail of where the first user has been using Google maps, plus a little more functionality.
Since I am not serving HTML, I am wondering whether to use HTTP GET / POST or a proprietary protocol over TCP/IP. I would like to encrypt it, so SSL seems in order,
Is there any compelling reason to use one or the other of use HTTP GET / POST or a proprietary protocol over TCP/IP?
Would coding my Android app be easier if I used JSON as my data format (or something else?), irrespective of the protocl used to transfer the data?
Hmmm, since much of the data returned as a response to GET by one of the apps will be data used to draw a Google map with a series of points showing travel, could I do the heavy duty processing on the server & return the HTML (or JS) necessary to display it an dhave the app embedd a browser in its UI to display the map? (the UI will also disply more, but maybe I shoudl just make it browser based HTML & JS, rather thn an actual Jav Android app? As you can see, I am confused)
[Update] I want to code both the clients and the server and to host the MySql database. I would prefer no 3rd party frameworks unless they are excedding simply to use and play very well together with Delphi or PHP.
I may recommend you to use Wcf with poco entity that provide you security as you want and create client in android to consume it.here is simple example of using wcf in android-http://fszlin.dymetis.com/post/2010/05/10/Comsuming-WCF-Services-With-Android.aspx
I would strongly recommend the use of the newest Google Play Services with the Google Cloud Messaging... It takes out of you all that work.
Check out, see if you like ;D
Google Cloud Message: http://developer.android.com/google/gcm/index.html
There is also a great video of this year's Google I/O about the maps improvement on Google Play Services:
https://developers.google.com/events/io/sessions/325172829
Are you trying to create App that can be delivered from Google Play Store or a Andriod enable Web application? You Question is confusing in your need. If you are looking to create Andriod Apps then definitely PHP is not going to serve it up. Look for Andriod SDK and create your interface using that, then for Windows Server EXE you can do PHP based API or as suggested by other answers. But for User interface PHP is no. You need to read and understand the different between APP and Web App. or your question is not very clear on topic.
I have a mysql database with informations about users. Now I want to work with them in my iPhone app. Is there a way to bring the stuff from mysql to objective-c?
It's not an option to use the mysql c api because my web server just allows php.
Greekings, Valle.
Your question is not very clear but maybe this can help:
You can serve the information from your database with PHP (some XML or JSON can be useful) and then parse it on your iPhone App.
Or you can export your database and convert it to SQLite and use it directly on your iPhone.
With a bit more information maybe we can give you a better answer.
It sounds like you want to create an Application Programming Interface (API), in PHP on the web server that your Objective-C program will talk to using the devices internet connection.
Effectively you want to create special url's and request parameters your web server will use to pull database information, parse them into JSON or XML, and return them as plain text.
Inside your Objective-C application you will craft these URL's and parse the incoming JSON or XML into actionable data for your app.
Well, I don't think you explained your problem very well. Shared web hosting solutions usually don't offer access to MySQL servers from outside, it means you can access only from their servers (like PHP scripts on your hosting).
Solution to this is usually simple web service - REST, XML-RPC, SOAP.
Web service is some application that can be written in PHP and is running on your web hosting. This application returns data (usually in XML or JSON) via specific URL.
Example:
From your native iPhone you call
http://yourserver.com/webservice/?what=episode-list&tvshow=battlestar-galactica
and output will be XML with list of episodes.