1

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!

Joris416
  • 4,751
  • 5
  • 32
  • 59
  • You should have **TWO** APIs. One designed for remote usage via HTTP calls, and one for local use as PHP function calls and whatnot. Forcing your own server's code to do a full-blown http request TO ITSELF is a hideous waste of resources and highly inefficient. – Marc B Jan 08 '14 at 20:34

1 Answers1

3

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.

Community
  • 1
  • 1
berrberr
  • 1,914
  • 11
  • 16
  • Hi @berrbz, thank you for the reaction! But I think there is a misunderstanding. I understand generally how php and REST Work (I am also an iPhone developer and have iphone applications working with REST servers). But doing t in a website with PHP code is new to me. All I am asking is a line/piece of code that demonstrates how to send some variables to a REST API through POST from a php website. would you be so kind to provide me with that? – Joris416 Jan 08 '14 at 20:54
  • I see, the way to do that is using cURL. Read [here](http://stackoverflow.com/questions/2138527/php-curl-http-post-sample-code) for a basic example. Make sure that you have the cURL module enabled in PHP as well! – berrberr Jan 08 '14 at 21:12
  • Thank you! I spent today trying to achieve this by curl. I can connect to my API, which is good! There is one problem: My API is making an array to return to my website, but my website receives it as a simple sting. I made the connection like your example. Now after doing some research, I have also seen that people mention 'sessions' to do the api communication. Would this be better? Or is there a way to solve the problem in curl? Thank you in advance! – Joris416 Jan 09 '14 at 21:35
  • The most popular way (and probably best) is for your API to output JSON (which is, really, just a string). You can then read in that JSON data using cURL in PHP and use [json_decode](http://ca1.php.net/json_decode) to turn the string data into a PHP object. To output your array data from within your API simply use the inverse function, [json_encode](http://ca1.php.net/manual/en/function.json-encode.php) – berrberr Jan 10 '14 at 03:02
  • JSON did the trick! can you edit your answer, than I'll up vote and accept is as solved – Joris416 Jan 10 '14 at 12:53
  • I edited to answer to include the important points from our discussion. I think the answer is a decent starting point incase anyone stumbles on this page from google/SO search. Please feel free to suggest some edits if you see anything :) – berrberr Jan 10 '14 at 14:49