我已经在代码中通过以下方式注册了一个 api 请求,然后在邮递员中我调用了该请求并添加了一些参数,但是当我运行 api 请求端点时,它返回 null。如何返回正在发送的数据?/** * This is our callback * function that embeds our phrase in a WP_REST_Response */function addProductFromCRM($data) { //$name = $data['name']; // rest_ensure_response() wraps the data we want to return into a WP_REST_Response, and ensures it will be properly returned. return rest_ensure_response($data);}/** * This function is where we register our routes for our example endpoint. */function wp_register_crm_routes() { // register_rest_route() handles more arguments but we are going to stick to the basics for now. register_rest_route('crm/v1', '/addproduct/', array( // By using this constant we ensure that when the WP_REST_Server changes our readable endpoints will work as intended. 'methods' => 'POST', // Here we register our callback. The callback is fired when this endpoint is matched by the WP_REST_Server class. 'callback' => 'addProductFromCRM', ));}add_action('rest_api_init', 'wp_register_crm_routes');
2 回答
RISEBY
TA贡献1856条经验 获得超5个赞
应该返回什么addproduct端点?JSON?你可以这样做:
function addProductFromCRM($request) {
wp_send_json($request->get_params());
}
30秒到达战场
TA贡献1828条经验 获得超6个赞
您可以根据需要使用以下代码片段,例如 onisset($_POST)或任何其他callback function. 你必须知道你的注册路由 URL 并且必须工作。您可以使用wp_remote_get或wp_remote_post根据您的需要。更多参考请查看WordPress官方网站
$response = wp_remote_get("URL TO YOUR REGISTER ROUTE");
if ( is_array( $response ) ) {
$response_code = wp_remote_retrieve_response_code( $response );
$body = wp_remote_retrieve_body( $response );
$body_data = json_decode($body);
}
- 2 回答
- 0 关注
- 139 浏览
添加回答
举报
0/150
提交
取消