connect to php API from php website - php

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.

Related

Data Base with anonymous users

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.

RESTful Api or direct access?

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.

PHP RESTful Web Service for an iPhone

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))

Should I always use webservice?

HI all,
I am designing an client - server application system. The Server provide 2 features: upload and download images. The clients (maybe Iphone, Android, WP7, ...) use these features to provide to end-users.
At this time, I can design the server as web-pages (such as upload.php, download.php) as well as webservices. Both ways work fine for me.
My question is: which one I use webservices or web-pages technical for my designing? And why?
Thanks.
To be honest with you, using something like SOAP for this seems like a lot of unnecessary effort. You can very easily accomplish this using form encoded data and the right query string parameters without going through the trouble of building SOAP envelopes and writing all of the XML. Granted, the system I described is very close to a REST webservice, so I suppose it depends on what you mean by "webservice." Either way though, given relatively simple system you've outlined, I don't think something like SOAP will give you a very good return on your investment.

Does a MySQL, PHP, JSON framework exist?

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.

Categories