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 Stack Overflow for API connections but most of the questions are about the FaceBook 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.

1
  • 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. Commented Jan 8, 2014 at 20:34

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

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?
I see, the way to do that is using cURL. Read here for a basic example. Make sure that you have the cURL module enabled in PHP as well!
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!
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 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
JSON did the trick! can you edit your answer, than I'll up vote and accept is as solved
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.