I'd like to query a MySQL database via a RESTful service with the middleware being PHP. I'd like the output to be JSON. I'm a beginner in those areas. Are there any frameworks or scripts that can do this without requiring you to be an expert? I don't have a problem stringing a few scripts together if they can work.
Or, if it can be done much simpler without using REST (i.e. query parameters), that's fine.
In the end, I want to have an iPhone app fetch this data and have it returned via JSON. No javascript will be involved.
You can convert a MySQL result set to JSON easily: http://phpclasses.nlared.com/browse/package/3195.html
For a RESTful interface, basically any hosted PHP script can function as a REST interface for your application.
Check out this project: http://phprestsql.sourceforge.net/
On this site you will find a RESTful
interface (written in PHP) to a
database (a MySQL database, but that's
not important). Below you will find a
interactive tutorial that will get you
accessing, adding and deleting rows
from our database via our Javascript
powered REST browser.
The PHP shouldn't necessarily be your middleware in your situation, you'll have to build your "RESTful" service somehow, whether it be with PHP or any other language. You don't have to be an expert, but I think CakePHP has some of these capabilities.
Maybe explain in more detail what you want to do.
Update
Try this out:
http://techno-geeks.org/2009/08/easy-json-with-cakephp-and-jquery/
Not only is CakePHP easy to install and use, it's also easy to extend.
Related
I need an online Data Base with anonymous connection that works with Android. I need my app to connect anonymously to a Data Base to retrieve, and send data, I have been searching for tutorials but as far as I've seen the most easy way is using PHP + MySql (I have never used webservices but Im learning) but I haven't seen MySql connection without user login, so does MySql allow anyonymous users connect to my Data Base? And how would it be?
What you probably want to look into is using a REST API implementation to handle this for you. REST uses HTTP to handle requests and responses, and there are lots of options available to you in any programming language your comfortable in.
You mention PHP, so you might look at Slim http://www.slimframework.com/
But there are REST APIs designed specifically for Android like https://github.com/manavo/Android-RESTapi.
Do your research to find one that will best fit your use case. Look on line for tutorials for Android REST or RESTful to MySQL and you will turn up a bunch of info.
You can also look at frameworks like Laravel or Codeiginiter, but they may be overkill for what you want.
Often in PHP you will see something like this in code
class The_data {
function getData($var1,$var2,$var3) {
// Do something with the code here....
}
This can translate into https://example.com/the_data/getData/name/something/something_else
So you can pass data first to the Class, then to the Method or function, then to any number of parameters like the $vars in my example.
But if any of the data is sensitive you must do it securely not over http. Also you need to be sure all the data is sanitized to prevent SQL injection.
I'm quite new to PHP. As a learning project, I'm currently building a website on which users can order products.
I don't want my website to connect to the database I have directly, but I want that process to go through an API I've made. The API has a quite simple structure: the index.php receives the call and the variables (through post) and then, depending on the type of data received, runs one of the functions in one of it's controllers.
So, the question:
How do I set up a connection between my website and my php api (on the same server) to access my database?
I have searched the web and SO for API connections but most of the questions are about the FB API and oAuth etcetera. If I have missed a similar question please inform me because then I'll delete this question.
Any help would be much appreciated, Thank you in advance!
Sounds like you want to implement a REST API. There are a boatload of tutorials and helpful links that you can find very easily to read up on this subject. (Here is a decent starting point). There are also many, many frameworks that you can use that handle RESTful interactions automatically.
EDIT:
Once you have a REST API setup, the best way to connect and interact with your API in PHP is using the cURL module. This is a good intro to the subject of using cURL in PHP.
The current preferred structure for passing data from API -> client is JSON. PHP makes it trivial to work with JSON. Within your API use json_encode to convert a PHP variable into it's JSON equivalent string. Inside your client, convert the JSON response from your API into a PHP object using the inverse function: json_decode
This is a very well known/widely used technique and there are many more nuances to consider, but this should be a sufficient intro for testing purposes. Once you understand the ideas I strongly recommend doing some google/stackoverflow searches and reading more on the subject.
We are developing an extjs app that retrieves data from database via php and mysql. We see that it is possible, however we are curious about other possibility of accessing mysql database with extjs.
In fact, we are not sure whether this old-school way is the way to do it efficiently in extjs.
So are there other methods available for extjs?
It is possible to use ExtJS server side. You'd probably need to utilize Node.js...there are MySQL drivers for Node by the way.
Here is a project that uses ExtJS models on the back end with Node. This might help you get an idea of what is possible.
https://github.com/storminwalker/node-extjs
I have just about got my head around how JavaScript frameworks like Backbone.js and Spine.js deals with the client-side state of a data model in regards to updating views etc. I am looking to build a web application with Backbone that syncs its model with a database. Most of the examples out there use Ruby, but I am currently much more comfortable writing PHP. I have experience with MySQL and MongoDB.
I am looking for a very basic, stripped down version, of a RESTful web application that uses Backbone and syncs with a database through PHP. I have found tutorials like this one and this one, however, even these are too complex for me to learn from.
It would be nice to have a very basic example. For example, a HTML form input, and a list. Enter into the input to add to the list. Of course, this data model will be synced to a database (preferably MySQL). Would anyone be able to provide me with the code for this example? I am comfortable with using a PHP framework like Slim also.
Once I understand how to sync created data to a database, I can then begin to grasp the rest (CRUD). I have seen very few tutorials out there on how to this at a basic level with PHP, so hopefully any support will benefit others too.
Have you seen this one from net.tuts? I think they will post a new tut soon with more details
Edit
And there are at least 2 similar questions here actually:
simple PHP code sample to serve backbone.js
Backbone.js How to use with PHP
I'm developing an iPhone APP and need to implement also an Web Service.
First of all I'm not a Developer and never made something big in PHP, Objective-C, xCode.
My PHP knowledge isn't also good. But let's start with my Environment.
iPhone APP (xCode 4.2, iOS5), PHP Web Service, MySQL DB
I was researching the WEB and most People tend more to REST than SOAP. I think i see also the advantages of REST (using of simple HTTP Verbs (get, post, delete etc...), but that's not the main point here...
I think I understand the main goal of the REST Architecture and tried to make a little concept with an URI and Verb Mapping. Here just a simple example of the mapping:
/location/{location_id}/product
/location/{location_id}/product/{product_id}
Both are GET operations who should get me ether a single product or all products of a location.
How would a simple PHP REST Web Server look like with these functions?
Another part should implement a User Authentication from the iPhone. Somehow i need to store the user session, right now I don't have any idea how to make that. The goald is that if only a user is logged in, he could review the product.
Now I've researched also the Web but couldn't find an easy step-by-step Tutorial.
Do you know any good Tutorials which will help me achieve my goal? :)
A lot of people prefer using PHP Frameworks like ZEND. This seems very interesting, but it seems like a big package with a lot of modules.
Does someone know exactly which Modules are needed to get my Web Service working?
This is quite a good tutorial, it uses the codeigniter framework which makes the learning curve a bit steeper but makes it a lot more powerful in the long run.
http://net.tutsplus.com/tutorials/php/working-with-restful-services-in-codeigniter-2/
If you want to build this the custom way it's actually very easy to do if you want to return information in the JSON format especially for php5 which is generally well supported amongst hosts these days.
Essentially the steps are like this:
Pass in product id via url and retrieve using GET i.e. service.php?product_id=10
Query database and return data for product id that was passed in
Store returned data in an array
Set header content-type to application/json
json_encode the result (json_encode)
That way when you call that url in a browser you will get a nice JSON formatted array result in a key:value pair manner. And as of iOS5 json parser comes with the framework (for earlier versions SBJson is a good framework to use (SB JSON))