3

How do I limit the number of results provided by Stack.PHP (a library wrapper over the Stackexchange API)?

For example, how do I get the first 50 questions tagged with a keyword?

The query I currently have is:

$apps = $stackapps->Questions()->Tagged('drupal')->IncludeAnswers()->IncludeBody()->Exec();

1 Answer 1

4

As the author of Stack.PHP, I will try to answer your questions.

"How do I limit the number of results provided by STACK.PHP..."

The API will only return the first 30 items from the result set by default. This number can be set to anywhere from 1 to 100. To set this value in Stack.PHP, call the Pagesize() method on a PagedResponse object. For example:

$response = API::Site('stackapps')->Users()->Exec();
$response->Pagesize(45);

This snippet will return up to 45 items in the response.

"...how do I get the 1st 50 questions tagged with a keyword?"

To return 50 items, you would want to call $response->Pagesize(50); to instruct the PagedResponse object to return up to 50 items in the response. Keep in mind that there could easily be fewer than 50 items returned.

I should also point out that you have an error in your example:

$stackapps->Questions()->Tagged('drupal')->IncludeAnswers()->IncludeBody()->Exec()

Unfortunately there are no methods named IncludeAnswers and IncludeBody. To ensure these items are returned in the response, you will need to make use of filters (introduced in v2.0 of the API). Stack.PHP provides the Filter class to make this easier.

Although you can create a filter using this code:

$filter = new Filter();
$filter->SetIncludeItems(array('question.body', 'question.answers'));

...this is not recommended since filters are stable (they don't change or expire) and can be "baked in" to an application. Instead, visit this page and click the [edit] button next to the "default filter":

enter image description here

Then select the items you want included in the results:

enter image description here

And lastly, click the save button near the bottom to save the filter:

enter image description here

The filter builder will provide you with a unique filter ID that you can use in your application:

enter image description here

So then your code will look something like this:

$apps = $stackapps->Questions()->Tagged('drupal')->Filter('!-q2R_6PL')->Exec();

You must log in to answer this question.

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