如何使用大于/小于运算符将cUrl JSON请求发送到Yii2 api


How to send cUrl JSON request to Yii2 api using greater/less than operators

作为针对yii2 restfull web服务的cUrl请求的示例,我有以下命令:

curl -i -H "Accept:application/xml" "http://localhost/api/users"

但我想得到id在100到250之间的用户(这不是分页的一部分,它只是这样的请求)。

我应该如何指定此条件?

我试过这个,但它不起作用:

curl -i -H "Accept:application/xml" "http://localhost/api/users?id={"lt": 100, "gt": 30}"

如果不想使用yii2 resfull控制器的分页功能,可以使用limitoffset参数。它有点类似于创建SQL查询。

例如,curl -i -H "Accept:application/xml" "http://localhost/api/users?offset=100&limit=150

和在你的控制器

class UserController extends Controller
{
    public function actionIndex()
    {
        $limit = Yii::$app->request->post('limit', 20);
        $offset = Yii::$app->request->post('offset', 0);
        return new ActiveDataProvider([
            'query' => User::find()->limit($limit)->offset($offset),
        ]);
    }
}