WordPress:使用wp_remote_post发送帖子请求无法正常工作

时间:2019-11-27 00:19:44

标签: php wordpress woocommerce wordpress-rest-api

我需要通过woo-commerce自定义端点在product restful API页面上提交表单,问题是wp_remote_post无法与我一起正常工作,或者我大部分时间都在做某事错误的。

到目前为止我的代码

add_action('rest_api_init', function () {
    register_rest_route( 'yith-composite', 'add-item',array(
        'methods' => 'POST',
        'callback' => 'yith_custom_add_item'
    ));
});

function yith_custom_add_item( WP_REST_Request $request ) {
    $currentuserid_fromjwt = get_current_user_id();
    $product_id = $_GET['id'];
    $url = get_permalink( $product_id );
    $user_cookie = wp_generate_auth_cookie($currentuserid_fromjwt, time() + (10 * 365 * 24 * 60 * 60), 'logged_in');

    $response = wp_remote_post( $url, array(
        'body'    => json_encode($request->get_json_params()['body']),
        'headers' => array(
                           'Content-Type' => 'multipart/form-data;',
                           'cookie' => 'PHPSESSID=2f3f5db725c728c627ada8196ca9d438; tk_ai=woo%3A4404G2GnKIS%2BNq44234vMsYl; wordpress_test_cookie=WP+Cookie+check; wp-settings-time-138=1574607034; wordpress_logged_in_856ec7e7dd32c9b2fc11b366505cf40d=' . $user_cookie
                       ),
    ) );
    return $response;
    exit;
}

我向邮递员发送以下正文,包括不记名令牌:

{
    "body": {
        "ywcp_selection[153CE4EE-C1A0-6D39-9AD5-58B51C5AB306]": -1,
        "ywcp_variation_id[153CE4EE-C1A0-6D39-9AD5-58B51C5AB306]": 124136,
        "ywcp_quantity[153CE4EE-C1A0-6D39-9AD5-58B51C5AB306]": 1,
        "ywcp_selected_product_value[153CE4EE-C1A0-6D39-9AD5-58B51C5AB306]": 10,
        "ywcp_selection[471BAD03-9C3B-B44E-8584-348A5F33F8A6]": -1,
        "attribute_pa_sphere[471BAD03-9C3B-B44E-8584-348A5F33F8A6]": "5-75",
        "ywcp_variation_id[471BAD03-9C3B-B44E-8584-348A5F33F8A6]": 110359,
        "ywcp_quantity[471BAD03-9C3B-B44E-8584-348A5F33F8A6]": 1,
        "ywcp_selected_product_value[471BAD03-9C3B-B44E-8584-348A5F33F8A6]": 102552,
        "quantity": 1,
        "add-to-cart": 102491
    }
}

0 个答案:

没有答案