【问题标题】:Pass String Parameters to WP REST API将字符串参数传递给 WP REST API
【发布时间】:2017-06-04 00:06:17
【问题描述】:

我可以将整数值传递给 WP REST API。但是,不能传递非数字字符。它给出了错误。

这是我用的……

add_action( 'rest_api_init', function () {
    register_rest_route( 'crowdapi/v1', '/register/(?P<id>\d+)/(?P<username>\d+)', array(
        'methods' => 'POST',
        'callback' => 'userCheck',
    ) );
} );

知道如何传递字符串吗?

【问题讨论】:

    标签: php wordpress rest wordpress-rest-api


    【解决方案1】:

    你需要试试这个,它会起作用的

    add_action( 'rest_api_init', function () {
        register_rest_route( 'crowdapi/v1', '/register/(?P<id>\d+)/(?P<username>\w+)', array(
            'methods' => 'POST',
            'callback' => 'userCheck',
        ) );
    } );
    

    【讨论】:

      【解决方案2】:

      这对我有用:/(?P&lt;slug&gt;\w+)

      【讨论】:

        【解决方案3】:

        也可以尝试下面的代码来定义端点..

        add_action( 'rest_api_init', function () {
            register_rest_route( 'crowdapi/v1', '/register/(?P<id>\d)/(?P<username>\d)', array(
                'methods' => 'POST',
                'callback' => 'userCheck',
            ) );
        } );
        

        【讨论】:

        • 我使用(?P&lt;stringvar&gt;.+)(?P&lt;stringvar&gt;[^/]+) 作为字符串参数。这实际上取决于您需要返回和放入的内容。
        【解决方案4】:

        我自己找到的……

        字符串使用[a-zA-Z0-9-] 而不是\d

        add_action( 'rest_api_init', function () {
            register_rest_route( 'crowdapi/v1', '/register/(?P<id>\d+)/(?P<number>[a-zA-Z0-9-]+)', array(
                'methods' => 'POST',
                'callback' => 'userCheck',
            ) );
        } );
        

        【讨论】:

          猜你喜欢
          • 2016-03-16
          • 1970-01-01
          • 1970-01-01
          • 2011-05-12
          • 2018-04-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-10-28
          相关资源
          最近更新 更多