What is an API?

api Sometimes as developers, we have things we need to do that we wish someone else could do for us. Chances are someone might have already done it for you. When you start programming you learn to do stuff on your own. Later, you learn of libraries and tools you can use from other developers. Sometimes, you might want to access another developer’s data, tools, or resources. Good examples of these things might be; getting Facebook friends, adding something to a calendar, or processing some data.

These types of activities might be out of the question, or hard to achieve on your own. However, many developers have APIs or Application Programming Interface that you can use. These might be free, or cost a little bit of money. The great thing about APIs is that they are EVERYWHERE. I am going to start off basic here and go into more depth in future posts. I just want to open your eyes to the opportunities.

The most common APIs are using a method called ReSTful or Representational State Transfer. RESTful APIs uses standard HTTP requests such as GET and POST. Modern RESTful APIs use PUT, DELETE, and PATCH also. PHP requires a little bit extra work for these extra types, but not much.

Now, how are you suppose to have your web server contact another web server? In PHP you are going to use CURL or file_get_contents(). CURL is a set of functions to make a CURL request to the API. CURL looks like this.

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $request_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
$result = curl_exec($ch);


This is a quick example, and CURL has a lot of options you can set. Here is a link to the official PHP documents for CURL (PHP.net CURL). In later posts, I will go over CURL in more detail.

Leave a Reply

Your email address will not be published. Required fields are marked *