Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

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?

share|improve this question

closed as too broad by John Conde Jul 31 at 11:45

There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs.If this question can be reworded to fit the rules in the help center, please edit the question.

1  
Nice! I have created an API token at DigitalOcean and tried to use it via curl, it works great! –  William David Edwards Jul 31 at 14:53

3 Answers 3

up vote 2 down vote accepted

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.

share|improve this answer
    
This makes things much more clear for me :) Therefore, I'll accept this answer. –  William David Edwards Jul 31 at 10:25
1  
+1, minor bug in your code sample... WeatherApi::APIKEY should be Weather::APIKEY or simply self::APIKEY. (And the URL parameter value $location should be urlencode()d before constructing the request URL if you are passing in special characters, such as the spaces in your example.) –  w3d Jul 31 at 10:55
    
@w3d Thanks for letting me know! Had originally called the class WeatherApi but shortened it for the sake of space. Also added urlencode on the $location param. –  Wexford Jul 31 at 11:52

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.

share|improve this answer
1  
Well, not all APIs need an API key. In most cases you just need an API key if you request sensitive data. –  Jurik Jul 31 at 7:52
    
Yes, I know. Each API has functions. How do I call them in PHP? There must be a 'general' way to do that. –  William David Edwards Jul 31 at 8:10
    
@Jurik Yeah, given the question, I think you're splitting hairs. ;) –  Paul Dessert Jul 31 at 8:10
    
@WilliamDavidEdwards - You'll need to learn php. There is no way I can teach you how to use an unknown API here. –  Paul Dessert Jul 31 at 8:11
    
@PaulDessert I know PHP. The API I want to use RESTful. All responses are return JSON objects. Is that enough info? :) –  William David Edwards Jul 31 at 8:13

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

share|improve this answer
    
I think I'm beginning to understand the basics. Thanks. –  William David Edwards Jul 31 at 8:30

Not the answer you're looking for? Browse other questions tagged or ask your own question.