最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

rest api - Is there anyway to format my EndPoint URL in WordPress?

programmeradmin2浏览0评论

I have wrote an endpoint in WordPress and it works just fine. The problem is with the URL. The current format of the URL request must be like this:

/msisdn=019009

However, The URL request must be in this format:

;msisdn=019009

How do I format that?

Here is my code:

register_rest_route( 'zaindob/v1', '/sync_order/' . 'key=' . '(?P<key>\d+)' .  '/msisdn=' . '(?P<msisdn>\d+)' , array( 

    'methods' => 'GET',
    'callback' => 'updatetable',
  ) );
} );

I have wrote an endpoint in WordPress and it works just fine. The problem is with the URL. The current format of the URL request must be like this:

https://iotkidsiq/wp-json/zaindob/v1/sync_order/key=011900/msisdn=019009

However, The URL request must be in this format:

https://iotkidsiq/wp-json/zaindob/v1/sync_order?key=011900&msisdn=019009

How do I format that?

Here is my code:

register_rest_route( 'zaindob/v1', '/sync_order/' . 'key=' . '(?P<key>\d+)' .  '/msisdn=' . '(?P<msisdn>\d+)' , array( 

    'methods' => 'GET',
    'callback' => 'updatetable',
  ) );
} );
Share Improve this question edited Sep 5, 2019 at 15:04 user174452 asked Sep 5, 2019 at 14:43 Ahmed Dawood SalmanAhmed Dawood Salman 111 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 1

You don't. Query args are not part of the route URL. Your endpoint URL is:

https://iotkidsiq/wp-json/zaindob/v1/sync_order

So needs to be registered as:

register_rest_route( 'zaindob/v1', '/sync_order', array( 
    'methods' => 'GET',
    'callback' => 'updatetable',
) );

key and msisdn are arguments that are sent to your endpoint. To define these use set the args property of the endpoint options:

register_rest_route( 'zaindob/v1', '/sync_order', array( 
    'methods'  => 'GET',
    'callback' => 'updatetable',
    'args'     => array(
        'key'    => array(
            'type'     => 'integer',
            'required' => true,
        ),
        'msisdn' => array(
            'type'     => 'integer',
            'required' => true,
        ),
    ),
) );

Now your endpoint callback can accept the key and msisdn paramaters, and they are required for the endpoint to return a result:

function updatetable( $request ) {
    $key    = $request->get_param( 'key' );
    $msisdn = $request->get_param( 'msisdn' );

    // etc.
}
发布评论

评论列表(0)

  1. 暂无评论