Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 无法为post方法wordpress REST服务传递参数_Php_Wordpress_Rest_Routes_Wordpress Rest Api - Fatal编程技术网

Php 无法为post方法wordpress REST服务传递参数

Php 无法为post方法wordpress REST服务传递参数,php,wordpress,rest,routes,wordpress-rest-api,Php,Wordpress,Rest,Routes,Wordpress Rest Api,我是Wordpress和REST-API的新手,我已经能够让get函数工作,但我无法将参数传递给post函数下面我粘贴了我的代码。当输入活动id时,结果是,但如果我提供,我会得到{“code”:“rest_no_route”,“message”:“找不到与URL和请求方法匹配的路由”,“data”:{“status”:404}在Wordpress的REST API控制台中运行/localhost/v1/activeitem?45时,我得到“完成”。所以在这一点上,我想知道我做错了什么的思想是xx

我是Wordpress和REST-API的新手,我已经能够让get函数工作,但我无法将参数传递给post函数
下面我粘贴了我的代码。当输入活动id时,结果是,但如果我提供,我会得到{“code”:“rest_no_route”,“message”:“找不到与URL和请求方法匹配的路由”,“data”:{“status”:404}
在Wordpress的REST API控制台中运行/localhost/v1/activeitem?45时,我得到“完成”。所以在这一点上,我想知道我做错了什么
的思想是xxx/activeitem调用将提供活动id,而xxx/activeitem[参数]将根据提供的id更新活动项目

function LocalHost_GetActiveItem() {
    global $wpdb;
    $querystr = "select option_value as pid from wp_options where option_name = 'acelive_active';";
    $activeitem = $wpdb->get_results($querystr);
    return $activeitem[0];
}

function LocalHost_SetActiveItem($id) {

    //return $id;
    return "done";
}

add_action( 'rest_api_init', function () {
    register_rest_route( 'localhost/v1', '/activeitem/', array(
        array(
            'methods' => 'GET',
            'callback' => 'LocalHost_GetActiveItem',
        ),
        array(
            'methods' => 'POST',
            'callback' => 'LocalHost_SetActiveItem',
            'args' => array('id' => 234)
        ),
    ) );
} );

add_action( 'rest_api_init', function () {
    register_rest_route( 'localhost/v1', '/lastupdate/', array(
        'methods' => 'GET',
        'callback' => 'LocalHost_LastUpdate',
    ) );
} );

确保你的正则表达式是好的。对于id,可以在register\u rest\u route()的$route参数中使用
activeitem/(?P[\d]+)
,如果要更新id,请确保 将寄存器_rest_route()的$override参数设置为true

register\u rest\u路由('localhost/v1','/activeitem/(?P[\d]+)',数组(
'方法'=>'发布',
'callback'=>'LocalHost\u SetActiveItem',
'args'=>array('id'=>234)
),对);

提供xxx/activeitem/123时出现404错误的原因是没有捕获123并将其作为id传递到url,因为没有为路由提供正确的正则表达式。

确保正则表达式正常。对于id,可以在register\u rest\u route()的$route参数中使用
activeitem/(?P[\d]+)
,如果要更新id,请确保 将寄存器_rest_route()的$override参数设置为true

register\u rest\u路由('localhost/v1','/activeitem/(?P[\d]+)',数组(
'方法'=>'发布',
'callback'=>'LocalHost\u SetActiveItem',
'args'=>array('id'=>234)
),对);
提供xxx/activeitem/123时出现404错误的原因是没有捕获123并将其作为id传递到url,因为没有为路由提供正确的正则表达式

register_rest_route( 'localhost/v1', '/activeitem/(?P<id>[\d]+)', array(
          'methods' => 'POST',
        'callback' => 'LocalHost_SetActiveItem',
        'args' => array('id' => 234)

), true );