Mobile app version of vmapp.org
Login or Join
Courtney195

: How do I use an API? I need to use an API. I want to do the programming in PHP, but I have no idea where to start. I contacted the creator of the API asking how should I use API's

@Courtney195

Posted in: #Api

I need to use an API. I want to do the programming in PHP, but I have no idea where to start. I contacted the creator of the API asking


how should I use API's in general?


and he sent a link to the GitHub repository of someone who made a 'wrapper' for the API. What's a wrapper? And how do I use the API in PHP?

A Google search for api php tutorial only gives results for how to create an API, not how to use them. I have no idea where to start, can someone help me?

10.03% popularity Vote Up Vote Down


Login to follow query

More posts by @Courtney195

3 Comments

Sorted by latest first Latest Oldest Best

 

@Gloria169

As stated above, API stands for Application Programming Interface. In layman's terms, it's basically a service that you can access. In most cases, it is a service that you can access whenever you are looking for information that is not on-hand.

For example, if I want information about weather forecasts for a specific area, I can access one of the various weather APIs. If I want information about local bus times, I can check to see if the bus company has an API that I can use. If I want information about a specific Youtube video, I can use the Youtube API.

As you can see, the possibilities are endless and each API can be accessed in a completely different way. The URL structure, the request method (GET, POST, PUT) and the result format (XML, JSON) can differ from one API to the next.

In most cases, an API key is a string of characters that gives you access to the API. It also allows the API service to identify you (i.e. usage limits / billing).

A wrapper class (in regards to an API) is basically just a class that makes it easier to access a specific API. For example:

class Weather{

const APIKEY = 'mykey';

public static function forecast($location){
$location = urlencode($location);
$url = "http://api.com/forecast?loc=$location&key=" . Weather::APIKEY;
$contents = json_decode(file_get_contents($url));
return $contents;
}

public static function current($location){
$location = urlencode($location);
$url = "http://api.com/now?loc=$location&key=" . Weather::APIKEY;
$contents = json_decode(file_get_contents($url));
return $contents;
}

}


The example wrapper class above would allow me to easily access the weather API in question, like so:

$forecastDublin = Weather::forecast('Dublin, Ireland');
$forecastLondon = Weather::forecast('London, England');
$currentWeatherLA = Weather::current('Los Angeles, USA');


i.e. I do not need to construct multiple "API calls". In fact - I don't need to worry about how the API calls are constructed. I can just use the wrapper class and remain blissfully ignorant to the internal workings.

10% popularity Vote Up Vote Down


 

@Cugini213

REST API are without sessions. API is any part of code which return you the desired output. So as the API you are using is RESTfull api it has to got some url to hit through. say http:/something/id which will return some JSON input to you. Now that API may return you a webpage containing JSON or a string containing JSON. Using file_get_contents() on the API url you can get the json response in a variable and the you can parse that json to get your data

10% popularity Vote Up Vote Down


 

@Correia994

An API is an Application Programming Interface. It gives other developers the ability to tap into another application and pull/push data between the two.

You use an API by connecting with an API key that is assigned to you by the API provider.

Beyond that, each API is different. You'll need to read the documentation on how to access the data you're looking for.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme