doufangmu9087 2019-05-22 04:57
浏览 81

如何通过wordpress api请求从对象发送的响应中获得响应

I have registered a api request the following way in the code, then in postman I call that request and add some params, but when I run the api request endpoint it returns null. How do I return the data that's being sent?

/**
 * 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条回答 默认 最新

  • doudao9915 2019-05-22 06:43
    关注

    What addproduct endpoint should return? JSON? You can do something like this:

    function addProductFromCRM($request) {
        wp_send_json($request->get_params());
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题