0

What I want and have

I'm willing to create an API, I already know what does an API stands for. I use PHP as a server side language.

However I don't really understand how an API works from scratch, and how can I enable users to use my API after I created it.

What I think and know

Well right now I think that with an API I enable developers to reach my database and request queries. But then why should I create a full language for it? Why can't they use PHP or any other server side language?

As I saw, companies who crates API's for their site, they require a registration to give to developers an API KEY. Does the developers log in to database trough this API KEY? And if yes, then why they don't have a password too? If I know somebody's API KEY I can use it or what?

And as I read/heard XML is related somehow to API's, but why and how?

Also please consider in your answers that I don't want to use any frameworks or something like that, I want to make it in scratch so I want to understand how it works from A to Z.

Adam Halasz
  • 57,421
  • 66
  • 149
  • 213
  • 2
    You should remove the `API` code styling, as that makes it look like a buzzword in your question. Furthermore you are actually asking about "Webservice RPC" interfaces. Also, try to enumerate your multiple questions; might yield better explanations. – mario Nov 27 '10 at 17:00

3 Answers3

1

An API is basically any interface which allows other programmers to access your program. You probably want to sit down and think about what you want other people to be able to do. You generally don't want to give other devs pure acces to your database for several (hopefully obvious) reasons.

API keys are generally used to control access to the API, these can be sent along with method calls to make sure only people who you want to give access, have access. They are basically passwords. Reading the wikipedia entry may make things clearer.

Jim
  • 22,354
  • 6
  • 52
  • 80
1

An API is really just methods that you publicly expose. Say, for example, you have a blog site. You want your members to be able to post a blog entry without actually coming to your site - logging in, filling out a form, etc.. Perhaps they want to create an Android/IPhone app to post to your cool blog site. You can allow them to do that.

Create an API for this:

    <?php
        function createBlogEntry(memberEmailAddress, memberPassWord, BlogTitle, BlogBody)
        {

           //Here you would usually call an internal (or not publicly exposed) method that
           //contains the actual logic to insert. Like Jim's answer states, you just want 
           //people to be able to performan action in your system, you don't want them to see HOW you do it.

          encapsulatedBlogCreation(memberEmailAddress, memberPassWord, BlogTitle, BlogBody);  


           //This is where API's get tricky. I need to return to the user the result of
           // the save so that my user knows what happens. In my API documentation, I 
           //tellthe user 0 = Created Successfully. If this is a READ operation
           //just return the result. Most modern programming languages also have
           //a way to capture an HTTP response - so dealing with the return is their responsibility

            return 0;
        }
 ?>

A couple things:

  • In my API, I didn't need a developer Key, I allowed the user to pass in their login credentials. An API key does the same thing, it uniquely identifies a request and links it to a user in the system.
  • This API doesn't use XML. This is about as simple as an api gets. This can be called by sending an HTTP request which can be done with any modern programming language. Say I put the above code in a file called blog.php and put it in the root of my blogsite. I would call it like:

*http://myblogsite.com/blog.php?memberEmailAddress=myEmal@example.com&memberPassWord=myPass&BlogTitle=Title-of-post&BlogBody=the_blog_post_is_here*

One thing you need to understand, is that there has to be a need for an API. You can expose a method to create a blog entry, but if no one knows about it or uses it - it's worthless. API's need to be documented, advertised, and provide a benefit to potential users.

tpow
  • 7,600
  • 11
  • 59
  • 84
  • Thanks for your answer, Ok I can create functions what people can use, but how can they use it if they don't have access to my database? Should I give them my database connection details(username, password)? And how can they use my function in PHP if they don't have it in their own machine? Do I need to give them a file where there are the functions etc, like on YouTube API? – Adam Halasz Nov 27 '10 at 17:25
  • Wow man this is exactly what I would like to know even if I didn't know what I want xD, the LINK omg, it's soo logical in this way ... :D thanks man! – Adam Halasz Nov 27 '10 at 17:31
  • @CIRK - I updated the answer. Basically, you make them pass in everything you need to perform an action. They don't need your connection details (and I hope they don't have them), because you're going to pass what they've given you into your program - your program knows how to connect to the database. The actual DB connection, DB insert is happening inside "encapsulatedBlogCreation()". This method is in another file that is NOT publicly exposed - and contains the database connection strings, and logic to insert the blog entry (i.e. SQL Query). – tpow Nov 27 '10 at 17:35
  • @cinqoTimo thanks very much! One more question, when the user is requesting something from the database and not inserting or deleting, how should I give them the data results? I think here comes the XML, however anything should be good, but how should I give the information from the page back to the requester? – Adam Halasz Nov 27 '10 at 17:38
  • @CIRK - Add a return statement (updated above). If you were returning a blog entry instead of creating it, it would look the same way. This type of web service is returning primitive data - just a integer as a code to let the user know what happened. If you want your API to return an entire blog entry (BlogId, BlogTitle, BlogDate, BlogBody) you need structured data. This is why you here of XML because it is a standard way to return structured data. – tpow Nov 27 '10 at 17:55
  • @cinqoTimo But I don't understand how should I return it? – Adam Halasz Nov 27 '10 at 18:10
  • So this is how I think right now, I'm going to Example Site, I download a file what contains the API, then I can use a function like, get(link), then the PHP goes to the link copies everything from the page and saves to my page? I'm right? – Adam Halasz Nov 27 '10 at 18:14
  • @CIRK - You return it like "return $myAnswer;". I updated my post. See at the end of the function, there is a return - this will return the results of the function to the user of the API. If you have further questions, articulate it and repost another question on stackoverflow – tpow Nov 27 '10 at 18:29
1

API stand for Application Programming Interface, and we can think on it as a set of libraries composed for functions that you can rehuse over and over. For example the WIN32 API is composed for the set of functions (developed in C/C++) for the applications to interact with the Operative System, this API include libraries as: kernel32.dll, advapi32.dll, gdi32.dll, user32.dll, comctl32.dll, shell32.dll.

There are some other terms you should know such as toolkit and framework to well determine what you want to do. Normally APIs are written in C/C++ and in the most of the cases rehuse the functions provided by the Operative System API.

Community
  • 1
  • 1
ArBR
  • 4,032
  • 2
  • 23
  • 29
  • thanks very much for you answer, well I mean API's for web :) but good to know that OS's works just like the web. – Adam Halasz Nov 27 '10 at 17:39
  • This answer only applies to a small subset of API's that reflect his/her experience. – tpow Nov 27 '10 at 18:04
  • @cinqoTimo: I was talking about OS APIS, wich include web server side APIS also. Particularly creating an API for interchanging information using PHP in the server side is related to create WebServices, XML-RPS or SOAP based services. PHP provides access to both through the xmlrpc extension, which is based on the xmlrpc-epi project [http://xmlrpc-epi.sourceforge.net] wich is written in C. – ArBR Nov 27 '10 at 22:38