在用户注册时使用wp_emote_post向外部API发送数据

时间:2019-02-17 作者:Dan Sutherland

我正在尝试创建一个基本插件,当用户作为基本订阅者在WordPress网站上注册时,将用户详细信息发送到CRM。

我已经到了一个关键点,我试图尽可能地解释API的细节,但我不确定我是否正确格式化了我的身体数据,因为它根本不起作用。

我可以使用Postman来完成这篇文章,但这是通过json/javascript实现的,我正在尝试使用wp_remote_post.

如有任何指示或意见,将不胜感激。

add_action( \'user_register\', \'send_new_user\', 10, 1 );

function send_new_user($user_id) {

    $new_user = get_userdata($user_id);
    $useremail = $new_user -> user_email;

    $url = \'https://api.capsulecrm.com/api/v2/parties\';
    $body = array(
        \'firstName\' => \'WhyWontYouWork\',
        \'email\' => $useremail
    );

    $args = array(
        \'method\' => \'POST\',
        \'timeout\' => 45,
        \'redirection\' => 5,
        \'httpversion\' => \'1.0\',
        \'sslverify\' => false,
        \'blocking\' => false,
        \'headers\' => array(
            \'Authorization\' => \'Bearer {private token goes here!!!!}\',
            \'Content-Type\' => \'application/json\',
            \'Accept\' => \'application/json\',
        ),
        \'body\' => json_encode($body),
        \'cookies\' => array()
    );

    $request = wp_remote_post ($url, $args);
};

1 个回复
SO网友:sMyles

我建议只设置您绝对需要更改的默认参数,以消除测试时的潜在问题。最好只在需要时添加额外的参数。

可以在此处找到所有参数:https://developer.wordpress.org/reference/classes/WP_Http/request/

如果可能,还可以首先删除所需的身份验证,以便在添加安全性/身份验证之前只测试POST。

也不需要用分号关闭函数;

我建议查看如何设置xdebug,以便您可以在本地调试代码,如果您不能这样做,那么我建议使用error_log 要登录到主机提供商错误日志,以便查看响应。

您还设置blocking 若为false,是否不希望服务器响应?

// If this file is called directly, abort.
if ( ! defined( \'WPINC\' ) ) {
    die;
}

//Find the details of a new user

add_action(\'user_register\', \'send_doqaru_user\', 10, 1);

function send_doqaru_user ($user_id){

    $new_doqaru_user = get_userdata($user_id);
    $doqaru_user_email = $new_doqaru_user -> user_email;

    // get all the meta data of the newly registered user
    $new_user_data = get_user_meta($user_id);

    // get the first name of the user as a string
    $doqaru_user_firstname = get_user_meta( $user_id, \'first_name\', true );
    $doqaru_user_lastname = get_user_meta( $user_id, \'last_name\', true );


    if( ! $new_user ){
        error_log( \'Unable to get userdata!\' );
        return;
    }

    $url  = \'https://api.capsulecrm.com/api/v2/parties\';
    $body = array(
        \'type\' => \'person\',
        \'firstName\' => $doqaru_user_firstname,
        \'lastName\' => $doqaru_user_lastname,
        \'emailAddresses\'     => array(
            \'type\' => \'Work\',
            \'address\' => $new_user->user_email

    ));

    $args = array(
        \'method\'      => \'POST\',
        \'timeout\'     => 45,
        \'sslverify\'   => false,
        \'headers\'     => array(
            \'Authorization\' => \'Bearer {token goes here}\',
            \'Content-Type\'  => \'application/json\',
        ),
        \'body\'        => json_encode($body),
    );

    $request = wp_remote_post( $url, $args );

    if ( is_wp_error( $request ) || wp_remote_retrieve_response_code( $request ) != 200 ) {
        error_log( print_r( $request, true ) );
    }

    $response = wp_remote_retrieve_body( $request );
}