3

I would like to know how to use the Stack API and get all questions with a particular tag. I am coding in PHP. So I would like to write a script which can get all questions with a particular tag. I already got an API key but I do not know what to do with it.

1 Answer 1

4

I would highly recommend using Stack.PHP, a library for the API that I wrote. Writing the code to access the API in PHP yourself will take a considerable amount of time once you add pagination, request throttling, etc. By using Stack.PHP, you needn't worry about any of that.

Now on to your question:

"...get all questions with a particular tag."

This is very easy with Stack.PHP. For example, to get all of the posts here on Stack Apps with the tag, you would do the following:

$response = API::Site('stackapps')->Questions()->Tagged('app')->Exec();

while($question = $response->Fetch(TRUE))
    echo $question['title'];

The example above will fetch and display the titles of all of the questions with the tag. Keep in mind that the code above will fetch as many pages as necessary to get all of the questions - if you only want the first page, pass FALSE to Fetch() instead of TRUE.

"I already got an API key but I do not know what to do with it."

Your app can make up to 300 requests without a key (like in the example above) but this number increases to 10,000 with an API key. You can provide Stack.PHP with your API key running this line before making any requests:

API::$key = 'key_goes_here';

If you are interested in using Stack.PHP, you can download the latest version (0.5 beta 1) here. The archive consists of the source code, documentation, examples, and test suite. The source code is in the src/ directory.

If you want some help getting started, check out my tutorial here:
https://docs.google.com/document/pub?id=11n_dp6t2jpPcgqNuoEOO0fwLvKxYp_HH-zGoycAnUmY

...and be sure to check out the Stack.PHP website stackphp.quickmediasolutions.com.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .